Skip to content

Commit

Permalink
refactor(loader): functioning loader
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed Apr 10, 2018
1 parent 6234d42 commit 90038fc
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 80 deletions.
28 changes: 17 additions & 11 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ function dbmigrate (plugins, isModule, options, callback) {
// delayed loading of bluebird
Promise = require('bluebird');
this.internals.migrationOptions = {
dbmigrate: this.internals.dbm,
dbmigrate: {
version: this.internals.dbm.version,
dataType: this.internals.dbm.dataType
},
dryRun: this.internals.dryRun,
cwd: this.internals.cwd,
noTransactions: this.internals.notransactions,
verbose: this.internals.verbose,
type: this.internals.dbm.dataType,
log: log,
ignoreOnInit: this.internals.argv['ignore-on-init'],
Promise: Promise
};
this.internals.seederOptions = {
dbmigrate: this.internals.dbm,
Promise: Promise
};
this.internals.safeOptions = this.internals.migrationOptions;
}

dbmigrate.prototype = {
Expand All @@ -101,10 +107,10 @@ dbmigrate.prototype = {
},

/**
* Registers and initializes hooks.
*
* @returns Promise
*/
* Registers and initializes hooks.
*
* @returns Promise
*/
registerAPIHook: function (callback) {
var plugins = this.internals.plugins;
var self = this;
Expand Down Expand Up @@ -309,8 +315,8 @@ dbmigrate.prototype = {
},

/**
* Transition migrations to the latest defined protocol.
*/
* Transition migrations to the latest defined protocol.
*/
transition: function () {
load('transition')(this.internals);
},
Expand Down
43 changes: 28 additions & 15 deletions connect.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* This file is going to disappear.
* Only still here for backwards compatibility.
* */
var recursive = require('final-fs').readdirRecursive;
var fs = require('fs');
var driver = require('./lib/driver');
Expand All @@ -6,8 +10,10 @@ var log = require('db-migrate-shared').log;

exports.connect = function (config, PassedClass, callback) {
var internals = {};

var prefix = 'migration';
var migrationsDir;
if (config.config) {
prefix = config.prefix || prefix;
internals = config.internals;
config = config.config;
}
Expand All @@ -19,15 +25,16 @@ exports.connect = function (config, PassedClass, callback) {
}

if (internals.migrationMode) {
var dirPath = path.resolve(config['migrations-dir'] || 'migrations');

var dirPath = path.resolve(
internals.argv['migrations-dir'] || 'migrations'
);
if (internals.migrationMode !== 'all') {
var switched = false;
var newConf;

try {
newConf = require(path.resolve(
config['migrations-dir'] || 'migrations',
internals.argv['migrations-dir'] || 'migrations',
internals.migrationMode
) + '/config.json');
log.info(
Expand All @@ -48,9 +55,10 @@ exports.connect = function (config, PassedClass, callback) {
null,
new PassedClass(
db,
config['migrations-dir'],
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals
internals,
prefix
)
);
});
Expand All @@ -60,17 +68,18 @@ exports.connect = function (config, PassedClass, callback) {
null,
new PassedClass(
db,
config['migrations-dir'],
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals
internals,
prefix
)
);
}
} else {
recursive(
dirPath,
false,
config['migrations-dir'] || 'migrations'
internals.argv['migrations-dir'] || 'migrations'
).then(function (files) {
var oldClose = db.close;

Expand All @@ -89,6 +98,7 @@ exports.connect = function (config, PassedClass, callback) {
PassedClass,
db,
oldClose,
prefix,
cb
);
};
Expand All @@ -101,9 +111,10 @@ exports.connect = function (config, PassedClass, callback) {
null,
new PassedClass(
db,
config['migrations-dir'],
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals
internals,
prefix
)
);
}
Expand All @@ -128,6 +139,7 @@ function migrationFiles (
PassedClass,
db,
close,
prefix,
cb
) {
var file;
Expand Down Expand Up @@ -156,8 +168,8 @@ function migrationFiles (

db.switchDatabase(switched ? newConf : config.database, function () {
internals.matching = file.substr(
file.indexOf(config['migrations-dir'] || 'migrations') +
(config['migrations-dir'] || 'migrations').length +
file.indexOf(internals.argv['migrations-dir'] || 'migrations') +
(internals.argv['migrations-dir'] || 'migrations').length +
1
);

Expand All @@ -170,9 +182,10 @@ function migrationFiles (
null,
new PassedClass(
db,
config['migrations-dir'],
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals
internals,
prefix
)
);

Expand Down
46 changes: 29 additions & 17 deletions lib/commands/up.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,46 @@ var assert = require('./helper/assert.js');
var migrationHook = require('./helper/migration-hook.js');

module.exports = function (internals, config, callback) {
migrationHook(internals)
.then(function () {
var Migrator = require('../migrator.js');
var index = require('../../connect');
migrationHook(internals).then(function () {
var Migrator = require('../walker.js');
var index = require('../../connect');

if (!internals.argv.count) {
internals.argv.count = Number.MAX_VALUE;
}
index.connect({
if (!internals.argv.count) {
internals.argv.count = Number.MAX_VALUE;
}
index.connect(
{
config: config.getCurrent().settings,
internals: internals
}, Migrator, function (err, migrator) {
internals: internals,
prefix: 'migration'
},
Migrator,
function (err, migrator) {
if (!assert(err, callback)) return;

if (internals.locTitle) {
migrator.migrationsDir = path.resolve(internals.argv['migrations-dir'],
internals.locTitle);
} else { migrator.migrationsDir = path.resolve(internals.argv['migrations-dir']); }
migrator.migrationsDir = path.resolve(
internals.argv['migrations-dir'],
internals.locTitle
);
} else {
migrator.migrationsDir = path.resolve(
internals.argv['migrations-dir']
);
}

internals.migrationsDir = migrator.migrationsDir;

migrator.driver.createMigrationsTable(function (err) {
if (!assert(err, callback)) return;
log.verbose('migration table created');

migrator.up(internals.argv, internals.onComplete.bind(this,
migrator, internals, callback));
migrator.up(
internals.argv,
internals.onComplete.bind(this, migrator, internals, callback)
);
});
});
});
}
);
});
};
16 changes: 10 additions & 6 deletions lib/executors/versioned/v1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const Promise = require('bluebird');
const { maybePromise } = require('../../temputils.js');
const { maybePromised } = require('../../temputils.js');

const execUnit = {
up: function (context, driver, file) {
Expand All @@ -9,13 +11,15 @@ const execUnit = {
const _file = file.get();

if (typeof _file.setup === 'function') {
_file.setup(context.internals.fileOptions, context.seedLink);
_file.setup(context.internals.safeOptions, context.seedLink);
}

return maybePromise(_file.up);
return maybePromised(file, _file.up, [context.driver]);
})
.then(() => {
return Promise.promisify(context.writeRecord.bind(context))(file);
return Promise.promisify(context.writeMigrationRecord.bind(context))(
file
);
})
.then(context.driver.endMigration.bind(context.driver));
},
Expand All @@ -27,10 +31,10 @@ const execUnit = {
const _file = file.get();

if (typeof _file.setup === 'function') {
_file.setup(context.internals.fileOptions, context.seedLink);
_file.setup(context.internals.safeOptions, context.seedLink);
}

return maybePromise(_file.down);
return maybePromised(file, _file.down, [context.driver]);
})
.then(() => {
return Promise.promisify(context.deleteRecord.bind(context))(file);
Expand Down
19 changes: 12 additions & 7 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const inflection = require('inflection');
const Promise = require('bluebird');
const lpad = require('db-migrate-shared').util.lpad;

const LOADER = {
seeder: 'allLoadedSeedsAsync',
migration: 'allLoadedMigrationsAsync'
};

function formatPath (dir, name) {
return path.join(dir, name);
}
Expand Down Expand Up @@ -65,10 +70,10 @@ const File = function () {
this.internals = arguments[1];
}

this._super(this.internals);
this.internals = this.internals.safeOptions;
};

File.protoype = {
File.prototype = {
parseName: function (path) {
const match = path.match(/(\d{14}-[^.]+)(?:\.*?)?/);
return match[1];
Expand Down Expand Up @@ -115,8 +120,8 @@ File.registerHook = function (Plugin, prefix, internals) {
});
};

File.loadFromFileystem = function (dir, type, internals) {
log.verbose('loading ' + type + ' from dir', dir);
File.loadFromFileystem = function (dir, prefix, internals) {
log.verbose(`[${prefix}] loading from dir`, dir);

return fs
.readdirAsync(dir)
Expand All @@ -131,9 +136,9 @@ File.loadFromFileystem = function (dir, type, internals) {
});
};

File.loadFromDatabase = function (dir, type, loader, internals) {
log.verbose('loading ' + type + ' from database');
return loader()
File.loadFromDatabase = function (dir, prefix, driver, internals) {
log.verbose(`[${prefix}] loading from database`);
return driver[LOADER[prefix]]()
.catch(function (err) {
if (internals.dryRun) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Migration = Skeleton.extend({
this.internals = arguments[1];
}

this._super(this.internals);
this._super(this.internals.migrationOptions);
}
});

Expand Down
4 changes: 2 additions & 2 deletions lib/temputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isPromise (probe) {
);
}

function maybePromise (context, action, params) {
function maybePromised (context, action, params) {
let cbExecuted = false;

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -50,5 +50,5 @@ function maybePromise (context, action, params) {
}

module.exports = {
maybePromise: maybePromise
maybePromised: maybePromised
};
Loading

0 comments on commit 90038fc

Please sign in to comment.