Rehearse the Rollback Before You Merge an Agent Migration

2026-12-03来源 dev.toEnglish

A migration is not ready because the forward path works. It is ready when a second person has undone it on a copy.

Model-assisted drafts reach shared databases quickly now. A schema change applies, the exit code reads zero, and the merge proceeds. None of that shows whether the change can be reversed.

This play covers one decision only: accept, restrict, or reject an agent-drafted migration. It suits teams where a human merges migrations an assistant drafted. It assumes a disposable copy of the target store exists at the same engine version.

Prerequisites are modest but not optional. The draft must carry an explicit rollback statement, not a promise of one. Two seats are required: the author, and a separate rehearser who did not write the draft.

A single wiki page holds the run. Six beats, one verdict line, one expiry date. Paste it once, then edit fields per change.

Steps

The first beat fixes a baseline. Copy the store to a scratch instance on the same engine version. Record the schema and important constraints, row counts and a deterministic digest over ordered rows and relevant columns. Define the sort order and encoding, or compare rows directly on a small fixture.

The second beat applies the forward change to that copy. Capture the same counts and checksums afterwards. This is the post-forward state, and it is worth keeping.

The third beat runs the stated rollback exactly as written. Read the reported status. Do not trust it yet; the next beat decides.

The fourth beat recomputes counts and checksums. Compare them against the step-one baseline. A restored schema with missing rows fails here, and that failure is the useful signal.

The fifth beat writes a verdict from a closed set: reversible, forward-fix-only, or blocked. Forward-fix-only means no honest undo exists, so recovery depends on a second migration. Attach the rehearser's name and the date.

The sixth beat sets an expiry. A verdict older than the tables it describes is stale. Re-rehearse after any unrelated migration touches the same tables.

The page shape below is illustrative and was not executed. Field names matter here; the syntax does not.

# illustrative record shape — not executed change: add_status_to_invoices engine_version: <same as target> rollback_statement: "reverse SQL, stated explicitly" baseline: {invoices: <count>, checksum: <value>} post_forward: {invoices: <count>, checksum: <value>} post_rollback: {invoices: <count>, checksum: <value>} verdict: reversible | forward-fix-only | blocked rehearser: <name> author: <name> expires: <date, or next migration touching these tables> 

Why counts beat exit codes

A reverse schema change can restore a column definition without restoring its former values. Dropping a column normally removes that column’s data while leaving the table’s rows.

Digests help find differences when both sides use the same deterministic representation. Diff rows to investigate a mismatch. A rollback that re-adds a column with a default passes every structural check. Row-level comparison shows the original values are gone.

Idempotency deserves its own beat. Run the forward statement twice on the copy. The second run should no-op or fail loudly, never half-apply.

Partial failure is the realistic disaster. A batch backfill that stops midway leaves mixed state, and the rollback was written for the finished case. Rehearse with the interruption, not around it.

Copies carry risk too. Personal or regulated data in a scratch instance needs the same masking as a backup. The rehearsal is not a licence to widen access.

Tradeoffs and limits

Budget time for the rehearsal and for verifying recovery from backups when no data-preserving inverse exists. PostgreSQL backup and recovery options That trade is poor for tiny additive changes, though the verdict line still documents intent.

External side effects escape the play entirely. Emails sent, webhooks fired, and payments captured cannot be un-sent by SQL. Treat those changes as forward-fix-only by default.

Append-only event stores are another exclusion. Rewriting history to undo an append is usually worse than appending a correction.

The play also assumes the copy resembles production. Missing extensions, different engine minor versions, or absent row volume weaken every observation. Record those gaps on the page rather than ignoring them.

Test

Use two controls in disposable data. First insert a uniquely identified fixture row and undo precisely that insert; the starting schema and data should be restored. Then remove a fixture column containing two different non-default values and add it back with a single default. Counts can match while the values differ. That inverse is insufficient. Classify the change as blocked until an acceptable forward fix or tested recovery plan is available. A backup restores a point in time; account for subsequent legitimate writes before using one.

Three further checks need no execution. Confirm the verdict vocabulary stays closed at three values. Confirm the rehearser field cannot be filled by the author. Confirm the page demands a rollback statement rather than a rollback intention.

Scope limits are testable too. Present a change that fires an outbound webhook and check whether the play refuses it. A play that scores such a change reversible has lost its boundary.

This article was created with AI assistance.

原文:https://www.a8q9.cn/en/article/rehearse-the-rollback-before-you-merge-an-agent-migration-bae882.html

相关文章