Automated Database Migration Safety Review for Claude Code

You get a structured risk assessment of your SQL migration scripts to prevent production downtime, data loss, and long-running table locks.

Agent prompt for Claude

Prompt

Prompt
Act as a Senior Database Reliability Engineer. Your job is to review a provided SQL migration script for production safety before execution. Before analyzing, ask these 3 questions: 1. Which database engine (e.g., PostgreSQL, MySQL 8.0) are you using? 2. What is the approximate record count of the target table? 3. Is the migration intended for a zero-downtime deployment? After the user answers, provide a risk assessment. Rules: 1. Identify specific commands that trigger table locks or full table rewrites. 2. Flag missing backward compatibility for existing application code. 3. Suggest batch sizes for data backfills to avoid transaction log exhaustion. 4. Propose a rollback strategy for each change. Output format: ## Migration Risk Assessment | Risk Level | SQL Statement | Potential Impact | Mitigation Strategy | ## Critical Hazards | Bulleted list of locking risks ## Rollback Plan | Step-by-step recovery commands. This is not professional database administration advice; consult your lead DBA or infrastructure architect before running destructive migrations on production systems.

Example output

Migration risks and how to mitigate themPostgreSQL 15 migration on a 50-million row users table
ItemRiskMitigation
ALTER TABLE ADD COLUMN NOT NULL (High)Exclusive lock blocks all reads/writesAdd nullable, backfill, then set NOT NULL
UPDATE users SET status (Medium)Log bloat and long-running locksBatch 5000 rows with sleep intervals

Example for: a PostgreSQL 15 migration adding a non-nullable column to a 50-million row users table.

Migration Risk Assessment

Risk LevelSQL StatementPotential ImpactMitigation Strategy
HighALTER TABLE ADD COLUMN ... NOT NULLExclusive table lock blocking all reads/writesAdd column as nullable first, backfill data, then set NOT NULL constraint.
MediumUPDATE users SET status = 'active'Transaction log bloat and long-running locksPerform update in batches of 5000 rows with sleep intervals.

Critical Hazards

  • The ALTER TABLE command will lock the table for the duration of the column addition, causing an application outage.
  • Adding a NOT NULL constraint without a default value on a large existing table requires a full sequential scan.
  • Large UPDATE statements will hold row-level locks that prevent concurrent authentication attempts.

Rollback Plan

  1. If the migration fails, verify the lock queue using SELECT pg_blocking_pids(pid).
  2. Drop the newly added column: ALTER TABLE users DROP COLUMN IF EXISTS status_new;
  3. Restore original application connection pool settings if they were modified for the migration.
  4. Clear any orphaned temporary tables created during the backfill process using DROP TABLE IF EXISTS temp_backfill_migration_001;.
Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces a risk-first perspective by requiring engine and scale context.
  • Categorizes findings into actionable tables and clear hazard lists.
  • Mandates a rollback strategy to ensure recovery paths exist before execution.

Use this in Claude Code or the Claude web interface when preparing database migrations. Paste your raw SQL script immediately after the prompt sends its questions. It works best for SQL files targeting production environments where downtime is unacceptable.

Common edits

  • Adjust the batch size recommendations if your specific database has tight transaction log limits.
  • Add specific compliance checks if your organization requires audit logging for all schema changes.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

More in Claude Code
Coding

Debug SQL query errors and incorrect row counts

Find syntax errors, incorrect joins, and silent null filter bugs in database queries. Use it when a query returns syntax errors or unexpected row counts.

Coding

Design Cursor-Based Pagination Logic for High-Volume APIs

Get a complete implementation plan for cursor-based pagination including SQL queries, cursor encoding logic, and strategies for handling record deletions.

Coding

Find and Fix Data Quality Issues with Ready-to-Run SQL Checks

Paste your schema and get SQL queries that catch duplicates, NULLs, orphaned foreign keys and out-of-range values, each with what a bad result means and a fix.

Critical Thinking & Decisions

Design a clean rollback plan for risky launches

Establish unambiguous abort triggers, rollback sequences, and data safeguards before shipping. Designed for engineering managers and product leads.

Audit Error Boundaries and Telemetry Coverage in Code

This prompt audits your codebase for missing error boundaries, unhandled promise rejections, and incomplete telemetry traces to ensure production stability.