Skip to content
Agent loop · Data

Migration Round Trip

Apply a database migration to a scratch database, roll it back, apply it again, and compare schemas until the migration is clean in both directions.

Up to 6 iterationsStop rules and safety limits
The prompt

Copy the Migration Round Trip loop

Stops when up, down, and up again all succeed and the schema matches after each round trip, or after 6 iterations. Edit the settings block, then paste the whole prompt into your agent.

migration-round-trip.prompt.md
You are running the "Migration Round Trip" loop.

Settings (edit before running):
- Migration to test: <path to the new migration>
- Scratch database URL: postgres://localhost:5432/migration_loop_scratch
- Migrate up command: <for example, npx drizzle-kit migrate or alembic upgrade head>
- Migrate down command: <for example, alembic downgrade -1>
- Schema dump command: pg_dump --schema-only --no-owner
- Max iterations: 6

Goal: the migration applies, rolls back, and re-applies cleanly, and the schema is identical at matching points.

Each iteration:
1. Drop and recreate the scratch database. Migrate it to the version just before the migration under test. Seed a few representative rows in affected tables.
2. Dump the schema (snapshot A). Apply the migration. Dump the schema (snapshot B).
3. Run the down migration. Dump and diff against snapshot A. Any difference is a rollback bug.
4. Apply the migration again. Dump and diff against snapshot B. Any difference or error means the migration is not repeatable.
5. Check that the seeded rows survived in a sensible form.
6. Fix the migration and log what failed.

Stop when:
- All steps succeed and both diffs are empty. Success.
- You reach the max iteration count.
- The change cannot be reversed without data loss (for example dropping a column). Report it and suggest a safer pattern such as expand and contract.

Rules:
- Only connect to the scratch database URL above. Never run migrations against development, staging, or production databases.
- Do not edit migrations that have already been applied elsewhere. Only the migration under test.
- Avoid long table locks: flag any operation that rewrites a large table.

Final report: final migration, the up and down diffs, data checks, and lock or data loss risks.
When to use it

When this loop fits

  • You wrote a migration and want proof that its rollback really restores the old schema.
  • A migration failed halfway in staging and you need it to be safe to re-run.
  • You review migrations from other people and want an automatic check before approving.
How it works

What happens on each pass

The agent repeats these steps until the stop condition holds or it runs out of iterations.

  1. Prepare a scratch database. Create a disposable database and migrate it to the state before the new migration.
  2. Snapshot and apply. Dump the schema, apply the migration, and dump the schema again.
  3. Roll back and compare. Run the down migration and diff the schema against the first snapshot.
  4. Apply again. Re-apply the migration and diff against the second snapshot.
  5. Fix and repeat. Fix the migration until every step succeeds and both diffs are empty.
Recommended servers

MCP servers for this loop

The 6 strongest of 1347 matching servers, ranked by how closely they fit and then by trust score.

More in Databases
  • Trust score 77 of 100, Good

    DBHub

    by bytebase · TypeScript

    Token-efficient database MCP server for PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 25 days ago.
    3.7K stars—/wk weekly downloads— tools
  • Trust score 70 of 100, Good

    dbmcp

    by haymon-ai · Rust

    Database MCP server for MySQL, MariaDB, PostgreSQL & SQLite with PII redaction and write-prevention

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 21 days ago.
    33 stars—/wk weekly downloads— tools
  • Trust score 65 of 100, Good

    ohmyho.st

    by amerged-org · TypeScript

    Operate ohmyho.st hosting from your coding agent: deploy GitHub apps, Postgres, mail, domains.

    Registry
    LicenseQualityMaintenanceLicense: A. Apache-2.0 is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 6 days ago.
    0 stars—/wk weekly downloads— tools
  • Trust score 65 of 100, Good

    pgmoat

    by kitalon-labs-llc · TypeScript

    Governed, read-only Postgres access for AI agents: SQL safety, RLS, budgets, tamper-evident audit.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 7 days ago.
    0 stars—/wk weekly downloads— tools
  • Doxa MCP logo
    Trust score 63 of 100, Good

    Doxa MCP

    by the-doxa-way · JavaScript

    Record and hold prophetic encouragement for decades. Doxa's Bible (BSB) and encouragement tools.

    RegistryRemote
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 12 days ago.
    1 stars—/wk weekly downloads— tools
  • Supabase logo
    Trust score 63 of 100, Good

    MCP server for interacting with the Supabase platform

    OfficialRegistryRemote
    LicenseQualityMaintenanceLicense: Not graded. No license is recorded for this server.Quality: Not graded. No README or tool list has been captured yet.Maintenance: Not graded. No commit or release date has been recorded.
    — stars—/wk weekly downloads— tools
Run it

How to run this loop

  1. Connect the tools. Install a server the loop can use, for example DBHub, or rely on your agent's built-in terminal and file access.
  2. Edit the settings block. Replace the example commands, targets, and limits with your project's own. Keep the iteration cap low on the first run.
  3. Paste it into an agent session. Start a fresh session in Claude Code, Cursor, or VS Code agent mode, in the repository root, and paste the prompt.
  4. Review before you merge. Read the final report and the diff. The loop leaves its work for you to review rather than shipping it.
FAQ

Migration Round Trip questions

When does the Migration Round Trip loop stop?

It stops as soon as up, down, and up again all succeed and the schema matches after each round trip. It also stops after 6 iterations by default, or earlier when the prompt's own stop rules say it is stuck, and then reports what changed and what is left. Change the cap in the settings block at the top of the prompt.

What if my migration tool does not support down migrations?

Skip the rollback steps and keep the repeatability check: apply the migration to a fresh copy twice in a row, from scratch each time, and compare schemas. It still catches non-deterministic migrations and partial failures.

Why does the loop insist on a scratch database?

The loop drops and recreates the database on every pass. That is exactly what you want for a reliable test and exactly what must never happen to a shared one. A disposable local or container database keeps the loop fast and harmless.

Keep exploring
All loops