Skip to content

LXGIC-Studios/ai-migrate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ai-migrate

npm version npm downloads License: MIT

Generate database migration SQL from schema diffs. Compare SQL schema files and produce ALTER TABLE statements automatically.

What it does

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

Install

npx @lxgicstudios/ai-migrate

Or install globally:

npm install -g @lxgicstudios/ai-migrate

Usage

# 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

Options

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

Example

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();

Supported Changes

  • ADD COLUMN
  • DROP COLUMN
  • ALTER COLUMN type
  • ADD/DROP INDEX
  • ADD/DROP CONSTRAINT
  • RENAME COLUMN (when detectable)

FAQ

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.

License

MIT


Built by LXGIC Studios

🔗 GitHub · Twitter

💡 Want more free tools like this? We have 100+ on our GitHub: github.com/lxgicstudios

About

Generate database migration SQL from schema diffs. Compare SQL schema files and produce ALTER TABLE statements for column changes, type changes, and index updates. Supports PostgreSQL, MySQL, SQLite. npx ai-migrate

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors