- Create a
.migreat.js
file that exports a database connection and migration file dir:
const dbConnection = require('./lib/db');
const path = require('path');
module.exports = {
db,
migrationDir: path.join(__dirname, './migrations')
};
- Create a migration:
$ migreat gen my_first_migration
- Edit it:
$ vim migrations/1513098322271__my_first_migration.js
module.exports = {
up: (db) => {
return db.query(`
CREATE TABLE new_table (
id SERIAL PRIMARY KEY,
data TEXT,
);
`)},
down: (db) => {
return db.query(`
DROP TABLE new_table;
`);
}
};
- Migrate:
$ migreat up
The database connection that you export in .migreat.js
can be any object with the following property: query(string) -> Promise
. Migreat will pass this connection into the up()
and down()
methods in your migration files.
Migreat relies on SQL transactions to make each migration atomic, so your database backend must support them.