⌨️ Codingintermediatedatabasemigrationpostgresqlschema

Write a Database Migration

Generate a safe, reversible database migration for schema changes with up and down functions.

The Prompt

prompt.txt
Write a database migration for the following schema change. Include:
1. An `up` function with the forward migration
2. A `down` function to reverse it completely
3. Safe handling — avoid data loss where possible
4. Appropriate transaction wrapping
5. Comments explaining any irreversible steps

Migration framework: [PRISMA / KNEX / ALEMBIC / FLYWAY — specify]
Database: [POSTGRESQL / MYSQL / SQLITE]

Change to make:
[DESCRIBE THE SCHEMA CHANGE — e.g., Add a non-null `role` column to the users table with default 'member', split the `name` column into `first_name` and `last_name`, and add a partial index on email WHERE is_active = true]

Example Output

Generated a Knex migration that adds the role column with a DEFAULT in the same transaction to avoid lock issues on large tables, splits the name field with a data migration step before dropping the original, and safely rolls back by restoring the concatenated name if down() is called.

FAQ

Which AI model is best for Write a Database Migration?

Claude Sonnet 4 — careful about data-safe migrations and reversibility. GPT-4o also works well.

How do I use the Write a Database Migration prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Generated a Knex migration that adds the role column with a DEFAULT in the same transaction to avoid lock issues on large tables, splits the name field with a data migration step before dropping the original, and safely rolls back by restoring the concatenated name if down() is called.