Full answer
Why not ALTER TABLE ... RENAME COLUMN: the rename is instant, but during a rolling deploy some servers run the old code, which immediately fails with 'column does not exist'.
Expand and contract sequence:
- Migration: add
full_name(nullable). - Release A: write both
nameandfull_name; readname. - Backfill
full_namefromnamein batches. - Release B: read
full_name; still write both, so a rollback to A works. - Release C: stop writing
name. - Migration: drop
name.
A trigger that copies between the columns can replace dual writes in code. Alternatively, a view exposing the old name lets old code continue reading while the base table changes.
The general rule: every migration must work with the code version before and after it, because both run at the same time for a while.
Key points
- Rolling deploys run two code versions
- In-place renames break the old version
- Expand, dual write, backfill, switch, contract
- Each step must support a rollback
- Views or triggers can bridge versions
Common mistakes
- Renaming columns in the same release as the code change
- Dropping the old column before all servers stop using it
Follow-ups
- How would you rename a table the same way?
- How do you decide when to drop the old column?
Tips
- State the two-version rule