Bylaw.Db.Adapters.Postgres.Checks.EctoChangesetCheckConstraints (bylaw_postgres v0.2.0)

Copy Markdown View Source

Validates Ecto.Changeset.check_constraint/3 annotations for Postgres checks.

Examples

With a check constraint on users.age, before:

def changeset(user, attrs) do
  user
  |> Ecto.Changeset.cast(attrs, [:age])
  |> Ecto.Changeset.validate_number(:age, greater_than_or_equal_to: 13)
end

The database still protects the invariant, but a constraint failure may reach callers as a database error instead of a changeset error attached to :age.

After, annotate the changeset with the matching check constraint:

def changeset(user, attrs) do
  user
  |> Ecto.Changeset.cast(attrs, [:age])
  |> Ecto.Changeset.validate_number(:age, greater_than_or_equal_to: 13)
  |> Ecto.Changeset.check_constraint(:age, name: :users_age_check)
end

Ecto can turn the database rejection into a normal changeset error.

Notes

The check skips dynamic cast or change field lists, check expressions without catalog column metadata, and constraints whose columns cannot be mapped to Ecto schema fields.

Options

  • :validate - explicit false disables this check.
  • :paths - required non-empty list of source paths to parse for changeset functions.
  • :otp_app - OTP app used for compiled schema discovery. When the target repo can report config()[:otp_app], this is inferred.
  • :schema_modules - explicit non-empty list of schema modules to inspect instead of discovering schemas from :otp_app.
  • :rules - optional rule keyword list or non-empty list of rule keyword lists. Rules use only shared scope keys.

This check requires :paths and schema discovery from the target repo's inferred :otp_app, explicit :otp_app, or explicit :schema_modules, so bare-module configuration is not valid.

Run globally for discovered schemas:

{Bylaw.Db.Adapters.Postgres.Checks.EctoChangesetCheckConstraints,
 paths: ["lib/my_app"]}

Run globally for explicit schema modules:

{Bylaw.Db.Adapters.Postgres.Checks.EctoChangesetCheckConstraints,
 paths: ["lib/my_app/catalog"],
 schema_modules: [MyApp.Catalog.Product, MyApp.Catalog.Price]}

Run only for matching rule scopes:

{Bylaw.Db.Adapters.Postgres.Checks.EctoChangesetCheckConstraints,
 paths: ["lib/my_app"],
 rules: [
   where: [schemas: ["public"]],
   except: [[tables: ["legacy_products"], constraints: ["legacy_price_check"]]]
 ]}

The check discovers compiled Ecto schemas through reflection, parses source files for conservative changeset candidates, and only requires check_constraint/3 when Postgres can associate a check constraint with fields that a candidate casts.

Usage

Add this module to the checks passed to Bylaw.Db.Adapters.Postgres.validate/2. See the README usage section for the full ExUnit setup.

Summary

Functions

Implements the Bylaw.Db.Check validation callback.

Types

check_opt()

@type check_opt() ::
  {:validate, boolean()}
  | {:otp_app, atom()}
  | {:paths, [Path.t()]}
  | {:schema_modules, [module()]}
  | {:rules, keyword() | [keyword()]}

check_opts()

@type check_opts() :: [check_opt()]

Functions

validate(target, opts)

@spec validate(target :: Bylaw.Db.Target.t(), opts :: check_opts()) ::
  Bylaw.Db.Check.result()

Implements the Bylaw.Db.Check validation callback.