Bylaw.Db.Adapters.Postgres.Checks.PrimaryKeyType (bylaw_postgres v0.1.0-alpha.1)

Copy Markdown View Source

Validates that Postgres primary key columns use configured data types.

Examples

With rules: [[only: [schema: "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

By default the check inspects all non-system schemas in a Postgres target. Use rules: [...] to configure allowed types for scoped groups of tables:

{Bylaw.Db.Adapters.Postgres.Checks.PrimaryKeyType,
 rules: [
   [
     only: [schema: "public"],
     types: ["uuid"],
     except: [[table: "schema_migrations"]]
   ]
 ]}

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()]}

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() | [matcher_value()]

rule()

@type rule() :: [
  only: 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.