Generate database migration scripts for schema changes with rollback support and data preservation strategies.
Copy & paste the prompt below into your preferred LLM. Unless a specific AI model is mentioned, you can use whichever you prefer.
You are a database architect. Generate safe, tested migration scripts for my schema change. Migration details: - Database: [POSTGRESQL / MYSQL / SQLITE / MONGODB / OTHER] - ORM/Framework: [PRISMA / SEQUELIZE / TYPEORM / DJANGO / RAILS / KNEX / RAW SQL / OTHER] - Current schema: [DESCRIBE CURRENT TABLE STRUCTURE OR PASTE SCHEMA] - Desired change: [WHAT DO YOU WANT TO CHANGE? ADD TABLE, ADD COLUMN, RENAME, MODIFY TYPE, etc.] - Data in table: [EMPTY / SMALL / LARGE (MILLIONS OF ROWS) / PRODUCTION DATA] - Downtime tolerance: [ZERO DOWNTIME / SHORT MAINTENANCE WINDOW / FLEXIBLE] Deliver: 1. **Up Migration:** The complete migration script to apply the change 2. **Down Migration:** The complete rollback script to undo the change 3. **Data Migration:** If existing data needs transformation, include the data migration step 4. **Safety Checks:** - Pre-migration validation queries (verify assumptions about current state) - Post-migration verification queries (confirm the migration worked) - Row count checks before and after 5. **Zero-Downtime Strategy:** If this involves a large table, provide the multi-step approach (add column → backfill → add constraints → drop old) 6. **Testing Plan:** How to test this migration on a copy of production data 7. **Backup Command:** The exact command to backup the affected table(s) before migrating 8. **Estimated Impact:** Lock duration, table size impact, index rebuild time for large tables 9. **Edge Cases:** What happens if the migration fails halfway? How to recover? Never migrate without a rollback plan. Never run on production without testing on a copy first.
The effectiveness of "Migration Script Generator" comes from its well-structured structure. By incorporating sequential task breakdown and output formatting and negative constraints, it channels the AI's capabilities toward producing exactly what you need. This means you get production-quality code that handles edge cases and follows your stack conventions without the trial-and-error that wastes most people's time with AI.
These coding tips will help you get stronger results when using "Migration Script Generator" and similar prompts in this category.
"Migration Script Generator" is particularly useful in these situations. If any of these scenarios sound familiar, this prompt will save you significant time.
When you use "Migration Script Generator" with ChatGPT, Claude, or Gemini, here is what to expect in the AI output.
Adapt "Migration Script Generator" to your specific situation by modifying these key areas. The more context you add, the better the results.