A safe plan for a risky database change with Claude

The struggle: You need to change a table that live customers depend on, and one wrong migration could lock the app or lose data.

What you end up with: A reviewed migration plan with staged steps, a rehearsal on a copy and a rollback you have tested.

Claude Code finds everything that touches the table, then helps you split the change into small backward-compatible steps. You rehearse on a copy, and a person runs the real migration.

Tools: Claude. Level: Pro. Time: 2 hours for the plan.

Use it if

  • You are renaming, splitting or dropping a column in use
  • Several services read the same table
  • You fear a long lock or lost data

Skip it if

  • You would run the generated migration on production without rehearsal
  • The change is a new empty table with no readers

What you need first

  • Claude Pro or higher
  • Claude Code installed with the repo cloned
  • A copy of your database to rehearse on

The method

  1. Start read-only (Claude: Claude Code)

    In a clone, run claude --permission-mode plan, or press Shift+Tab until plan mode on shows. It reads and plans but makes no edits. Do not connect it to a production database.

    Prompt to copy:

    List the schema and migration files in this repo and the database engine and version they imply. Make no edits and do not connect to any database. Tell me what you cannot determine.

    You get: A read-only session with no database access.

    Tip: Give it the schema file and migrations from the repo, not a live connection.

  2. Find every reader and writer (Claude: Claude Code)

    Ask Claude to search the repo for each place that uses the table or column.

    Prompt to copy:

    The change: [describe, for example rename users.phone to users.phone_number]. Search this repo for every use of the table and column: models, queries, migrations, jobs, scripts, tests, reports. List file and line and say read or write. List what you could not search (other repos, BI tools).

    You get: A list of code paths, jobs, reports and queries that touch it, by file.

    Tip: Also search scripts, cron jobs and analytics queries, not just the app.

  3. Split it into safe steps (Claude: Claude Code)

    Ask for a plan where old and new code both work during the change, such as add, backfill, switch, then remove.

    Prompt to copy:

    Write a step-by-step migration plan that keeps the app working at every step (for example add new column, write to both, backfill in batches, switch reads, remove old column later). For each step give: SQL or code change, locking risk, how to verify, how to roll back. State the assumptions about my database and engine: [engine and version].

    You get: A numbered plan, each step deployable alone.

    Tip: Never drop or rename in the same release that switches the code.

  4. Pressure-test the plan (Claude: Chat)

    Paste the plan into a Claude chat and ask what could go wrong: locks, long backfills, replicas, failing deploys.

    Prompt to copy:

    Review this database migration plan as a sceptical senior engineer. List how each step could fail (locks, long-running backfills, replication lag, partial deploys, data loss), what to monitor, and what is missing. Do not invent facts about my system; ask me when you need them. Plan: [paste]

    You get: A list of failure cases and checks to add.

    Tip: Ask what happens if the deploy halts after step two.

  5. Rehearse on a copy, then a person runs it (Claude: Chat)

    Restore a backup to a copy or staging, and run every step there yourself. Time it and run the rollback. Only then does a person on your team run the real migration, with a fresh verified backup in place.

    Prompt to copy:

    Write a rehearsal checklist for this plan: restore a backup to a copy, run each step with timings, verify data matches before and after, run the rollback, and record the results. Add a go or no-go list for the real run, owned by a named role.

    You get: A timed rehearsal and a rollback that worked.

    Tip: If the rollback has never run, you do not have one.

Where this goes wrong

  • Take a backup and prove you can restore it before anything runs. Have a tested rollback written down before the real run.
  • Run every step on staging or a copy first. A human runs every command and every migration, never Claude. Do not let Claude Code run SQL or connect to a database.
  • Claude does not know your data volume or engine settings. Rehearse on a copy of real size before trusting the timing. Review every generated SQL line before it is committed.
  • Keep production credentials and data out of the session and out of prompts.

Sources

More like this

Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.