node-migrator

0.1.3 • Public • Published

node-migrator

This module is for automate executing migrations and use it with any VCS system.

Installation

Simple type in your console:

npm install node-migrator --save

or add this to your package.json:

...
    "dependencies": {
        "node-migrator": "^0.1.0"
    }
...

How it works

Migrations are js-modules with .up() and .down() export methods. These methods are for upgrading & downgrading to some version.

Migrations should contains changes in database, not-project files, environment, etc in specific version.

You store all migrations files in one dir, name as current version (use semver syntax):

  • 0.1.0.js
  • 0.1.1.js
  • 0.1.2.js
  • 0.2.0.js

Tune Migrator with all paths and give it current project's version. Migrator will do the rest.

Options

{
    current: '0.1.0', // current version in semver format
    migrations_dir: './migrations', // dir where you store migrations
    done_migrations_dir: './migrations/done', // dir where migrator will store done migrations (log)
    history_file: './migrations/done/history.json', // json file with history of done migrations
    params: [ database, fs ] // params passed to .up() and .down() methods of every migration
}

Migration example

.up() and .down() methods can be just function or Promises (if you work with database for example)

module.exports = {
	up: function ( database ) {
		return new Promise( function ( resolve ) {
			database.createTable( ... ).then( resolve );
		} );
	},

	down: function ( data, saveDataToFile ) {
        database.dropTable( ... ); // it's just for example. With database - promise is better
	}
};

Execute example

Execute is really simple. You need just change current version.

var options = { ... };
options.current = config.get( 'current version' );

var Migrator = require( 'node-migrator' );
var migrator = new Migrator( options );

migrator.run().then( function ( migrations ) {
    // Migrations is array with all done migrations (or empty, if no migrations needed).
    // Array of objects in format: { name: migration-version, action: up / down }
    
    console.log( migrations[ 0 ] );
    // { name: '0.1.0', action: 'up' }
} ).catch( function ( error ) {
    // error is your problem
} );

Important

Don't forget to git-ignore done migrations & history file.

Example. If you checkout to previous version (0.5.0 -> 0.3.0), migration files are deleted (in previous version there was not 0.5.0, 0.4.0 migrations), so Migrator need its log-files from ./migrations/done dir.

Readme

Keywords

Package Sidebar

Install

npm i node-migrator

Weekly Downloads

1

Version

0.1.3

License

MIT

Last publish

Collaborators

  • levchurakov