Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite loader #537

Merged
merged 16 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 27 additions & 26 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,9 @@ var log = require('db-migrate-shared').log;

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

var prefix = 'migration';
if (config.config) {
prefix = config.prefix || prefix;
internals = config.internals;
config = config.config;
}
Expand All @@ -19,15 +24,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 +54,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 +67,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 +97,7 @@ exports.connect = function (config, PassedClass, callback) {
PassedClass,
db,
oldClose,
prefix,
cb
);
};
Expand All @@ -101,9 +110,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 +138,7 @@ function migrationFiles (
PassedClass,
db,
close,
prefix,
cb
) {
var file;
Expand Down Expand Up @@ -156,8 +167,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 +181,10 @@ function migrationFiles (
null,
new PassedClass(
db,
config['migrations-dir'],
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals
internals,
prefix
)
);

Expand All @@ -181,14 +193,3 @@ function migrationFiles (
}
});
}

exports.createMigration = function (migration, callback) {
migration.write(function (err) {
if (err) {
callback(err);
return;
}

callback(null, migration);
});
};
46 changes: 29 additions & 17 deletions lib/commands/check.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.check(internals.argv, internals.onComplete.bind(this,
migrator, internals, callback));
migrator.check(
internals.argv,
internals.onComplete.bind(this, migrator, internals, callback)
);
});
});
});
}
);
});
};
Loading