# Plan a Database Migration with an AI Agent: Rollback Checks

> Canonical: https://skillalmanac.com/learn/plan-a-database-migration-with-an-ai-agent | Updated: 2026-11-21 | Index status: not offered to search engines

Steps for an AI agent to plan a database migration with a tagged rollback point, reversible changesets, row-count checks and safe PostgreSQL locking.

## Facts

- 2026-11-21: Liquibase rollback reverts deployed changesets newest first and stops at a tag; it fails with a 'Could not find tag' error when the tag is missing (https://docs.liquibase.com/concepts/changelogs/rollback.html)
- 2026-11-21: Liquibase rollback of DROP, DELETE or TRUNCATE recreates the objects but does not restore the original data (https://docs.liquibase.com/concepts/changelogs/rollback.html)
- 2026-11-21: Liquibase blocks rollback of a changeset whose checksum changed after deployment, so roll back before editing (https://docs.liquibase.com/concepts/changelogs/rollback.html)
- 2026-11-21: Most PostgreSQL ALTER TABLE forms take an ACCESS EXCLUSIVE lock that blocks reads as well as writes (https://www.postgresql.org/docs/current/sql-altertable.html)
- 2026-11-21: PostgreSQL documents ADD ... NOT VALID followed by VALIDATE CONSTRAINT as a way to reduce lock impact; the reference does not document a lock_timeout setting (https://www.postgresql.org/docs/current/sql-altertable.html)

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

1. **Tag the database before any change.** Liquibase rollback stops at a tag, so create one with the `tag` command and confirm it exists with `tag-exists` before the run. Without it, the rollback fails.

2. **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.

3. **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.

4. **Preview the reversal.** Run `rollback-sql` and read the SQL it prints before running `rollback`.

5. **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 run `VALIDATE CONSTRAINT` as 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.

6. **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-version` option, 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 `USING` clause. 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](/skills/skill-almanac/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](/skills/skill-almanac/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

- [sql-and-database-helper](https://skillalmanac.com/skills/skill-almanac/sql-and-database-helper.md): Platform Score 57/100 (provisional)
- [infrastructure-as-code-review](https://skillalmanac.com/skills/skill-almanac/infrastructure-as-code-review.md): Platform Score 45/100 (provisional)

## Related guides

- [Draft a Cloud Migration Plan with AI: Inventory First](https://skillalmanac.com/learn/draft-a-cloud-migration-plan-with-ai.md)
- [Optimize a SQL Query With an AI Agent: Start From the Plan](https://skillalmanac.com/learn/optimize-a-sql-query-with-an-ai-agent.md)
- [Format SQL with an AI agent without changing its meaning](https://skillalmanac.com/learn/format-sql-with-an-ai-agent.md)
- [Convert JSON to CSV: Nested Data, Nulls and Checks](https://skillalmanac.com/learn/convert-json-to-csv-with-an-ai-agent.md)

## Sources

- https://docs.liquibase.com/concepts/changelogs/rollback.html
- https://www.postgresql.org/docs/current/sql-altertable.html
- https://www.postgresql.org/docs/current/ddl-alter.html

Platform Scores report our assessment under a published method. They are not a safety guarantee.
