Generate database migration SQL from schema diffs. Compare SQL schema files and produce ALTER TABLE statements automatically.
Point it at two SQL schema files (old vs new), and it spits out the migration SQL you need. No more writing ALTER TABLE statements by hand.
- Detects column additions, removals, and type changes
- Generates ALTER TABLE, ADD COLUMN, DROP COLUMN statements
- Supports PostgreSQL, MySQL, and SQLite syntax
- Produces rollback migrations automatically
- Zero external dependencies
npx @lxgicstudios/ai-migrateOr install globally:
npm install -g @lxgicstudios/ai-migrate# Compare two schema files
npx @lxgicstudios/ai-migrate --from schema-old.sql --to schema-new.sql
# Output to file
npx @lxgicstudios/ai-migrate --from old.sql --to new.sql --output migration.sql
# Generate rollback too
npx @lxgicstudios/ai-migrate --from old.sql --to new.sql --rollback
# Specify dialect
npx @lxgicstudios/ai-migrate --from old.sql --to new.sql --dialect postgres| Option | Description | Default |
|---|---|---|
--from |
Old schema file | Required |
--to |
New schema file | Required |
--output |
Output migration file | stdout |
--rollback |
Generate rollback migration | false |
--dialect |
SQL dialect (postgres, mysql, sqlite) | postgres |
--dry-run |
Preview without writing | false |
Old schema (v1.sql):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL
);New schema (v2.sql):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL,
name VARCHAR(100),
created_at TIMESTAMP DEFAULT NOW()
);Output:
-- Migration: v1 -> v2
ALTER TABLE users ADD COLUMN name VARCHAR(100);
ALTER TABLE users ADD COLUMN created_at TIMESTAMP DEFAULT NOW();- ADD COLUMN
- DROP COLUMN
- ALTER COLUMN type
- ADD/DROP INDEX
- ADD/DROP CONSTRAINT
- RENAME COLUMN (when detectable)
Does it handle foreign keys? Yes. It detects FK constraints and orders DROP/ADD statements correctly to avoid constraint violations.
Can I use it in CI/CD?
Absolutely. Use --dry-run to validate schema changes, or pipe output directly to your migration runner.
What about data migrations? This tool handles schema DDL only. For data migrations (UPDATE statements, backfills), you'll need custom scripts.
MIT
Built by LXGIC Studios
💡 Want more free tools like this? We have 100+ on our GitHub: github.com/lxgicstudios