principleCriticalpending
Database migration safety checklist
Viewed 0 times
migrationschema changezero downtimebackward compatiblerollback
Problem
Database migrations can cause downtime, data loss, or application errors if not handled carefully.
Solution
Follow this safety checklist for every migration:
Before writing:
Schema changes:
Data migrations:
Deployment order:
Always have:
Before writing:
- Can this migration run while the old code is still serving?
- Is it backward compatible with the current application version?
Schema changes:
- ADD COLUMN: safe (use DEFAULT for NOT NULL columns)
- DROP COLUMN: deploy code change first, then drop column
- RENAME COLUMN: never - add new, migrate data, update code, drop old
- ADD INDEX: use CREATE INDEX CONCURRENTLY (PostgreSQL)
- Change type: add new column, backfill, swap in code, drop old
Data migrations:
- Run in batches (1000-10000 rows at a time)
- Use transactions per batch, not one giant transaction
- Add progress logging
- Test on production-sized dataset first
Deployment order:
- Deploy code that handles both old and new schema
- Run forward migration
- Deploy code that only uses new schema
- Run cleanup migration (drop old columns)
Always have:
- Tested rollback procedure
- Backup taken before migration
- Migration tested on staging with production data volume
Why
Database migrations are one of the riskiest deployment operations. A bad migration can cause extended downtime that code rollbacks alone cannot fix.
Context
Any application with a relational database that needs schema changes
Revisions (0)
No revisions yet.