Plan a Database Migration with an AI Agent: Rollback Checks
Steps for an AI agent to plan a database migration with a tagged rollback point, reversible changesets, row-count checks and safe PostgreSQL locking.
Updated · Markdown version
An AI agent can run a database migration safely only when the run includes a tagged rollback point, a reversal for every change, and row counts taken before and after each step. The steps below cover the checks agents most often skip, using the behavior documented for Liquibase and PostgreSQL as of 2026-11-21.
Steps
- Tag the database before any change. Liquibase rollback stops at a tag, so create one with the
tagcommand and confirm it exists withtag-existsbefore the run. Without it, the rollback fails.
- Write a rollback for every changeset. Modeled changelogs (XML, YAML, JSON) generate rollback SQL for many change types. Formatted SQL changelogs need every rollback statement written by hand. Rollback runs the newest changeset first, so the agent should check the order of the changelog before it runs anything. If a changeset should have no reversal, mark it explicitly as not required instead of leaving the rollback empty by accident.
- Record row counts per affected table. Run
SELECT count(*)on each table the migration touches, before and after the change, and store the numbers next to the tag name. This check does not depend on the migration tool, so the agent can run it in any session.
- Preview the reversal. Run
rollback-sqland read the SQL it prints before runningrollback.
- Reduce lock impact on PostgreSQL. Most ALTER TABLE forms hold an ACCESS EXCLUSIVE lock, so reads wait too. To add a foreign key on a large table, add it with
NOT VALID, then runVALIDATE CONSTRAINTas a separate statement. The second step checks existing rows under a weaker lock. The reference does not describe a lock timeout setting, so confirm any timeout in the configuration docs for your PostgreSQL version.
- Export destructive data first. A rollback of a DROP, DELETE or TRUNCATE recreates the objects but not the rows, so export those tables before the run.
Check that it worked: the rollback-sql output matches the plan, the post-migration row counts match the expected change, and a rehearsal rollback on a copy returns the original counts.
Pitfalls
- Rollback blocked by a checksum error. You edited a changeset after it was deployed. Notice it from the checksum message in the rollback output. Fix it by rolling back first, then editing the changeset.
- Ambiguous tag. A tag that appears more than once makes the stop point depend on the
tag-versionoption, which defaults to OLDEST. Fix it by giving each release a unique tag name. - Long ADD COLUMN. PostgreSQL computes a volatile default for every row, while a constant default avoids the rewrite. Notice it from a long-running statement. Fix it by adding the column with a constant default and backfilling in batches.
- Type change fails mid-run. The conversion succeeds only when each value casts implicitly; otherwise supply a
USINGclause. Notice it from the cast error. Fix it by querying for values that will not convert and counting them before the change. - Counts that do not match. The before and after counts used different filters or different tables. Fix it by running the same query both times and saving the exact SQL with the tag.
When to use a skill instead
Ad-hoc prompting is enough for a one-off migration on a small table. A reusable skill makes sense when the same review must happen on every migration. sql-and-database-helper writes, reviews and migrates SQL for PostgreSQL, MySQL and SQLite, and it works read-only unless asked otherwise, which suits reviewing the migration SQL before the agent runs it. infrastructure-as-code-review reviews Terraform, CloudFormation and Kubernetes files and plans, so it applies only when the same migration also changes infrastructure. The tag, rollback-preview and row-count steps above depend on your migration tool, so no bundled skill replaces them.
FAQ
Can a rollback restore rows that a migration deleted?
No. Liquibase rollback recreates dropped, deleted or truncated objects but does not restore the original data, so export the rows before the run.
Why does my rollback fail with a checksum error?
The changeset was edited after it was deployed, so its checksum no longer matches. Roll it back first, then make the edit.
How do I add a foreign key without scanning the whole table under a strong lock?
Add it with NOT VALID, which skips the full-table scan, then run VALIDATE CONSTRAINT in a separate statement.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
Sources
- https://docs.liquibase.com/concepts/changelogs/rollback.html (accessed 2026-11-21)
- https://www.postgresql.org/docs/current/sql-altertable.html (accessed 2026-11-21)
- https://www.postgresql.org/docs/current/ddl-alter.html (accessed 2026-11-21)
This page is not offered to search engines yet: The content file does not mark this page indexable yet.