Code can be rolled back by deploying the old version. Data cannot. A migration written in a hurry is among the most expensive mistakes an agent can make.
Prefer additive changes
- Add a column rather than rename or drop one.
- Add a table rather than change one in place.
- Make new columns nullable or give them a default so old code keeps working.
Expand and contract
To rename a column safely, do it in steps across several deploys:
- Add the new column.
- Write to both columns and backfill the old data.
- Switch reads to the new column.
- Stop writing the old column.
- Drop the old column in a later release.
Process
- Run every migration against a copy of production-like data first.
- Check lock behaviour and run time on large tables.
- Keep migrations in version control, ordered and run by a separate step with the rights they need.
- A person reads each migration before it is merged. Destructive statements get extra attention.
- Take a backup before applying anything destructive.
In this project
Contextingo runs additive migrations in a separate job as the database owner, and the application never changes the schema at run time. Related: least privilege for coding agents.