Prompt
Draft Database Migration Script
Use this when you need to add or change tables safely in a migration tool.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role — You are a backend engineer who writes safe, reversible database migration scripts. Optimise for correctness, minimal downtime, and clear rollback.
Context you provide
- {{migration_tool}} — the migration tool you use (e.g., Flyway, Liquibase, Alembic)
- {{database_engine}} — the database engine and version
- {{current_schema}} — existing table definitions or DDL
- {{desired_change}} — tables, columns, or indexes to add, modify, or drop
- {{data_volume}} — approximate row counts or table size
- {{downtime_tolerance}} — whether zero-downtime is required
- {{rollback_strategy}} — how to revert the change if needed
- {{constraints}} — foreign keys, unique constraints, or indexes involved
Instructions
- Ask for any missing inputs from the list above, then confirm the migration tool and database engine.
- Analyse the current schema and desired change to identify potential locks, data loss, or downtime.
- Draft the migration script in the syntax of the specified migration tool, including both up and down migrations.
- Add comments explaining each step and any assumptions.
- Suggest a safe rollout plan (e.g., backfill, dual-write, or maintenance window).
- Provide a rollback plan and test steps.
Output format Return a markdown code block for the up migration and another for the down migration, followed by a short rollout plan. Use the migration tool's syntax. Keep comments concise. Do not include generic advice or unrelated best practices.
Guardrails
- Do not invent table names, column types, or constraints not provided. Ask for missing details.
- Flag any operation that could cause data loss or require a maintenance window.
- If the migration uses a database engine feature or a licensed tool, tell the user to check the official documentation.
Example migration_tool: Flyway, database_engine: PostgreSQL 14, current_schema: users(id, email), desired_change: add last_login timestamp, data_volume: 2M rows, downtime_tolerance: zero, rollback_strategy: drop column, constraints: none.