Expand description
Apply config to the database: DDL for schemas, enums, tables, indexes, and foreign keys. Order follows PostgreSQL dependencies (see docs/postgres-config-schema.md § 3.5).
Structs§
- Migration
Execution Result - Result returned by
execute_migration_plan. - Migration
Plan - Computed diff between two package versions expressed as ordered migration steps.
- Migration
Step - One step in a migration plan: a DDL statement with metadata.
- Migration
Summary
Enums§
- Migration
Operation - Migration
Risk - Risk category associated with a migration step.
- Migration
Safety - How safely a migration step can be executed.
- Step
Decision - Whether a plan step still has work to do against a particular database.
Constants§
- RLS_
TENANT_ COLUMN - Name of the column added to app tables when RLS is enabled. Used by migration and CRUD.
Functions§
- apply_
migrations - Apply full config to the database: CREATE SCHEMA, CREATE TYPE, CREATE TABLE, CREATE INDEX, ADD FK.
Validates config first. Idempotent for schemas and types (IF NOT EXISTS); tables are CREATE TABLE only (fails if exists).
When
schema_overrideisSome(s), app tables/indexes/FKs are created in schemasinstead of config schema names (e.g. for schema-strategy tenants). Whenrls_tenant_columnisSome(col), each table gets that column (if missing), RLS enabled, and policies usingcurrent_setting('app.tenant_id', true). - apply_
rls_ to_ tables - Ensure every table in
confighas the RLS tenant column, row-level security enabled, and the four tenant-isolation policies. Tables flaggedglobalinstead get asymmetric policies — every tenant may read (SELECT USING (true)) but only the Platform Admin tenant may write — so they hold data shared across all RLS tenants. Fully idempotent (ADD COLUMN IF NOT EXISTS,DROP POLICY IF EXISTSthenCREATE POLICY), so it is safe to run on both fresh installs and upgrades. On upgrades this is the path that backfills the tenant column + policies for newly created tables, which the diff-based migration plan does not touch. - compute_
migration_ plan - Diff two package configs and produce an ordered list of migration steps.
This is a pure function — it does not touch the database.
Pass the result to
execute_migration_planafter user confirmation. - execute_
migration_ plan - Execute a pre-computed
MigrationPlanagainst the tenant database. - history_
table_ ddl - Build CREATE TABLE DDL for the
{table}_historycompanion table used by row versioning. All source columns are replicated with their types but as nullable and without any constraints (no NOT NULL, no UNIQUE, no FK, no CHECK). Five versioning metadata columns are prepended. - reconcile_
step - Decide whether
stepneeds to run against the database described bysnap. - revert_
migrations - Revert migrations for a package: drop tables, enum types, and schema (if not public) in reverse order of apply. Uses the same schema_override as apply_migrations (tables/enums live in that schema).