StoicBuilder

HandbookXI the TabulariumMigrations without downtime

XIthe Tabularium, Model the data

Migrations without downtime

1 min readUpdated 28 November

In plain words

A zero-downtime migration changes the schema in small steps that both the old and the new code can live with, often called expand and contract, so the app keeps working during and after the deploy.

Where you see it

Harbor renames invoices.total to amount_cents. Done in one migration, every server still running the old code fails with column "total" does not exist for the minutes the deploy takes. Done in steps, nobody notices.

Expand and contractdatabase
1 expand alter table invoices add column amount_cents bigint;2 deploy code writes both total and amount_cents3 backfill update invoices set amount_cents = total where amount_cents is null; -- in batches4 deploy code reads amount_cents only5 contract alter table invoices drop column total; -- a later release, once nothing reads it
Five small steps instead of one big one. Old and new code can run side by side at every step.

What goes wrong

  1. Renaming or dropping a column in one step

    Old servers, background jobs and cached code still use the old name while the deploy rolls out. Add the new column, move reads and writes over, and drop the old one in a later release.

  2. Locking a big table

    Some changes take a lock that blocks every write until they finish, and on a large table that means minutes of failed checkouts. Create indexes concurrently, outside a transaction, add constraints as not valid and validate them later, and set a lock_timeout.

    create index invoices_team_idx on invoices (team_id); -- blocks writes while it builds
  3. Backfilling millions of rows in one update

    One giant update holds locks for a long time and can time out halfway. Backfill in batches of a few thousand rows, with a short pause between them.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Plan this schema change as expand and contract, so old and new code can both run against the database at every step. Split it into separate migrations and deploys, create any index concurrently, backfill in batches, and tell me which statements take locks that block writes.

Quick check

Sign in to earn +10 XP for it

Harbor needs to rename invoices.total to amount_cents with no downtime. What's the safe first step?

Got a prompt that touches migrations without downtime?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.