Change a database schema without downtime or data loss by expanding first, migrating in the background, and contracting only once nothing reads the old shape. Use when altering a schema, renaming a column, or planning a migration.
--- name: ship-a-safe-migration category: coding description: >- Change a database schema without downtime or data loss by expanding first, migrating in the background, and contracting only once nothing reads the old shape. Use when altering a schema, renaming a column, or planning a migration. --- # Ship a Safe Migration For a few minutes during every deploy, **old code and new code run at the same time against the same database**. Most migration disasters are a schema that only one of those two versions can use. The fix is to never have a moment where the schema and the running code disagree. That means no migration is a single step. ## Expand → Migrate → Contract Three deploys, not one. Each is independently revertible. ### Deploy 1 — Expand Add the new shape. Change nothing that exists. - Add the new column as **nullable**, with no default that rewrites every row. - Add new tables and indexes. - Write to **both** old and new shapes. Read from the old one. The old code still works, because from its perspective nothing changed. The new code works, because everything it needs exists.
You are reading the opening of this skill.
The rest is open to any signed-in account — free included. Reading the library costs nothing.
Shared publicly by the author and open for any use. See the Terms.