{"id":21268782,"url":"https://github.com/zhangmingfeng/easy-migrate","last_synced_at":"2025-08-16T02:05:20.229Z","repository":{"id":57218551,"uuid":"150246261","full_name":"zhangmingfeng/easy-migrate","owner":"zhangmingfeng","description":"数据库migrate工具，可以自动生成迁移语句，目前暂支持MySQL，欢迎Issues和PR","archived":false,"fork":false,"pushed_at":"2019-12-27T05:06:12.000Z","size":40,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-30T03:06:14.836Z","etag":null,"topics":["auto-migrate","migrate","migration","mysql","sequelize"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhangmingfeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-25T10:16:35.000Z","updated_at":"2022-12-09T03:45:56.000Z","dependencies_parsed_at":"2022-08-28T21:41:02.880Z","dependency_job_id":null,"html_url":"https://github.com/zhangmingfeng/easy-migrate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zhangmingfeng/easy-migrate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangmingfeng%2Feasy-migrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangmingfeng%2Feasy-migrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangmingfeng%2Feasy-migrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangmingfeng%2Feasy-migrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhangmingfeng","download_url":"https://codeload.github.com/zhangmingfeng/easy-migrate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangmingfeng%2Feasy-migrate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270657710,"owners_count":24623465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-16T02:00:11.002Z","response_time":91,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-migrate","migrate","migration","mysql","sequelize"],"created_at":"2024-11-21T08:06:21.977Z","updated_at":"2025-08-16T02:05:20.182Z","avatar_url":"https://github.com/zhangmingfeng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# easy-migrate\n\n简单高效的migrate工具，可以根据模型配置文件自动生成迁移语句，开发人员只需要关注配置文件的增删改即可，无需关心迁移的语句\n\n# 特性\n\n- 本工具的模型管理是基于[sequelize](https://github.com/sequelize/sequelize)，如需要手动修改migration文件，请参考它的API\n- 模型配置文件简单，目前只支持MySQL，根据配置文件的变化和数据库当前表结构的对比，自动生成迁移语句，开发人员只需要维护配置文件即可\n- 根据MySQL常用的数据类型，目前只支持MySQL的INT、VARCHAR、DECIMAL、DATETIME、DATE、TEXT、LONGTEXT，其他类型，如FLOAT、DOUBLE,可以使用DECIMAL代替，CHAR使用VARCHAR代替\n- 后续版本会支持更多种数据库，sequelize支持的都可以\n\n# 安装\n\n```bash\n$ npm i easy-migrate -g\n$\n$ migrate\nUsage: migrate [options] [command]\n\nOptions:\n  -V, --version  output the version number\n  -h, --help     output usage information\n\nCommands:\n  config         show migrate config\n  create         Create a new migration\n  up             Migrate up to a give migration version, default lastest\n  down           Migrate down to a given migration version, default current pre-version\n  help [cmd]     display help for [cmd]\n```\n\n# 使用\n## migrate create\n\n```bash\n$ migrate create -h\nUsage: migrate-create [options] \u003cname\u003e\n\nOptions:\n  -V, --version                output the version number\n  --define-path \u003cpath\u003e         Set define file path\n  --db-config-path \u003cpath\u003e      Set db config path\n  --migrations-path \u003cpath\u003e     Set migrations out path\n  --model-path \u003cpath\u003e          Set sequelize model file out path\n  -h, --help                   output usage information\n\n```\n- --define-path: 执行模型配置文件的目录（项目开发过程中一般按模块开发的话，模型也应该是模块级的），可以是一个文件，也可以通过设置环境变量: MIGRATE_DEFINE_PATH\n- --db-config-path: 数据库的配置文件路径，也可以通过设置环境变量: MIGRATE_DB_CONFIG_PATH\n- --migrations-path: 生成的migration文件输出目录，也可以通过设置环境变量: MIGRATE_MIGRATIONS_PATH\n- --model-path: 当项目使用sequelize作为orm工具的时候，model定义文件可以根据配置文件自动生成，配置输出目录即可，也可以通过设置环境变量: MIGRATE_MODEL_PATH\n```bash\n$ migrate create --define-path /path/to/define/somemodule --db-config-path /path/to/db/config --migrations-path /path/to/migrations\nmigration file '20181011164723-migration.js' created!\n```\n或者\n```bash\n$ MIGRATE_DEFINE_PATH=/path/to/define/somemodule MIGRATE_DB_CONFIG_PATH=/path/to/db/config MIGRATE_MIGRATIONS_PATH=/path/to/migrations migrate create\nuse MIGRATE_DB_CONFIG_PATH for db config: /path/to/db/config\nuse MIGRATE_DEFINE_PATH for define path: /path/to/entity/somemodule\nuse MIGRATE_MIGRATIONS_PATH for migrations path: /path/to/migrations\nmigration file '20181011164723-migration.js' created!\n```\n\n- `20181011164723-migration.js`\n\n```js\n'use strict';\n\n/**\n* Auto-created by execute migrate-create script, Please do not modify it manually!\n*/\nmodule.exports = {\n    up: async function (queryInterface, Sequelize) {\n        // add columns    \n        await queryInterface.createTable('table_test1', {\n            columns1: {\n            type: Sequelize.INTEGER(11),\n            allowNull: false,\n            primaryKey: true,\n            autoIncrement: true,\n            comment: 'columns1 comment'\n        },\n            columns2: {\n            type: Sequelize.STRING(32),\n            allowNull: false,\n            comment: 'columns2 comment'\n        },\n            columns3: {\n            type: Sequelize.DECIMAL(11, 2),\n            defaultValue: 0,\n            allowNull: true,\n            comment: 'columns3 comment'\n        },\n            columns4: {\n            type: Sequelize.DATE,\n            comment: 'columns4 comment'\n        }\n    });    \n\n        // modify columns    \n\n        // drop columns    \n\n        // drop indexs    \n\n        // add indexs    \n        await queryInterface.addIndex('table_test1', {\n            type: 'UNIQUE',\n            fields: ['columns2'],\n            name: 'index_columns2_un'\n        });    \n        await queryInterface.addIndex('table_test1', {\n            fields: ['columns3', 'columns4'],\n            name: 'index_columns3_columns4'\n        });    \n    },\n\n    down: async function (queryInterface, Sequelize) {\n        // add columns    \n\n        // modify columns    \n\n        // drop columns    \n        await queryInterface.dropTable('table_test1');    \n\n        // drop indexs    \n\n        // add indexs    \n    }\n};\n```\n数据库配置文件如下:\n\n``` js\nmodule.exports = {\n    dialect: 'mysql',\n    database: 'database',\n    username: 'root',\n    password: 'admin@123',\n    timezone: '+08:00',\n    host: '127.0.0.1',\n    port: '3306',\n};\n```\n\n模型配置文件如下:\n\n```js\nmodule.exports = {\n    tableName: 'table_test1',\n    columns: {\n        columns1: {\n            type: 'integer',\n            length: 11,\n            allowNull: false,\n            primaryKey: true,\n            autoIncrement: true,\n            comment: 'columns1 comment'\n        },\n        columns2: {\n            type: 'string',\n            length: 32,\n            allowNull: false,\n            comment: 'columns2 comment'\n        },\n        columns3: {\n            type: 'decimal',\n            precision: 11,\n            scale: 2,\n            defaultValue: 0.00,\n            allowNull: true,\n            comment: 'columns3 comment'\n        },\n        columns4: {\n            type: 'date',\n            comment: 'columns4 comment'\n        }\n    },\n    indexs: {\n        index_columns2_un: {\n            type: 'UNIQUE',\n            fields: ['columns2']\n        },\n        index_columns3_columns4: {\n            fields: ['columns3', 'columns4']\n        }\n    }\n};\n```\n\n## migrate up\n\n```bash\n$ migrate up -h\nUsage: migrate-up [options] \u003cname\u003e\n\nOptions:\n  -V, --version               output the version number\n  --db-config-path \u003cpath\u003e     Set db config path\n  --target-version \u003cversion\u003e  Migrate up to a give migration version, default lastest\n  --migrations-path \u003cpath\u003e    Set migrations path\n  -h, --help                  output usage information\n```\n- --db-config-path: 数据库的配置文件路径，也可以通过设置环境变量: MIGRATE_DB_CONFIG_PATH\n- --target-version: 指定迁移的版本，默认是当前migration文件中版本最新的\n- --migrations-path: migration文件所在目录，也可以通过设置环境变量: MIGRATE_MIGRATIONS_PATH\n\n```bash\n$ migrate up --db-config-path /path/to/db/config --migrations-path /path/to/migrations\nnow version is up to 20181011164723\n```\n或者\n```bash\n$ MIGRATE_DB_CONFIG_PATH=/path/to/db/config MIGRATE_MIGRATIONS_PATH=/path/to/migrations migrate up\nuse MIGRATE_DB_CONFIG_PATH for db config: /path/to/db/config\nuse MIGRATE_MIGRATIONS_PATH for migrations path: /path/to/migrations\nnow version is up to 20181011164723\n```\n\n```bash\nmysql\u003e show table table_test1;\n| table_test1 | CREATE TABLE `table_test1` (\n  `columns1` int(11) NOT NULL AUTO_INCREMENT COMMENT 'columns1 comment',\n  `columns2` varchar(32) NOT NULL COMMENT 'columns2 comment',\n  `columns3` decimal(11,2) DEFAULT '0.00' COMMENT 'columns3 comment',\n  `columns4` datetime DEFAULT NULL COMMENT 'columns4 comment',\n  PRIMARY KEY (`columns1`),\n  UNIQUE KEY `index_columns2_un` (`columns2`),\n  KEY `index_columns3_columns4` (`columns3`,`columns4`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 |\n\n```\n\n## migrate down\n\n```bash\n$ migrate down -h\nUsage: migrate-down [options] \u003cname\u003e\n\nOptions:\n  -V, --version               output the version number\n  --db-config-path \u003cpath\u003e     Set db config path\n  --target-version \u003cversion\u003e  Migrate down to a give migration version, default lastest\n  --migrations-path \u003cpath\u003e    Set migrations path\n  -h, --help                  output usage information\n\n```\n- --db-config-path: 数据库的配置文件路径，也可以通过设置环境变量: MIGRATE_DB_CONFIG_PATH\n- --target-version: 指定迁移的版本，默认是当前数据库最新版本\n- --migrations-path: migration文件所在目录，也可以通过设置环境变量: MIGRATE_MIGRATIONS_PATH\n\n```bash\n$ migrate down --db-config-path /path/to/db/config --migrations-path /path/to/migrations\nnow version is down to 0\n```\n或者\n```bash\n$ MIGRATE_DB_CONFIG_PATH=/path/to/db/config MIGRATE_MIGRATIONS_PATH=/path/to/migrations migrate up\nuse MIGRATE_DB_CONFIG_PATH for db config: /path/to/db/config\nuse MIGRATE_MIGRATIONS_PATH for migrations path: /path/to/migrations\nnow version is down to 0\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhangmingfeng%2Feasy-migrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhangmingfeng%2Feasy-migrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhangmingfeng%2Feasy-migrate/lists"}