kura_schema_verify (kura v2.31.3)
View SourceRuntime verification that declared schemas match the live database.
Schemas declare their structure via kura_schema:fields/0 and
kura_schema:indexes/0. Migrations are supposed to bring the
database into agreement with those declarations — but every step in
that chain has been a silent failure mode in production:
- a generated migration was forgotten and never committed,
- a hand-written migration had a typo,
- the migration was applied on staging but rolled back on prod (e.g. a deploy aborted partway),
- the schema was edited without regenerating the migration,
rebar3 kura compilemissed the diff (e.g. an index that was declared after the table-creation migration but before index-diffing was supported by the plugin).
Each of these leaves an app that boots cleanly and only fails on
the first query that touches the missing column or constraint —
often hours later, in production. verify/1,2 is the runtime
guardrail: call it from a healthcheck or readiness probe, fail
loudly if drift is detected.
The check is structural, not behavioural: column type comparisons
across the postgres⇄kura type boundary are too noisy to be useful
(e.g. string ⇄ character varying(255) ⇄ text are equivalent
in practice and would generate false positives). We pin only:
- declared tables exist
- declared columns exist (by name) on those tables
- declared indexes exist as valid indexes, matched by what they are, not by what they are called: an index created under a custom name, or renamed since, counts, and one carrying the generated name over the wrong columns does not
A declared index is satisfied by a live one over the same key columns,
in order. A declared unique index needs a unique one that is partial
exactly when the declaration is. A declared plain index is also
satisfied by a unique one, and a partial declaration by a full index,
since either serves the same lookups. When the declaration names an
access method with using, the live index must be built with it
(pg_am.amname), so a btree on a column does not satisfy a declared
hnsw index; without using any method counts. The operator class
and with storage parameters are not compared. Expression columns
never match a declared column, and INCLUDE columns are not key
columns. A partial
index's predicate is not compared, because PostgreSQL stores its own
rewrite of the WHERE text (status = 'active' comes back as
((status)::text = 'active'::text)). An index left invalid by a failed
CREATE INDEX CONCURRENTLY does not count: PostgreSQL ignores it, for
ON CONFLICT inference too.
Out of scope: an on_conflict target naming a constraint
({constraint, Name}) resolves through pg_constraint, which kura's
CREATE UNIQUE INDEX never populates. Only current_schema() is
checked, so under schema-prefix tenancy this does not show whether any
tenant's schema is migrated.
Each call runs two catalog queries. Run it at boot, or cache the result behind a readiness probe, rather than once per probe request.
False negatives are possible (e.g. column type drift), but every issue this does report is a real, blocking problem.
Summary
Functions
The conventional name kura generates for an index declared via
kura_schema:indexes/0. Deprecated: call kura_migration:index_name/2.
Discover all kura_schema-implementing modules reachable from the
application that owns RepoMod. Uses the same app-modules-list
discovery contract that the migrator uses internally to find
migration modules. Useful as a starting point for building scoped
verifiers.
Verify all schemas reachable from the repo's owning application.
Verify a specific list of schema modules. Useful when callers want to scope the check (for example, to a single domain) rather than cover the whole app.
Types
Functions
The conventional name kura generates for an index declared via
kura_schema:indexes/0. Deprecated: call kura_migration:index_name/2.
Discover all kura_schema-implementing modules reachable from the
application that owns RepoMod. Uses the same app-modules-list
discovery contract that the migrator uses internally to find
migration modules. Useful as a starting point for building scoped
verifiers.
-spec verify(module()) -> ok | {drift, [drift_issue()]} | {error, term()}.
Verify all schemas reachable from the repo's owning application.
Returns ok if every declared schema's table, columns, and indexes
exist in the live database. Returns {drift, Issues} listing each
discrepancy. Returns {error, Reason} if the database itself can't
be reached.
-spec verify(module(), [module()]) -> ok | {drift, [drift_issue()]} | {error, term()}.
Verify a specific list of schema modules. Useful when callers want to scope the check (for example, to a single domain) rather than cover the whole app.