DevTools Logo

Database Migrations Cheat Sheet

Migration workflows, up/down patterns, expand-contract, versioning, and zero-downtime strategies.

Databases
database
migration
schema

Database migrations version-control your schema so changes are repeatable, reviewable, and reversible. A migration is a forward (up) change plus, ideally, a rollback (down).

Migration lifecycle

code
create → apply (up) → verify → rollback (down, if needed)
Table
StepPurpose
CreateGenerate a timestamped migration file.
ApplyRun pending migrations in order.
VerifyConfirm schema and data are correct.
RollbackRevert the last migration.

Up and down pattern

sql
-- up
ALTER TABLE users ADD COLUMN email TEXT;

-- down
ALTER TABLE users DROP COLUMN email;

Expand-contract (zero-downtime)

Table
PhaseAction
ExpandAdd the new column/table (backward compatible).
MigrateBackfill data in the background.
ContractDrop the old column/table after deploy.

This avoids breaking the running app during a deploy.

Best practices

Table
PracticeWhy
One change per migrationEasier to review and rollback.
Idempotent where possibleSafe re-runs.
Never edit applied migrationsHistory must stay immutable.
Test up and downBoth directions must work.
Back up before destructive changesData loss is irreversible.

Common tools

Table
ToolEcosystem
Prisma MigrateNode/TypeScript.
Drizzle KitNode/TypeScript.
Flyway / LiquibaseJava.
AlembicPython/SQLAlchemy.
golang-migrateGo.

References