0.3.1 • Published 8 years ago

mysql-migration-promise v0.3.1

Weekly downloads
60
License
GPL-2.0
Repository
github
Last release
8 years ago

mysql-migration-promise

This plugin was initially thought to be fork of https://github.com/borsch/node-mysql-migration, but ended up with quite a few changes.

It gives you the option to run your migrations automatically when starting your application

npm install mysql-migration-promise - to install util

//server.js
const mysql = require('mysql2');
const mysqlMigration = require('mysql-migration-promise');

async function init() {
  try {
    let connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : 'root',
      database : 'compendium',
      multipleStatements: true
    });

    let migrationService = await mysqlMigration.init(connection, __dirname + '/migrations');
    await migrationService.migrate();
  } catch (error) {
    console.error('Unable to start database: ', error);
  }

  app.listen(port);
}

init();

/migrations - is a folder where all migrations scripts are located. There is no default value for it so you should specify it

V(version name)__name_of_script_separated_with_lower_underline.sql

#example
V1__init_tables.sql
V2__add_new_column.sql

inside migrations file you should write migrations script in plain SQL

WARNING

for now migration support only one command in migration script. If you migration script contains the following

ALTER TABLE `tbl_name`
    ADD COLUMN `column_name` VARCHAR(250);
    
ALTER TABLE `tbl_name`
    ADD COLUMN `column_name1` VARCHAR(250);
    
UPDATE `tbl_name` SET `column_name`="asd";

then migration will fails. to solve this split such migration into three separate migration OR customize your connection settings. use:

migration.migrate(mysql.createConnection({
    host     : 'host',
    user     : 'user',
    password : 'password',
    database : 'database',
    multipleStatements: true // add this to allow multiple queries in single migration file
}), __dirname + '/migrations');

official node-mysql doc