Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/borsch/node-mysql-migration

node mysql migration util
https://github.com/borsch/node-mysql-migration

database-migrations migrations mysql nodejs npm-module

Last synced: 25 days ago
JSON representation

node mysql migration util

Awesome Lists containing this project

README

        

# node-mysql-migration

This plugin is create to simplify migration of mysql database migration.

Using

Installing

`npm install node-mysql-migration` - to install util

Setup

```javascript
# my_db_migrations.js
var mysql = require('mysql');
var migration = require('node-mysql-migration');

migration.migrate(mysql.createConnection({
host : 'host',
user : 'user',
password : 'password',
database : 'database'
}), __dirname + '/migrations');
```

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

File naming convention



migration script shoul have the following name template

```
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 wtire migrations script in plain SQL

WARNING

for now migration support only one command in migration script.


If you migration script contains the following
```sql
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:

```javascript
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](https://github.com/mysqljs/mysql#multiple-statement-queries)

Commands

run `npm my_db_migrations.js clean` to clean the data base


run `npm my_db_migrations.js init` to init empty migration scheta table


run `npm my_db_migrations.js migrate` to apply new migrations to your data base if such exists