Skip to main content

Module migration

Module migration 

Source
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§

MigrationExecutionResult
Result returned by execute_migration_plan.
MigrationPlan
Computed diff between two package versions expressed as ordered migration steps.
MigrationStep
One step in a migration plan: a DDL statement with metadata.
MigrationSummary

Enums§

MigrationOperation
MigrationRisk
Risk category associated with a migration step.
MigrationSafety
How safely a migration step can be executed.
StepDecision
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_override is Some(s), app tables/indexes/FKs are created in schema s instead of config schema names (e.g. for schema-strategy tenants). When rls_tenant_column is Some(col), each table gets that column (if missing), RLS enabled, and policies using current_setting('app.tenant_id', true).
apply_rls_to_tables
Ensure every table in config has the RLS tenant column, row-level security enabled, and the four tenant-isolation policies. Tables flagged global instead 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 EXISTS then CREATE 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_plan after user confirmation.
execute_migration_plan
Execute a pre-computed MigrationPlan against the tenant database.
history_table_ddl
Build CREATE TABLE DDL for the {table}_history companion 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 step needs to run against the database described by snap.
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).