From 3d3a5b31c4b89b7853fc546c60ed023554590518 Mon Sep 17 00:00:00 2001 From: Tobias Gurtzick Date: Fri, 2 Feb 2018 12:09:40 +0100 Subject: [PATCH] refactor(files): some more changes to file, walker as well as the start of the port of the version 1 schema. Refers to #215 Signed-off-by: Tobias Gurtzick --- lib/executors/versioned/v1.js | 46 +++++++++++++++++++++++ lib/file.js | 71 ++++++++++++++++++----------------- lib/walker.js | 51 +++++++++++-------------- 3 files changed, 105 insertions(+), 63 deletions(-) create mode 100644 lib/executors/versioned/v1.js diff --git a/lib/executors/versioned/v1.js b/lib/executors/versioned/v1.js new file mode 100644 index 00000000..105bd6c4 --- /dev/null +++ b/lib/executors/versioned/v1.js @@ -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; diff --git a/lib/file.js b/lib/file.js index b817a128..4255dafc 100644 --- a/lib/file.js +++ b/lib/file.js @@ -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); @@ -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); @@ -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, @@ -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' diff --git a/lib/walker.js b/lib/walker.js index ef5ea439..b4406188 100644 --- a/lib/walker.js +++ b/lib/walker.js @@ -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) { @@ -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; @@ -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); @@ -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 ); @@ -110,18 +103,18 @@ 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'); @@ -129,10 +122,10 @@ Walker.prototype = { 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); }