node-sql-migrations
raw SQL migrations for node
Example
In your project
var path = require('path');
require('sql-migrations').run({
});
CLI
run node ./migrate.js
with arguments
node ./migrate create migration_name
will create two migration files (up and down)
./migrations/1415860098827_up_migration_name.sql
./migrations/1415860098827_down_migration_name.sql
node ./migrate migrate
will run all pending migrations
node ./migrate.js rollback
will rollback the last migration if there is one
Programmatic API
Migrate
In your project
require('sql-migrations').migrate({
});
This returns a promise which resolves/rejects whenever the migration is complete.
Rollback
In your project
require('sql-migrations').rollback({
});
This returns a promise which resolves/rejects whenever the rollback is complete.
Configuration
Configuration should be specified as below:
var configuration = {
migrationsDir: path.resolve(__dirname, 'migrations'),
host: 'localhost',
port: 5432,
db: 'sql_migrations',
user: 'dabramov',
password: 'password',
adapter: 'pg',
parameters: {
"FOO": "bar"
},
minMigrationTime: new Date('2018-01-01').getTime()
};
You can also swap out the default logger (the console
object) for another one that supports the log and error methods. You should do this before running any other commands:
require('sql-migrations').setLogger({
log: function() {},
error: function() {}
});
Migration files
Write raw sql in your migrations. You can also include placeholders which will be substituted.
example
create table "test_table" (id bigint, name varchar(255));
drop table "test_table";