Source Reading Log: node-db-migrate db:create

Log of investigating node-db-migrate bug #468.

Debug with command:

node --inspect-brk node_modules/db-migrate/bin/db-migrate db:create wenshan

resolve('db-migrate'...) registers a callback function, in which is triggered.

-> defined in api.js:

run: function () {
  load('run')(this.internals, this.config);

-> run is defined in run.js:

case 'db':
  if (folder.length < 1) {'Please enter a valid command, i.e. db:create|db:drop');
  } else {
    internals.mode = folder[1];
    load('db')(internals, config);
  • folder: is an array containing the command arguments, e.g. ["db", "create"]
  • config: database configurations such as database name, driver, host, password, port, user… read from database.json.

-> load('db')(internals, config) -> db.js : executeDB

function executeDB (internals, config, callback) {

  index.driver(config.getCurrent().settings, function (err, db) {

    if (internals.mode === 'create') {
    } else if (internals.mode === 'drop') {

-> driver in connect.js:

exports.driver = function (config, callback) {
  var internals = {};
  if (config.config) {
    internals = config.internals;
    config = config.config;

  driver.connect(config, internals, callback);

Apparently, index.driver connects to mysql using configurations from config.getCurrent().settings, which was read from database.json (according to db-migrate Get Started: Configuration)

Therefore, if you have set "database": "wenshan" in your database.json, it would try to connect with that database and fail.

An easy fix: