east-mysql

MySQL adapter for "east" (node.js database migration tool)

Usage no npm install needed!

<script type="module">
  import eastMysql from 'https://cdn.skypack.dev/east-mysql';
</script>

README

east mysql

MySQL adapter for east which uses the MySQL driver

All executed migrations names will be stored at _migrations collection in the current database. Object with following properties will be passed to migrate and rollback functions:

  • db - instance of the database driver

Installation

npm install -g east east-mysql

alternatively you could install it locally

Usage

Go to project dir and run

east init

create .eastrc file at current directory

{
    "adapter": "east-mysql",
    "url": "mysql://user:password@host/dbname"
}

where url is url of database which you want to migrate in MySQL URL format

now we can create some migrations

east create users

create files will look like this one

"use strict";

exports.migrate = function (client, done) {
    var db = client.db;
    done();
};

exports.rollback = function (client, done) {
    var db = client.db;
    done();
};

Inside the migrations and rollback functions you can create the scripts to migrate up to that version and to rollback from it, eg

exports.migrate = function(client, done) {
    var db = client.db;

    var sql = "CREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP)";

    db.query(sql, function (err, rows, fields) {

        done(err);

    });
};

exports.rollback = function(client, done) {
    var db = client.db;

    var sql = "DROP TABLE users";

    db.query(sql, function (err, rows, fields) {

        done(err);

    });
};

to execute the migrations

east migrate

to rollback the migration

east rollback

Creating the database

if the database doesn't exist when first migration is created, add the following to the JSON in your .eastrc file

"createDbOnConnect": true

This will automatically create the database if it doesn't exist

Running tests

Clone this repo and run npm test. You can also check code coverage by running grunt coverage

License

MIT