Part 4 · 1 chapters · ~8 min

Database Migrations During Deploys

The compatibility rule (version N and N+1 against one schema), expand and contract across several deploys, migrations run before or after the code, online DDL per engine, backfills during deploys, and migration tooling (Flyway, Liquibase, Alembic, Rails, Prisma, Atlas).

5

Compatible across two versions

the rule
  1. During any rollout, two versions of code run against one schema.
  2. So every schema change must work with the code before it and the code after it.
  3. Run additive migrations before the code that needs them; run destructive ones after no code uses the old shape.
  4. Never combine a breaking schema change and the code that depends on it in one deploy.
toolecosystemnote
Flyway, LiquibaseJVM (and anything)versioned SQL or changelogs, checksums
Alembic, Django migrationsPythonautogenerate, then review the SQL
Rails migrationsRubystrong_migrations gem flags unsafe operations
Prisma Migrate, Drizzle KitNodegenerated SQL; review for locks
Atlas, sqitchanydeclarative diffs, linting for destructive changes
EXPAND AND CONTRACT
renaming a column with zero downtime, across five deploys
1 expandadd new column full_name(nullable)
swipe the figure sideways, or tap expand for full screen
1/5
expand
Add the new shape without removing the old: a nullable column, a new table, a new index built concurrently. Old code ignores it.
add, never remove, in the first stepold code keeps working