04.3.5 · Skill in Data engineering

Schema migrations

Changing the database structure safely while the product keeps running.

Migrations evolve the schema in versioned steps. Safe migrations avoid locking tables and stay compatible with the code running before and after the deploy. Expand and contract patterns let you change data without downtime.

Your status · Schema migrations

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Migrations change the database schema in versioned steps, next to the code that uses it. During a deploy, old code and new code run at the same time against the same database. A safe migration works with both, does not lock a busy table, and can stop halfway. Product engineers write most migrations, so this decides whether shipping a feature can take the product down.

How to do it

  1. Keep every schema change in a versioned migration file, reviewed like code.
  2. Expand first: add the new column or table without removing anything.
  3. Migrate: write to both, backfill old rows in small batches, then move reads to the new place.
  4. Contract last: remove the old column only when no running code uses it.
  5. Split destructive steps across separate deploys, so each one can be rolled back.
  6. Test the migration on a copy of production data, and time it.

Common mistakes

  • Renaming or dropping a column in the same deploy as the code change.
  • A backfill of millions of rows in one transaction.
  • Adding an index in a way that locks the table on a large database.
  • Migrations that work on an empty development database only.

Example

GitLab’s rule for dropping a column takes three releases. First, the code ignores the column, so nothing reads or writes it. In the next release, a migration that runs after the deploy drops it. In the release after that, the ignore rule is removed. Dropping a column cannot easily be rolled back, so no release combines these steps.

Timeline

Dropping a column at GitLab, using its own example

  1. Release 12.5Ignore the columnDecisionRemove every reference in the code
  2. Release 12.6Drop the columnShippedIn a migration that runs after the deploy
  3. Release 12.7Remove the ignore ruleResultThe column is gone everywhere

Old and new code never disagree about the column, because each release changes only one side.

ReportedGitLab development docs, Avoiding downtime in migrations

Sources: Evolutionary database design (Pramod Sadalage and Martin Fowler) ↗ · Parallel change (Danilo Sato) ↗ · Avoiding downtime in migrations (GitLab) ↗

Resources

2
  • articleStart here
    Parallel change ↗
    Danilo Sato (martinfowler.com) · 6 min

    Expand, migrate, contract. The pattern behind changing schemas and APIs without breaking running code.

  • articleGo deeper
    Evolutionary database design ↗
    Pramod Sadalage and Martin Fowler · 40 min

    How to change a database schema in small, safe steps alongside the code that uses it.

Tools

1
Tools for Data engineering