Complete AI Training

Prompt

Draft Database Migration Script

Use this when you need to add or change tables safely in a migration tool.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

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

  1. Ask for any missing inputs from the list above, then confirm the migration tool and database engine.
  2. Analyse the current schema and desired change to identify potential locks, data loss, or downtime.
  3. Draft the migration script in the syntax of the specified migration tool, including both up and down migrations.
  4. Add comments explaining each step and any assumptions.
  5. Suggest a safe rollout plan (e.g., backfill, dual-write, or maintenance window).
  6. 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.