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

Copy Markdown View Source

Validates that Postgres primary key columns use configured data types.

Examples

With rules: [where: [schemas: ["public"]], types: ["uuid"]], before:

CREATE TABLE users (
  id bigint PRIMARY KEY
);

CREATE TABLE audit_events (
  message text NOT NULL
);

Mixed primary key conventions complicate schemas, fixtures, foreign keys, and application code. Tables without primary keys are harder to address safely.

After, use the configured primary key type:

CREATE TABLE users (
  id uuid PRIMARY KEY
);

Tables now follow one identifier convention, and every scoped table has a stable row identity.

Notes

Tables with no primary key fail, and composite primary keys pass only when every primary key column has an allowed type. Exclude tables such as schema_migrations when they intentionally use a different convention.

Options

  • :validate - explicit false disables this check.
  • :rules - rule keyword list or non-empty list of rule keyword lists.
  • :types - required non-empty list of allowed primary key type names inside each rule.

This check requires :types, so bare-module configuration is not valid.

Run globally:

{Bylaw.Db.Adapters.Postgres.Checks.PrimaryKeyType,
 rules: [types: ["uuid"]]}

Run only for matching rule scopes:

{Bylaw.Db.Adapters.Postgres.Checks.PrimaryKeyType,
 rules: [
   [where: [schemas: ["public"]], types: ["uuid"]],
   [where: [schemas: ["billing"], tables: [~r/^invoice_/]], types: ["uuid", "bigint"]]
 ]}

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()} | {:rules, rule() | [rule()]}

check_opts()

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

matcher()

@type matcher() :: [
  schema: matcher_values(),
  table: matcher_values(),
  column: matcher_values()
]

matcher_value()

@type matcher_value() :: String.t() | Regex.t()

matcher_values()

@type matcher_values() :: [matcher_value()]

rule()

@type rule() :: [
  where: matcher() | [matcher()],
  except: matcher() | [matcher()],
  types: [String.t()]
]

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.