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 db-migrate.run() 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) {
    log.info('Please enter a valid command, i.e. db:create|db:drop');
  } else {
    internals.mode = folder[1];
    load('db')(internals, config);
  }
  break;
  • 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) {
    assert.ifError(err);

    if (internals.mode === 'create') {
      db.createDatabase(...);
    } else if (internals.mode === 'drop') {
      db.dropDatabase(...);
    }
  });
}

-> 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: https://github.com/db-migrate/node-db-migrate/pull/644