2.1.0 • Published 7 years ago
sqlmigrate v2.1.0
sqlmigrate
Helps automating MySQL migrations written in plain .sql
scripts.
Note: The CLI utility API has changed in v2.
Usage
Library
require('sqlmigrate').create({
migrationsDir: 'db/migrations',
dbConfig: {
database: 'sqlmigrate_test',
host: '127.0.0.1',
port: 3306,
user: 'travis',
password: ''
}
})
.migrate();
CLI
sqlmigrate
script will attempt to read the config file from the current
working directory named .sqlmigrate
:
// .sqlmigrate example
module.exports = {
migrationsDir: 'src/db/migrations',
dbConfig: {
database: process.env.DB_NAME,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASS
}
};
If driver
property is specified in the config file, it can be used to select
the database client. For example, this is how mysql2 can be used:
module.exports = {
migrationsDir: 'src/db/migrations',
driver: 'mysql2',
dbConfig: {...}
}
Installation
$ npm install sqlmigrate
$ export PATH="$(PWD)/node_modules/.bin:$(PATH)"
# or
$ npm install -g sqlmigrate
CLI help
$ sqlmigrate --help
Usage: sqlmigrate [command] [args...]
Migration will be performed when no command is specified.
The default command accepts the following optional args:
--config=file config file to use, default is .sqlmigrate
--max=n max number of migrations to perform
--any-order do not fail if there are new migrations
created before the last executed migration
Commands:
create [--name=string] creates a migration
help prints this help
init initialize config file
Initize config
$ sqlmigrate init [--config=/path/to/file]
Creating a migration script
$ sqlmigrate create --name='new-migration' [--config=/path/to/file]
Execute migrations
$ sqlmigrate [--config=/path/to/file]
Execute first two migrations
$ sqlmigrate --max=2
Perform the migrations, ignoring the order of currently executed migrations
$ sqlmigrate --any-order