Universal tool for automatic database structure comparison and SQL migration script generation. Works with any environment and database.
- Automatic database detection - scans folder and matches dev/sit/prod pairs
- Universal configuration - works for any environments and naming conventions
- CREATE TABLE generation - automatically creates missing table definitions
- Intelligent comparison - detects differences in columns, types, indexes
- Flexible configuration - full control over migration process
- Clear reports - detailed SQL comments
npm installFirst, export structures from both environments:
# Edit export-dbs.js and configure connections
# Then run:
node export-dbs.jsThis creates files in export/ folder:
export/
├── app_dev_database1.md
├── app_sit_database1.md
├── app_dev_database2.md
└── app_sit_database2.md
node compare-and-migrate.jsThe script automatically:
- Detects all database pairs
- Compares their structures
- Generates SQL scripts in
migration/folder
# Review generated files
ls migration/
# Execute migration
mysql -h <host> -u <user> -p < migration/migrate_all_dev_to_sit.sqlconst CONFIG = {
// Folders
exportDir: path.join(__dirname, "export"),
outputDir: path.join(__dirname, "migration"),
// File pattern: <prefix>_<env>_<dbname>.md
filePattern: /^(.+?)_([a-zA-Z0-9]+)_(.+)\.md$/,
// Migration direction
sourceEnvironment: "dev", // Source
targetEnvironment: "sit", // Target
// Options
normalizeTypeSizes: true, // bigint(20) === bigint
generateCreateTable: true, // Generate CREATE TABLE
warnExtraColumns: true, // Warn about extra columns
caseInsensitiveDbNames: true // ECF === ecf
};Default pattern: <prefix>_<environment>_<database>.md
Valid examples:
app_dev_users.md+app_sit_users.mdmyapp_prod_payment.md+myapp_dev_payment.mdproject_local_db1.md+project_staging_db1.md
Invalid examples:
dev-users.md(missing separator _)users_dev.md(wrong order, missing prefix)dev_users.txt(wrong extension, must be .md)
- Missing tables - generates CREATE TABLE
- Missing columns - generates ALTER TABLE ADD COLUMN
- Type differences - generates ALTER TABLE MODIFY COLUMN
- Nullable differences - generates ALTER TABLE MODIFY COLUMN
- Missing indexes - generates ALTER TABLE ADD INDEX/UNIQUE
- Extra tables - comment with warning
- Extra columns - comment with warning
-- Migration for database: users
-- From dev to sit
USE `users`;
-- Creating table 'activation_link' from dev
CREATE TABLE `activation_link` (
`id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_token` (`token`)
);
-- Table: user
-- Added missing column 'email' from dev
ALTER TABLE `user` ADD COLUMN `email` varchar(255) NULL;
-- Changed column type 'phone' from varchar(20) to varchar(255)
ALTER TABLE `user` MODIFY COLUMN `phone` varchar(255) NULL;
-- Added missing unique index 'UK_email'
ALTER TABLE `user` ADD UNIQUE INDEX `UK_email` (`email`);db_migration/
├── export/ # Database structure exports
│ ├── app_dev_ecf.md
│ ├── app_sit_ecf.md
│ ├── app_dev_payment.md
│ └── app_sit_payment.md
│
├── migration/ # Generated migrations
│ ├── migrate_ecf.sql
│ ├── migrate_payment.sql
│ └── migrate_all_dev_to_sit.sql
│
├── compare-and-migrate.js # Main script
├── export-dbs.js # Structure export script
├── config.example.js # Example configuration
├── package.json
└── README.md
Change in CONFIG:
sourceEnvironment: "sit",
targetEnvironment: "dev",Yes! You can use any names, for example:
sourceEnvironment: "local",
targetEnvironment: "staging",Make sure files in export/ folder use the same names:
app_local_users.mdapp_staging_users.md
NO. The script synchronizes only database STRUCTURE (tables, columns, indexes). It does not transfer records.
Currently the script processes all found pairs. If you want only one database:
- Move other files outside
export/folder - Run the script
- Restore the files
Or use a specific generated file, e.g., migrate_users.sql.
IMPORTANT: Before executing migration:
- Backup - Always backup the target database
- Test on dev - Test migration on test environment
- Check SQL - Review generated SQL scripts
- NOT NULL - NOT NULL columns may require default values
- Production data - Be especially careful with production
For issues:
- Check FAQ above
- Read
migration/README.md(generated automatically) - Contact development team
MIT
Database Migration Tool