Managing schema in code.
WHAT A MIGRATION IS
A versioned, repeatable change to the schema.
WHY THEY MATTER
The schema must move with the code, across every environment.
WHAT A MIGRATION SYSTEM TRACKS
Which have been applied, in which order.
WHAT EACH MIGRATION SHOULD BE
Small Reversible where possible Independent of application code
WHY INDEPENDENT
Migrations run when the code may be a different version.
WHAT TO NEVER DO
Edit a migration that has already run anywhere.
WHY
Environments diverge silently.
WHAT TO DO INSTEAD
Write a new migration correcting it.
WHAT TO BE CAREFUL WITH
Data changes inside migrations on large tables Migrations that cannot be reversed Long-running migrations blocking deployment
WHAT TO SEPARATE
Schema changes from data backfills.
WHY
Backfills should run in batches, outside the deployment.
WHAT TO TEST
Every migration against a copy of production data.
WHY PRODUCTION DATA SPECIFICALLY
Constraints added to a table containing violating rows will fail.
WHAT TO RUN THEM AS
An account with schema privileges, not the application's account.
WHAT TO RECORD
How long each took, on production.