co-migrator

Database migration tool for compound.js

npm install co-migrator
11 downloads in the last week
21 downloads in the last month

co-migrator

Database migration tool for use in compound.js.

Installation

Step 1. Install using npm:

npm install co-migrator --save

Step 2. Add co-migrator to config/autoload.js, for example:

module.exports = function (compound) {
    return [
        'ejs-ext',
        'jugglingdb',
        'seedjs',
        'co-logger',
        'co-migrator'
    ].map(require);
};

Step 3. Add schema to db/schema.js:

var migration = require('co-migrator').Schema.Migration;
schema.define('Migration', migration);

Step 4. Add model to app/models/migration.js:

module.exports = require('co-migrator').Models.Migration;

Usage

Create a new migration:

compound m create

A new migration will be saved in db/migrations/[version]. If a migration already exists for the current version of the application, a new migration will be incremented from the highest version of the current migration.

Example:

If the application is on version 1.2.3 and a migration is in db/migrations/1.2/1.2.3 called 1.2.3-mig.1.js, a new migration 1.2.3-mig.2.js will be placed in the same directory.

Migration example:

db/schemas/post.js:

module.exports = {
    name        : String,
    description : String,
    writtenOn   : {
        type: Date,
        default: function () { return new Date(); }
    }
};

db/migrations/1.2/1.2.3/1.2.3-mig.1.js:

var postSchema = require('../../schemas/post');

module.exports = function (compound, schema, models) {
    return {
        up   : function (done) {
            var Posts = schema.define('Posts', postSchema);

            schema.autoupdate(function (err) {
                if (err) { return done(err); }
                Posts.create({
                            name        : 'My First Blog Post',
                            description : 'My first blog post evar!!'
                }, done);
            });
        },
        down : function (done) {
            // Not used.
            done();
        }
    };
};

Run Migrations

Run migrations:

compound m

Rollback migrations:

compound m down
npm loves you