node package manager

mysql-migrate

Simple database migration system for mysql.

node-mysql-migrate

A simple database migration tool for mysql.

Installation

Install via npm:

npm install mysql-migrate

Usage

Use it in your project like this:

var migrator = require('mysql-migrate');

The imported module exports a single function, migrate(). It takes three arguments in the following order:

  1. A MySQL database client, generated by the standard mysql module. The database to be used should be specified ahead of time.
  2. An array of migration objects, described below.
  3. A callback function, called when all migrations are completed successfully.

Migration objects look like this:

{
    name: "AddNameFieldToUsers",
    dependencies: ["CreateUsersTable"],
    up: function(dbClient, cb) {
        dbClient.query("ALTER TABLE...", function(err, results) {
            if (err) throw err;
            cb();
        });
    }
}

The name property of the migration is a string that must be unique among all migrations.

The dependencies property is an array of other migration names that the migration depends on being executed before it.

The up property is a function that takes the db client and a callback as arguments and should perform the necessary operations on the database and call the callback when finished.

When you first call migrate() it will attempt to create a migrations table in your database. This table is used to keep track of what migrations have been completed.

After it does this, it will sort the provided migrations in an appropriate order and execute each one in series.

Currently, only upward migrations are supported. You can't rollback or undo migrations unless you do so manually.

Todo

  • command line utilities to assist in the creation of migration objects and to assist in executing migrations
  • examples
  • down() function for migration objects
  • enforce stucture of migrations table
  • clean up verbosity of migrate()