Plan Zero-Downtime Database Migration and Backfill

Get a step-by-step technical plan for migrating database schemas without downtime, including data backfill logic and verification steps.

Agent prompt for ChatGPT and Claude

Prompt

Prompt
Act as a Senior Database Reliability Engineer. I need to perform a breaking schema migration on a production SQL database. Before providing the plan, please ask me these 3 questions: 1. What is the current schema change you need to implement? 2. What is the approximate size of the table in rows? 3. Is the database using Postgres, MySQL, or another engine? Once I answer, provide a migration strategy divided into 5 phases: Dual-Write, Backfill, Read-Switch, Cleanup, and Verification. Rules: 1. Every phase must include a specific SQL verification query. 2. Ensure all steps are backward-compatible. 3. Use standard SQL syntax. 4. Do not include placeholders. Output format: ## Phase Name - Goal: One sentence. - Action: Bulleted technical steps. - Verification: The exact SQL query to confirm success. This is not legal or financial advice; consult with a database architect for high-stakes production changes.

Example output

Example for: migrating a user_email column to an auth_identities table in a 50 million row Postgres database. ## Phase 1: Dual-Write - Goal: Ensure new records are written to both old and new locations. - Action: Update application code to write to both the existing user_email column and the new auth_identities table. - Verification: SELECT count() FROM auth_identities WHERE created_at > NOW() - INTERVAL '1 minute'; ## Phase 2: Backfill - Goal: Migrate legacy data without locking tables. - Action: Run a background worker to copy rows in batches of 1000 with a 100ms sleep between iterations. - Verification: SELECT count() FROM users WHERE id NOT IN (SELECT user_id FROM auth_identities); ## Phase 3: Read-Switch - Goal: Direct traffic to read from the new schema. - Action: Update application logic to prioritize auth_identities for email lookups. - Verification: SELECT count() FROM auth_identities WHERE email IS NULL; ## Phase 4: Cleanup - Goal: Remove obsolete dependencies. - Action: Stop writing to the old user_email column and drop the column after 48 hours of monitoring. - Verification: SELECT column_name FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'user_email'; ## Phase 5: Verification - Goal: Confirm parity. - Action: Perform a checksum comparison between the old column and new table data for a random sample of 1000 users. - Verification: SELECT count() FROM users u LEFT JOIN auth_identities a ON u.id = a.user_id WHERE u.email != a.email;

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces a structured five-phase approach that prevents common production outages.
  • Requires specific SQL verification queries to ensure data integrity at each step.
  • Ensures backward compatibility by mandating a dual-write phase before cutting over.

Use this prompt when you need to safely alter production database tables without service interruption. It works well in ChatGPT, Claude, or GitHub Copilot Chat for planning migrations. You must have your current schema and table size ready to answer the initial questions.

Common edits

  • Adjust the batch size in the backfill phase based on your database write throughput limits.
  • Modify the verification queries to match your specific database engine syntax if it differs from Postgres.

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?

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.

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

Fix N+1 Database Query Patterns in ORM Code

This prompt identifies N+1 query issues in your code, provides the optimized eager-loading syntax, and calculates the reduction in database round-trips.

Data AnalysisClaude

Spec and Build a Weekly KPI Dashboard in Claude Data

Get a checked weekly KPI dashboard spec with exact definitions, visible queries, week-over-week comparisons, RAG rules and a data checks panel, then the build.