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.

  1. New code expects new tables. In production they do not exist yet.
  2. Merge first, and the code deploys before its tables exist.
  3. So the database goes first: an additive change, checked and approved.
  4. Your part: now merge.
  5. 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.

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.

Try “secrets”, “merge”, “handover” or “database”.