← Back to SQL, page 22

sql-0437 · Hard senior

A column rename is needed, but old and new versions of the application will run side by side during deployment. How do you do it safely?

Make each step compatible with both versions: add the new column, write to both (via the application or a trigger), backfill, switch reads to the new column, then stop writing the old one and drop it in a later release. Renaming in place breaks whichever version still uses the old name.

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:

  1. Migration: add full_name (nullable).
  2. Release A: write both name and full_name; read name.
  3. Backfill full_name from name in batches.
  4. Release B: read full_name; still write both, so a rollback to A works.
  5. Release C: stop writing name.
  6. 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