Adding a new column sounds simple. In production, it can break everything. Schema changes hit the most fragile points of a system: migrations, data types, indexes, and the services that consume them. A single oversight can turn a deploy into an outage.
A safe new column migration starts with clarity. Decide if it needs a default value or if it can be nullable. Using NULL reduces the migration lock time, but may require application logic updates. If you need a default, set it in the application layer first, then backfill asynchronously. Direct database defaults on large tables can lock writes.
When adding a new column to SQL databases like Postgres or MySQL, run the migration in phases:
- Add the column as nullable with no default.
- Deploy code that writes to both old and new fields.
- Backfill data in small batches.
- Switch reads to the new column.
- Remove the old column if it is no longer needed.
For NoSQL databases, adding a new field is conceptually simpler, but still requires planning. Ensure schema validation rules are updated. Update read/write logic to handle both old and new documents during the transition.