Executes migration files for MySQL.
npm install --save @yahapi/mysql-migrations
import Migrations from '@yahapi/mysql-migrations';
const migrations = new Migrations({
database: 'mysql_migrations',
host: '127.0.0.1',
port: 3306,
user: 'test',
password: 'test',
}, {});
migrations.run(`${__dirname}/migrations/*.js`);
migrations.on('log', (lvl, msg) => {
console.log(`${lvl}: ${msg}`);
});
The signature new Migrations(config)
accepts mysql connection options which are identical to mysql.createPool(). At a minimum host
, database
, user
and password
must be specified.
log
Emitted with two arguments;(lvl, msg)
.
Run docker-compose up
to start a MySQL server with correct table and user.
If you're not using docker-compose, make sure to have a running MySQL server on port 3306 with the following database setup:
CREATE DATABASE mysql_migrations;
CREATE USER 'mysql_migrations'@'%' IDENTIFIED BY 'mysql_migrations';
GRANT ALL PRIVILEGES ON mysql_migrations.* TO 'mysql_migrations'@'%' WITH GRANT OPTION;