Adding a new column sounds simple, but it can be risky at scale. Schema migrations must balance safety, speed, and rollback capability. The wrong approach can lock tables, block writes, or create downtime in high-traffic systems. The right approach keeps service running and deploys without delay.
First, define the column. Specify the name, type, default value, and nullability. Keep it explicit. Avoid assumptions in the migration script. Decide early if the column will be nullable to prevent costly rewrites later.
Second, plan the migration. In relational databases like PostgreSQL or MySQL, adding a column with a default and non-null constraint can rewrite the entire table. For large datasets, this means long locks. Use a two-step deploy:
- Add the column as nullable without a default.
- Backfill data in small batches.
- Add constraints and defaults in a later migration.
Third, test under load. Apply the migration to a staging or shadow environment seeded with production-like data. Measure query performance before and after. Watch for increases in CPU, IO, and lock contention.