Live schema migrations
A migration that passes on an empty dev database proves the SQL parses. It says nothing about how
long the lock is held, whether the old application version still reads the column you dropped, or
whether the rollback can reach the data.
When this fires
You are about to write, review or sequence a schema or data migration against a database that has
rows in it and something reading them. It does not fire for greenfield schema design, for a
throwaway local database, or for a query change with no DDL.
Procedure
Read the current state before writing any SQL. The live schema, the migration history, and
every reader of the objects you are touching — grep the application, the reports, the jobs, the
other services. A column no code in this repo reads may still be read by a deployed older
version. Name the readers you found; say if you could not enumerate them.
Classify the change. Additive (new table, new nullable column, new index), rewriting
(type change, adding NOT NULL or a unique constraint to existing data, renaming, dropping), or
data-only (backfill, correction). Only additive is safe to ship as one step. Everything else
gets sequenced.
Sequence anything non-additive as expand → migrate → contract, across separate deploys.
- Expand — add the new shape: nullable, unconstrained, unused. Old code is unaffected.
- Dual-write — deploy code that writes both shapes. Wait until every running instance is on
that version, including anything that scales up from an older image.
- Backfill — fill the historic rows (step 5).
- Switch reads — deploy code that reads the new shape. Watch before going further.
- Contract — drop the old shape in a later migration, once no supported version reads it.
Collapsing these into one migration is the most common way a change described as backward
compatible takes the application down.
Make the DDL lock-safe. Every statement takes a lock; what matters is which lock, for how
long, and what queues behind it. A strong lock waiting on one slow query blocks every read that
arrives after it.
- Set a short lock timeout (and statement timeout) for the migration session so blocked DDL
fails fast instead of stalling the table. Retry; do not wait.
- Build indexes without holding a write lock where the engine supports it. On Postgres that is
the concurrent index build — it cannot run inside a transaction block, and a failed one
leaves an invalid index behind that must be dropped before retrying.
- Add check and foreign-key constraints unvalidated first, then validate as a separate
statement, so the full-table scan does not sit under a strong lock.
- On MySQL, confirm the operation is genuinely online for that version and storage engine, or
route it through online-schema-change tooling (gh-ost, pt-online-schema-change) instead.
- Do not assume adding a column is free. Whether it rewrites the table depends on engine,
version, and whether the default is a constant.
Batch the backfill. Bounded ranges over the primary key, committed per batch, resumable
from a recorded cursor, with a pause between batches. A single statement across the whole table
holds locks for its whole duration, bloats WAL/undo, and cannot be stopped halfway. Backfill
in the database where the transform is expressible in SQL; reprocessing rows through the
application pipeline that produced them re-runs its side effects.
Write the rollback and say plainly what it cannot recover. Dropping a column you just added
is a real rollback. A down migration after a destructive step re-creates the shape, not the
data — that rollback is a restore from backup. When that is the case, say so, name the backup
that would be used, and state how long a restore takes. Never describe an irreversible
migration as reversible.
Rehearse on a copy with realistic volume. Record per-statement duration and what each one
locked. Timings from a small dataset are not evidence about production.
Stop before the production run. Applying to a shared or production database is an
outward-facing and potentially destructive action: present the plan, the rehearsal timings, the
rollback and its limits, and ask. Apply to a local, branch or disposable database yourself;
promoting it is the user's call, not yours.
Hand off to verification. Applying is not verifying. The before-counts and checksums that
database-migration-verification needs have to be captured before the migration runs — take
them in this procedure or they are gone.
Checklist
Failure handling
- DDL blocks and the timeout fires — that is the timeout working. Find the blocking session,
wait for a quieter window, retry. Do not raise the timeout to push it through.
- A concurrent index build fails — the leftover index is invalid and will not be used. Drop it
explicitly before retrying; a retry alone does not clean it up.
- The backfill dies partway — resume from the recorded cursor. If there is no cursor, you
cannot tell done rows from undone ones without a full comparison; say that rather than
re-running blind.
- Rehearsal timings look fine but production is much larger — the rehearsal did not cover it.
Say the lock duration is unknown at production scale instead of extrapolating.
- You cannot reach a database at all — the migration is written, not tested. Report it as
written, and do not call it safe.
Evidence to report
The migration files, in the order they deploy. The classification and the deploy boundaries. Per
statement: what lock it takes and how long the rehearsal took, with the row count it ran against.
The backfill's batch size and resume mechanism. The rollback, and what it cannot recover. The
readers you enumerated and the ones you could not. What is still unapplied and awaiting approval —
stated as unapplied, not as done.
1---2name: migrations3description: Change a live schema without breaking the application on it — expand/contract sequencing, lock-safe DDL, batched backfills, and a rollback that is actually reachable. Use when writing, reviewing or sequencing a migration that will run against a database holding real data with live readers. Not for designing a schema from scratch, not for proving afterwards that a migration did what it claimed (that is database-migration-verification), and it never runs the production step for you.4---56# Live schema migrations78A migration that passes on an empty dev database proves the SQL parses. It says nothing about how9long the lock is held, whether the old application version still reads the column you dropped, or10whether the rollback can reach the data.1112## When this fires1314You are about to write, review or sequence a schema or data migration against a database that has15rows in it and something reading them. It does not fire for greenfield schema design, for a16throwaway local database, or for a query change with no DDL.1718## Procedure19201. **Read the current state before writing any SQL.** The live schema, the migration history, and21 every reader of the objects you are touching — grep the application, the reports, the jobs, the22 other services. A column no code in this repo reads may still be read by a deployed older23 version. Name the readers you found; say if you could not enumerate them.24252. **Classify the change.** *Additive* (new table, new nullable column, new index), *rewriting*26 (type change, adding NOT NULL or a unique constraint to existing data, renaming, dropping), or27 *data-only* (backfill, correction). Only additive is safe to ship as one step. Everything else28 gets sequenced.29303. **Sequence anything non-additive as expand → migrate → contract, across separate deploys.**31 - *Expand* — add the new shape: nullable, unconstrained, unused. Old code is unaffected.32 - *Dual-write* — deploy code that writes both shapes. Wait until every running instance is on33 that version, including anything that scales up from an older image.34 - *Backfill* — fill the historic rows (step 5).35 - *Switch reads* — deploy code that reads the new shape. Watch before going further.36 - *Contract* — drop the old shape in a **later** migration, once no supported version reads it.3738 Collapsing these into one migration is the most common way a change described as backward39 compatible takes the application down.40414. **Make the DDL lock-safe.** Every statement takes a lock; what matters is which lock, for how42 long, and what queues behind it. A strong lock waiting on one slow query blocks every read that43 arrives after it.44 - Set a short lock timeout (and statement timeout) for the migration session so blocked DDL45 fails fast instead of stalling the table. Retry; do not wait.46 - Build indexes without holding a write lock where the engine supports it. On Postgres that is47 the concurrent index build — it cannot run inside a transaction block, and a failed one48 leaves an invalid index behind that must be dropped before retrying.49 - Add check and foreign-key constraints unvalidated first, then validate as a separate50 statement, so the full-table scan does not sit under a strong lock.51 - On MySQL, confirm the operation is genuinely online for that version and storage engine, or52 route it through online-schema-change tooling (gh-ost, pt-online-schema-change) instead.53 - Do not assume adding a column is free. Whether it rewrites the table depends on engine,54 version, and whether the default is a constant.55565. **Batch the backfill.** Bounded ranges over the primary key, committed per batch, resumable57 from a recorded cursor, with a pause between batches. A single statement across the whole table58 holds locks for its whole duration, bloats WAL/undo, and cannot be stopped halfway. Backfill59 in the database where the transform is expressible in SQL; reprocessing rows through the60 application pipeline that produced them re-runs its side effects.61626. **Write the rollback and say plainly what it cannot recover.** Dropping a column you just added63 is a real rollback. A down migration after a destructive step re-creates the *shape*, not the64 *data* — that rollback is a restore from backup. When that is the case, say so, name the backup65 that would be used, and state how long a restore takes. Never describe an irreversible66 migration as reversible.67687. **Rehearse on a copy with realistic volume.** Record per-statement duration and what each one69 locked. Timings from a small dataset are not evidence about production.70718. **Stop before the production run.** Applying to a shared or production database is an72 outward-facing and potentially destructive action: present the plan, the rehearsal timings, the73 rollback and its limits, and ask. Apply to a local, branch or disposable database yourself;74 promoting it is the user's call, not yours.75769. **Hand off to verification.** Applying is not verifying. The before-counts and checksums that77 `database-migration-verification` needs have to be captured *before* the migration runs — take78 them in this procedure or they are gone.7980## Checklist8182- [ ] Every reader of the touched objects enumerated, or the gap named83- [ ] Change classified; anything non-additive split across deploys84- [ ] Contract step is a separate, later migration85- [ ] Lock and statement timeouts set for the migration session86- [ ] Index builds and constraint validation kept off strong locks87- [ ] Backfill batched, resumable, and expressible without re-running app side effects88- [ ] Rollback written, and its limits stated where it cannot restore data89- [ ] Rehearsed on realistic volume, with timings recorded90- [ ] Before-state counts and checksums captured for verification91- [ ] Production application left to the user, with the plan presented9293## Failure handling9495- **DDL blocks and the timeout fires** — that is the timeout working. Find the blocking session,96 wait for a quieter window, retry. Do not raise the timeout to push it through.97- **A concurrent index build fails** — the leftover index is invalid and will not be used. Drop it98 explicitly before retrying; a retry alone does not clean it up.99- **The backfill dies partway** — resume from the recorded cursor. If there is no cursor, you100 cannot tell done rows from undone ones without a full comparison; say that rather than101 re-running blind.102- **Rehearsal timings look fine but production is much larger** — the rehearsal did not cover it.103 Say the lock duration is unknown at production scale instead of extrapolating.104- **You cannot reach a database at all** — the migration is written, not tested. Report it as105 written, and do not call it safe.106107## Evidence to report108109The migration files, in the order they deploy. The classification and the deploy boundaries. Per110statement: what lock it takes and how long the rehearsal took, with the row count it ran against.111The backfill's batch size and resume mechanism. The rollback, and what it cannot recover. The112readers you enumerated and the ones you could not. What is still unapplied and awaiting approval —113stated as unapplied, not as done.