Back to Library
code database migration sql

Database Migration Planner

P
PromptAura Team
Nov 4, 2026
0 Likes
prompt-content.md
#TASK
Generate a reversible SQL migration script that creates a new table, adds columns, or modifies schema while preserving data.

#REQUEST
You will receive a description of the desired schema change (e.g., "add column email VARCHAR(255) NOT NULL to users table"). Produce the SQL to apply the change and the SQL to roll it back.

#ACTION
1. Analyze the change request for type (create, alter, drop).
2. Generate forward SQL using appropriate syntax for the target database.
3. Generate reverse SQL that exactly undoes the forward change.
4. Wrap both in transactions if supported.
5. Add comments explaining each step.
6. Include safety checks: IF NOT EXISTS for creates, IF EXISTS for drops.

#CONTEXT
Target database is PostgreSQL (but syntax can be adapted for MySQL/SQLite). Assume existing tables may have data; avoid destructive operations without explicit consent.

#EXAMPLE
Request: "Add column email VARCHAR(255) UNIQUE NOT NULL to users table"
Forward:
BEGIN;
ALTER TABLE users ADD COLUMN IF NOT EXISTS email VARCHAR(255);
UPDATE users SET email = 'temp_' || id || '@example.com' WHERE email IS NULL;
ALTER TABLE users ALTER COLUMN email SET NOT NULL;
ALTER TABLE users ADD CONSTRAINT users_email_unique UNIQUE (email);
COMMIT;
Reverse:
BEGIN;
ALTER TABLE users DROP CONSTRAINT IF EXISTS users_email_unique;
ALTER TABLE users DROP COLUMN IF EXISTS email;
COMMIT;

Actions

Opening in Studio allows you to fill variables, use AI optimization, and save to your personal library.