Servers

Database migrations without downtime

Expand, migrate, contract: renaming a column while the old code is still running, in three deploys you can each roll back.

Alex Rivera

· 7 min read

A railway line curving through autumn trees

Two versions at once

During a rolling deploy, old and new code run side by side against the same database. A migration that the new code needs and the old code can't survive will break production for a few minutes, every time.

Expand

Add the new shape next to the old one. Nothing reads it yet, so this step is always safe to ship and to roll back.

sql
ALTER TABLE users ADD COLUMN display_name text;UPDATE users SET display_name = full_nameWHERE display_name IS NULL;

Migrate

Ship code that writes both columns and reads the new one. Backfill in batches so a large table doesn't hold a lock for minutes.

ts
await db.update(users)  .set({ fullName: name, displayName: name })  .where(eq(users.id, id));

Contract

Once no running code reads the old column, drop it. Wait a full deploy cycle first: the step you can't roll back should be the one nobody depends on.

Keep reading