Table of Contents
Note: this library was based on mongodb-migrations
$ npm install mongo-migrator -g
The package installs a single CLI executable — mograte
.
When installing locally to your project this executable can be found at
./node_modules/.bin/mograte
.
When installing globally the executable should automatically become accessible on your PATH.
$ mograte init
The CLI app will crate a file called configmigrate.json, this file has the default configuration
{
"migration": {
"directory": "migrations/",
"collection": "migrations"
},
"mongo": {
"database": "test",
"user": "mongo-user",
"password": "mongo-password",
"replicaSet": "replicaSet",
"servers": [{
"host": "localhost",
"port": 27017
}]
}
}
The configuration object can have the following keys:
migration
— Contains the configuration to run migrations,mongo
- Contains the configuration for MongoDB,migration.directory
— the directory (path relative to the current folder) to store migration files in and read them from,migration.collection
— The name of the MongoDB collection to track already ran migrations,mongo.database
— MongoDB database where migrations will be applied,mongo.user
[optional] — MongoDB user name when authentication is required,mongo.password
[optional] — MongoDB password when authentication is required,mongo.replicaSet
[optional] — MongoDB replicaSet if database has one,mongo.servers
- MongoDB servers configuration.mongo.servers[@].host
[localhost by default]- MongoDB host of servermongo.servers[@].port
[27017 by default]- MongoDB port of server
The app simplifies creating migration stubs by providing a command
$ mograte create 'migration name'
This creates automatically numbered file ddmmyyyyhhmmssms-migration-name.js
inside of the directory
defined in the
configuration file.
The migration file must be a EcmaScript6 module exporting the following:
id
— a string that's used to identify the migration (filled automatically when creating migrations throughmograte create
).upgrade
— a function used for forward migration.downgrade
— a function used for backward migration.
See Configuration if your config file has non-standard name.
The upgrade
and downgrade
functions take a single parameter — a Node-style callback:
const upgrade = function(done) {
// call done() when migration is successfully finished
// call done(error) in case of error
}
The upgrade
and downgrade
functions are executed with the scope
providing 2 convenient properties:
this.db
is an open MongoDB native driver connection. Useful if you are not using any ODM library.this.log
is a function allowing you to print informative messages during the progress of your migration.
'use strict';
const id = '${name}';
const upgrade = function(done) {
// use this.db for MongoDB communication, and this.log() for logging
done();
};
const downgrade = function(done) {
// use this.db for MongoDB communication, and this.log() for logging
done();
};
module.exports = {
id: id,
upgrade: upgrade,
downgrade: downgrade
};
Run all migrations from the directory
(specified in
Configuration) by calling
For Upgrades
$ mograte upgrade [null|id|name]
Where
- null will run all pending migrations,
- id is only the numeric on id
- name is tha part of the name on id
For downgrades
$ mograte downgrade [null|id|name]
Where
- null will do rollback to all migrations,
- id is only the numeric on id
- name is tha part of the name on id
The library only runs migrations that:
- have
upgrade
function defined, - have
downgrade
function defined, - were not ran before against this database.
- were downgraded at any moment
Successfully upgrade migrations are recorded in the collection
specified in Configuration and successfully
downgrade migrations remove the record from the collection
.
The migration process is stopped instantly if some migration fails (returns error in its callback).
See Configuration if your config file has no standard configuration.
The library also supports programmatic usage.
Start with require
'ing it:
var mograte = require('mongo-migrator');
Next, you can use mograte as was described before:
mograte.init();
mograte.create(name);
mograte.upgrade(option);
mograte.downgrade(option);
For downgrade and upgrade you have to sent the option in an Array as the second item([null, option]).