Node.js Database Migration
- Migration between multiple database
Support migration from one mysql database to another mysql database, or even to a mongo database
- Script based migration
Support javascript migration script besides SQL migration script. Provide extreme flexibility to data migration.
- Daemon to handle delta data
Support spin up a daemon to handle delta data. Allows data migration for some specific situation and parameters.
Install globally:
npm install -g node-db-migrator
Install locally
npm install node-db-migrator
Executable location:
node_modules/node-db-migration/bin/node-db-migrator
Before start the migration, create a database.json
file.
Example:
{
"defaultDb": "mysql",
"mysql": {
"driver": "mysql",
"user": "root",
"password": "yourpassword",
"database": "node_db_migration",
"host": "localhost",
"multipleStatements": true
},
"mongo": {
"driver": "mongodb",
"database": "node_db_migration",
"host": "localhost"
}
}
node-db-migrator create "your file name"
A new migration script will be created in your migration folder. By default the folder is your_workspace/migrations
.
Add code to the scripts to create a new migration.
node-db-migrator up
Only migrations that all not run yet will be run. If there're multiple migrations to be run, they will be run sequentially according to their creation time.
node-db-migrator up migration_file_name
node-db-migrator down
All migrations that are run already will be reverted.
node-db-migrator down migration_file_name
node-db-migrator trigger "your trigger script name"
Create a triggering script to handle delta data.
Notice: a token will be shown in the terminal, you can use it to refer to this triggering script.
Triggering script is used to simplify the double write. Sometimes it's very hard to implement a double write in your code, then you can think about using a triggering script.
When writing data, you called the API of triggering script and passed the parameters needed. The triggering script will do everything else.
For example: You are modifying changing column info
to new_info
in Customer
table. In your code you can do:
// pseudo code
triggerMigration(
token /*your migration token*/,
{customerId: customer_id} /*params*/
);
Then in node db migrator's daemon, it will catch this request and do the migration.
A common triggering migration script will look like this:
exports.trigger = function(db, params, callback) {
var customerId = params.customer_id;
db.mysql.query(
'some sql where customer_id = ?',
[customerId],
callback
);
};
node-db-migrator daemon
Spin up a daemon to listen to request to triggering migrations. By default it will spin up an http server (port: 8899) and a kafka consumer (topic: kafka_node_db_migration).
# list all daemon
node-db-migrator daemon list
# stop specific daemon
node-db-migrator daemon stop [daemon_id]
# stop all daemon
node-db-migrator daemon stopall
There're several options when using the node db migrator
Print version info.
The directory containing your migration files. [default: "workspace/migrations"]
Set the path of the migration table, which stores the migration history. [default: "workspace/db"]
Location of the database.json file. [default: "workspace/database.json"]
Database might in use (mysql,mongo). [default: "mysql"]
Port number when running daemon. [default: "8899"]
Automatically create two sql files for up and down statements in /sqls and generate the javascript code that loads them. [default: false | To be implemented]
Prints the SQL but doesn't run it. [default: false | To be implemented]