Home / Lessons / Set the boundaries / 19 of 27
The database must be ready first
Code that deploys by itself must never arrive before the tables it expects.
- New code expects new tables. In production they do not exist yet.
- Merge first, and the code deploys before its tables exist.
- So the database goes first: an additive change, checked and approved.
- Your part: now merge.
- The code arrives and finds what it expects.
New code may expect new tables, columns or permissions. If the code deploys automatically and production does not have them yet, it fails.
So additive database changes are applied and verified before the code that depends on them is merged.
A migration that has been applied is never edited. If it has a defect, a new corrective migration is added. Editing it would make different environments disagree about what the same migration means.
How I do it
The order is fixed: additive change, verify real behaviour, database ready, then my merge.
The rule
Database first, code second, and never rewrite a migration that has already run.
Rule 19. Database first, code second, and never rewrite a migration that has already run. (The database must be ready first, https://chiefofagents.si/one-page#rule-19)
The skill I use for this: /principle-make-operations-idempotent. Made by Lauren Tan.
Read next
The ideas behind it
This lesson as plain text, for your agent. Or all 27 on one page.