Skip to content

Commit

Permalink
refactor(files): some more changes to file, walker as well as the
Browse files Browse the repository at this point in the history
start of the port of the version 1 schema.

Refers to #215

Signed-off-by: Tobias Gurtzick <[email protected]>
  • Loading branch information
wzrdtales committed Mar 27, 2018
1 parent b401830 commit 3d3a5b3
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 63 deletions.
46 changes: 46 additions & 0 deletions lib/executors/versioned/v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var log = require('db-migrate-shared').log;
var Promise = require('bluebird');

const execUnit = {
up: function (context, driver, execUnit) {
return context.driver
.startMigration()
.then(() => {
var setup = execUnit.setup;

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

return execUnit.up();
})
.then(() => {
return Promise.promisify(context.writeexecUnitRecord.bind(context))(
execUnit
);
})
.then(context.driver.endMigration.bind(context.driver));
},

down: function (context, driver, execUnit) {
return driver
.startMigration()
.then(() => {
var setup = execUnit.setup;

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

return execUnit.down();
})
.then(() => {
return Promise.promisify(context.deleteexecUnitRecord.bind(context))(
execUnit
);
})
.then(context.driver.endMigration.bind(context.driver));
}
};

module.exports = execUnit;
71 changes: 37 additions & 34 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var fs = require('fs');
var path = require('path');
var log = require('db-migrate-shared').log;
var inflection = require('inflection');
var Promise = require('bluebird');
var lpad = require('db-migrate-shared').util.lpad;
const fs = require('fs');
const path = require('path');
const log = require('db-migrate-shared').log;
const inflection = require('inflection');
const Promise = require('bluebird');
const lpad = require('db-migrate-shared').util.lpad;

function formatPath (dir, name) {
return path.join(dir, name);
Expand All @@ -31,8 +31,8 @@ function formatTitle (title) {
}

function parseDate (name) {
var date = new Date();
var match = name.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-[^.]+/);
let date = new Date();
const match = name.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-[^.]+/);
date.setUTCFullYear(match[1]);
date.setUTCDate(match[3]);
date.setUTCMonth(match[2] - 1);
Expand All @@ -43,38 +43,41 @@ function parseDate (name) {
}

function parseTitle (name) {
var match = name.match(/\d{14}-([^.]+)/);
var dashed = match[1];
return inflection.humanize(dashed, true);
const match = name.match(/\d{14}-([^.]+)/);
return inflection.humanize(match[1], true);
}

var filesRegEx = /\.js$/;

var File = {
init: function () {
if (arguments.length >= 3) {
this.title = arguments[0];
this.date = arguments[2];
this.name = this.formatName(this.title, this.date);
this.path = this.formatPath(arguments[1], this.name);
this.templateType = arguments[3];
this.internals = arguments[4];
} else if (arguments.length === 2) {
this.path = arguments[0];
this.name = this.parseName(this.path);
this.date = this.parseDate(this.name);
this.title = this.parseTitle(this.name);
this.internals = arguments[1];
}

this._super(this.internals);
},
const filesRegEx = /\.js$/;

const File = function () {
if (arguments.length >= 3) {
this.title = arguments[0];
this.date = arguments[2];
this.name = this.formatName(this.title, this.date);
this.path = this.formatPath(arguments[1], this.name);
this.templateType = arguments[3];
this.internals = arguments[4];
} else if (arguments.length === 2) {
this.path = arguments[0];
this.name = this.parseName(this.path);
this.date = this.parseDate(this.name);
this.title = this.parseTitle(this.name);
this.internals = arguments[1];
}

this._super(this.internals);
};

File.protoype = {
parseName: function (path) {
var match = path.match(/(\d{14}-[^.]+)(?:\.*?)?/);
const match = path.match(/(\d{14}-[^.]+)(?:\.*?)?/);
return match[1];
},

get: function () {
return this._required || (this._required = require(this.path));
},

parseTitle: parseTitle,
parseDate: parseDate,
formatTitle: formatTitle,
Expand All @@ -83,7 +86,7 @@ var File = {
};

File.registerHook = function (Plugin, prefix, internals) {
var plugin = Plugin.hook(prefix + ':hook:require');
const plugin = Plugin.hook(prefix + ':hook:require');
internals.parser = internals.parser || {
filesRegEx: filesRegEx,
extensions: 'js'
Expand Down
51 changes: 22 additions & 29 deletions lib/walker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var dbmUtil = require('db-migrate-shared').util;
var log = require('db-migrate-shared').log;
var Promise = require('bluebird');
var File = require('./file.js');
const dbmUtil = require('db-migrate-shared').util;
const log = require('db-migrate-shared').log;
const Promise = require('bluebird');
const File = require('./file.js');

// Not sure what will happen to this yet
function SeedLink(driver, internals) {
Expand All @@ -15,7 +15,7 @@ function SeedLink(driver, internals) {
this.links = [];
}

var Walker = function(driver, directory, interface, empty, intern) {
const Walker = function(driver, directory, interface, empty, intern) {
this.driver = dbmUtil.reduceToInterface(driver, interface);
this._driver = driver;
this.directory = directory;
Expand Down Expand Up @@ -70,16 +70,9 @@ Walker.prototype = {
},

sync: function(options, callback) {
return Migration.loadFromDatabase(
this.directory,
this._driver,
this.internals
)
.then(completedMigrations => {
var mode = dbmUtil.syncMode(
completedMigrations,
funcOrOpts.destination
);
return File.loadFromDatabase(this.directory, this._driver, this.internals)
.then(completedFiles => {
const mode = dbmUtil.syncMode(completedFiles, funcOrOpts.destination);
if (mode === 1) {
log.info(this.prefix + 'Syncing upwards.');
return this.up(options);
Expand All @@ -96,10 +89,10 @@ Walker.prototype = {
File.loadFromFilesystem(this.directory, this.internals),
File.loadFromDatabase(this.directory, this._driver, this.internals)
])
.then(function(allMigrations, completedMigrations) {
var toRun = dbmUtil.filterUp(
allMigrations,
completedMigrations,
.then((allFiles, completedFiles) => {
const toRun = dbmUtil.filterUp(
allFiles,
completedFiles,
partialName,
count
);
Expand All @@ -110,29 +103,29 @@ Walker.prototype = {

return toRun;
})
.each(function(migration) {
log.verbose(this.prefix + 'preparing to run up:', migration.name);
var version = migration._meta.version || 1;
require('./executors/versioned/v' + version).up(this.driver, migration);
.each(file => {
log.verbose(this.prefix + 'preparing to run up:', file.name);
const version = file.get()._meta.version || 1;
require('./executors/versioned/v' + version).up(this.driver, file);
})
.nodeify(callback);
},

down: function({ partialName, count }, callback) {
return File.loadFromDatabase(this.directory, this._driver, this.internals)
.then(completedMigrations => {
let toRun = dbmUtil.filterDown(completedMigrations, partialName, count);
.then(completedFiles => {
const toRun = dbmUtil.filterDown(completedFiles, partialName, count);

if (toRun.length === 0) {
log.info(this.prefix + 'Nothing to run');
}

return toRun;
})
.each(migration => {
log.verbose(this.prefix + 'preparing to run down:', migration.name);
let version = migration._meta.version || 1;
require('./executors/versioned/v' + version).up(this.driver, migration);
.each(file => {
log.verbose(this.prefix + 'preparing to run down:', file.name);
const version = file.get()._meta.version || 1;
require('./executors/versioned/v' + version).down(this.driver, file);
})
.nodeify(callback);
}
Expand Down

0 comments on commit 3d3a5b3

Please sign in to comment.