# `Bylaw.Db.Adapters.Postgres.Checks.PrimaryKeyType`
[🔗](https://github.com/ryanzidago/bylaw/blob/v0.1.0-alpha.1/lib/bylaw/db/adapters/postgres/checks/primary_key_type.ex#L1)

Validates that Postgres primary key columns use configured data types.

## Examples

With `rules: [[only: [schema: "public"], types: ["uuid"]]]`, before:

```sql
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:

```sql
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:

```elixir
{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](readme.html#usage) for the full ExUnit setup.

# `check_opt`

```elixir
@type check_opt() :: {:validate, boolean()} | {:rules, [rule()]}
```

# `check_opts`

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

# `matcher`

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

# `matcher_value`

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

# `matcher_values`

```elixir
@type matcher_values() :: matcher_value() | [matcher_value()]
```

# `rule`

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

# `validate`

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

Implements the `Bylaw.Db.Check` validation callback.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
