SQL migration safety: add column nullable, backfill, then constrain

3670
0

The fastest way to surprise yourself in production is ADD COLUMN ... NOT NULL DEFAULT ... on a large table. My safe pattern is: add the column nullable with no default, backfill in batches, then add the NOT NULL constraint. If I need a default for new rows, I add it after the backfill so writes stay predictable. The key insight is that schema changes are production changes—treat them like deployments with staging and monitoring. I also prefer small migrations that do one thing, because when something goes wrong you want a simple rollback plan. This pattern has kept me out of trouble on tables with millions of rows.