Skip to content

The Renovations — Online Migrations Story#

Year 2 at the Elephant Dormitory. The hostel is packed every night, Porto is booming, and Management has a stack of blueprints for new wings, RFID badges, and strict rules — all while guests are asleep in the bunks.

Year 2 at the Elephant Dormitory. The hostel is packed every night, Porto is booming, and Management has a stack of blueprints for new wings, RFID badges, and strict rules — all while guests are asleep in the bunks.

Management: "RENOVATE EVERYTHING! ZERO DOWNTIME! NO BLOCKED CHECK-INS! ALSO, WE BOUGHT THE HOSTEL NEXT DOOR!"

Panda: Easy! I'll just open psql, paste some ALTER TABLE scripts in production, and hope nobody notices!

Ferris: Put the crowbar down, Panda. pgdorm.migrate reads PostgreSQL's own pg_catalog, plans lock-safe online steps, guards against hazards, and records every single change in _pgdorm.

The Renovations is the companion narrative to The Story, guiding you through every corner of pgdorm.migrate and the pgdorm CLI in learning-graph order. Every chapter is backed by a standalone runnable script in examples/ executed against live PostgreSQL with verified # => outputs and captured wire SQL.

New to migrations? Start with the internship.

Chapter 0 — The Internship builds the whole Dormitory schema from its first table, one migration per step, with a diagram of where you are after each one. Every later chapter changes that schema.

Two migration modes, one typed engine#

pgdorm.migrate lets you pick the workflow that matches your project stage without switching tools:

  • Mode A — Zero-file declarative sync (pgm.sync_schema() / pgdorm sync): Diff your Python pg.Table models directly against live pg_catalog, review hazards, execute lock-safe online DDL, and record the resulting snapshot in _pgdorm.
  • Mode B — Code-defined versioned releases (pgm.Migration + pgm.MigrationApp / pgdorm upgrade): Commit explicit, code-reviewed, SHA-256-checksummed Python Migration values (generated with pgm.render_python_migration() / pgdorm render) for deterministic staging → production rollouts, explicit renames, and RunPython zero-raw-SQL data backfills.

Act I — Blueprints & Guardrails#

Year 2 · Spring. Read PostgreSQL's own catalog, sync declaratively, catch drift, and evolve live tables without blocking the lobby.

Ch. Renovation What you'll get Superpowers
0 The Internship Build the Dormitory schema from its first table, one reviewed migration per step, with a diagram of where you are after each one. plan_migration() sync_schema() migration_history()
1 Opening the Blueprints Query pg_catalog with typed models, baseline an existing schema, sync declaratively, and catch out-of-band drift. baseline_schema() plan_migration() sync_schema()
2 Knocking Down Walls Safely Expand DDL into CONCURRENTLY and NOT VALID steps, block table rewrites and drops, and guard dependent views. LockMode HazardLevel UnsafeMigrationError

Act II — Planned Renovations#

Year 2 · Summer. Code-reviewed versioned work orders, zero-raw-SQL data backfills, lock-timeout retries, PgBouncer leases, and crash recovery.

Ch. Renovation What you'll get Superpowers
3 Signed Work Orders Ship code-reviewed Python migrations with explicit renames, SHA-256 checksums, and history-divergence guards. Migration MigrationApp upgrade_schema()
4 Moving Elephants Mid-Renovation Split columns zero-downtime via Expand -> RunPython Backfill -> Enforce -> Contract and replay ops in memory. RunPython add_column() / set_not_null() Expand-Contract
5 Hard Hats, Locks & Power Cuts Survive lock queues, PgBouncer transaction pooling, and interrupted CONCURRENTLY indexes with self-healing recovery. lock_timeout / lock_retries MigrationLockError PgBouncer leases

Act III — The Whole Estate#

Year 2 · Autumn. TypeTables, RLS policies, deferrable FK cycles, the pgdorm CLI in CI, and adopting the legacy hostel next door.

Ch. Renovation What you'll get Superpowers
6 Wings, Policies & Living Lookups Evolve TypeTables lock-free, manage RLS policies and deferrable FK graphs, and introspect partitioned tables. SyncTypeTable DEFERRABLE FKs enable_rls() / create_policy()
7 The Foreman's Toolkit Drive migrations from the pgdorm CLI in CI/CD with --json plans, exit-code-2 drift alarms, and static ReadPool checks. pgdorm doctor plan / render / sync pgdorm drift (exit 2)
8 Buying the Building Next Door Audit legacy schemas, compare Zero-DDL Bind vs Full Assimilation, and migrate in place with zero data loss. audit_schema() scaffold_from_db() AdoptionMap

Ready for brownfield onboarding after the Renovations? Continue straight into Assimilation for side-by-side migrations from Pure SQL, SQLAlchemy, SQLModel, Django, Tortoise, Piccolo, and Peewee — or look up any export signature in the Migrations & Assimilation Reference and CLI Reference.