Skip to content
mentoor

Topic 3 of 4 · 4 min read

Rollback plans before you merge

Goal: Check whether an agent-written database migration can be rolled back and rewrite it as expand-and-contract steps if not.

Before you merge an agent's change, ask one question: if this goes wrong at 3 a.m., what is the exact step that undoes it, and does it still work after the data has changed? Code rolls back easily. Data does not.

Check yourself · 1 of 3

Why is renaming a column in one step risky on a live system?

Choose one answer

Why it matters

Deploying the previous build is usually quick. But if the new version already changed the database, the old code may no longer run. Agents tend to write the tidy, final-state migration: rename the column, drop the old one, add the constraint. That is correct on a quiet staging database and risky on a live one, because for a while old and new code run at the same time.

How to do it

  1. Separate the deploy from the migration. Each should be reversible on its own, or it should be clear that it is not.
  2. Use expand and contract (also called parallel change). Expand: add the new column or table, keep the old one. Migrate: write to both, backfill old rows, switch reads. Contract: only after a release or two of stability, drop the old column in a later change.
  3. Flag destructive steps. DROP, RENAME, NOT NULL on existing data and type changes all deserve a human look.
  4. Write the rollback in the plan. "Redeploy build 412" is a plan. "Revert if needed" is not.
  5. Test the down path on a copy with realistic data, and test old code against the new schema.

Before and after

An agent was asked to rename users.fullname to users.display_name.

ALTER TABLE users RENAME COLUMN fullname TO display_name;

The old build, still serving during the deploy, now fails on every query that reads fullname. Rollback is broken too. Expand and contract fixes it in three releases.

-- Release 1 (expand)
ALTER TABLE users ADD COLUMN display_name text;
UPDATE users SET display_name = fullname WHERE display_name IS NULL;
-- App writes both columns, reads display_name with fallback.

-- Release 2 (migrate): reads and writes use display_name only.

-- Release 3 (contract), after stable
ALTER TABLE users DROP COLUMN fullname;

Each release can be rolled back to the one before it.

What goes wrong

  • Down migration that loses data. Dropping a column cannot be undone by re-adding an empty one.
  • Locking a big table. A backfill in one statement can block writes. Batch it.
  • Untested rollback. The command exists but nobody ran it.
  • Contracting too early. Dropping the old column in the same release removes your way back.

Examples

Catching a drop in review

A diff includes ALTER TABLE orders DROP COLUMN legacy_status; in the same PR that stops reading it. Ask the agent to split it: this PR stops reading the column, a later PR drops it after a release of stability.

The brief that prevents it

Write migrations as expand-and-contract. Additive only in this change.
No DROP or RENAME. Backfill in batches of 5000.
List the rollback step for each release.

The agent now produces the safe shape instead of the tidy one.

Key points

  • Ask what the exact undo step is, and whether it still works after data has changed.
  • Agents favour final-state migrations such as rename and drop, which break old code running during deploy.
  • Expand and contract splits a risky change into additive steps, each reversible, with the drop last.
  • Destructive statements (drop, rename, new NOT NULL, type change) always need a human check.
  • A rollback plan names a concrete command or build, and the down path has been tested on realistic data.

Common mistakes

  • Approving a rename or drop because it passed on an empty staging database.
  • Treating "redeploy the old build" as a full plan when the schema has already changed.
  • Backfilling a huge table in one statement.
  • Dropping the old column in the same release that adds the new one.

Check yourself · 2 of 3

In expand and contract, when is the old column dropped?

Choose one answer

Check yourself · 3 of 3

Which statement is a usable rollback plan?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic