Skip to content

Commit 879b1fa

Browse files
committed
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 <[email protected]>
1 parent 5d65cb7 commit 879b1fa

File tree

3 files changed

+105
-63
lines changed

3 files changed

+105
-63
lines changed

lib/executors/versioned/v1.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var log = require('db-migrate-shared').log;
2+
var Promise = require('bluebird');
3+
4+
const execUnit = {
5+
up: function (context, driver, execUnit) {
6+
return context.driver
7+
.startMigration()
8+
.then(() => {
9+
var setup = execUnit.setup;
10+
11+
if (typeof setup === 'function') {
12+
setup(context.internals.execUnitOptions, context.seedLink);
13+
}
14+
15+
return execUnit.up();
16+
})
17+
.then(() => {
18+
return Promise.promisify(context.writeexecUnitRecord.bind(context))(
19+
execUnit
20+
);
21+
})
22+
.then(context.driver.endMigration.bind(context.driver));
23+
},
24+
25+
down: function (context, driver, execUnit) {
26+
return driver
27+
.startMigration()
28+
.then(() => {
29+
var setup = execUnit.setup;
30+
31+
if (typeof setup === 'function') {
32+
setup(context.internals.execUnitOptions, context.seedLink);
33+
}
34+
35+
return execUnit.down();
36+
})
37+
.then(() => {
38+
return Promise.promisify(context.deleteexecUnitRecord.bind(context))(
39+
execUnit
40+
);
41+
})
42+
.then(context.driver.endMigration.bind(context.driver));
43+
}
44+
};
45+
46+
module.exports = execUnit;

lib/file.js

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
var fs = require('fs');
4-
var path = require('path');
5-
var log = require('db-migrate-shared').log;
6-
var inflection = require('inflection');
7-
var Promise = require('bluebird');
8-
var lpad = require('db-migrate-shared').util.lpad;
3+
const fs = require('fs');
4+
const path = require('path');
5+
const log = require('db-migrate-shared').log;
6+
const inflection = require('inflection');
7+
const Promise = require('bluebird');
8+
const lpad = require('db-migrate-shared').util.lpad;
99

1010
function formatPath (dir, name) {
1111
return path.join(dir, name);
@@ -31,8 +31,8 @@ function formatTitle (title) {
3131
}
3232

3333
function parseDate (name) {
34-
var date = new Date();
35-
var match = name.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-[^.]+/);
34+
let date = new Date();
35+
const match = name.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-[^.]+/);
3636
date.setUTCFullYear(match[1]);
3737
date.setUTCDate(match[3]);
3838
date.setUTCMonth(match[2] - 1);
@@ -43,38 +43,41 @@ function parseDate (name) {
4343
}
4444

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

51-
var filesRegEx = /\.js$/;
52-
53-
var File = {
54-
init: function () {
55-
if (arguments.length >= 3) {
56-
this.title = arguments[0];
57-
this.date = arguments[2];
58-
this.name = this.formatName(this.title, this.date);
59-
this.path = this.formatPath(arguments[1], this.name);
60-
this.templateType = arguments[3];
61-
this.internals = arguments[4];
62-
} else if (arguments.length === 2) {
63-
this.path = arguments[0];
64-
this.name = this.parseName(this.path);
65-
this.date = this.parseDate(this.name);
66-
this.title = this.parseTitle(this.name);
67-
this.internals = arguments[1];
68-
}
69-
70-
this._super(this.internals);
71-
},
50+
const filesRegEx = /\.js$/;
51+
52+
const File = function () {
53+
if (arguments.length >= 3) {
54+
this.title = arguments[0];
55+
this.date = arguments[2];
56+
this.name = this.formatName(this.title, this.date);
57+
this.path = this.formatPath(arguments[1], this.name);
58+
this.templateType = arguments[3];
59+
this.internals = arguments[4];
60+
} else if (arguments.length === 2) {
61+
this.path = arguments[0];
62+
this.name = this.parseName(this.path);
63+
this.date = this.parseDate(this.name);
64+
this.title = this.parseTitle(this.name);
65+
this.internals = arguments[1];
66+
}
67+
68+
this._super(this.internals);
69+
};
7270

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

77+
get: function () {
78+
return this._required || (this._required = require(this.path));
79+
},
80+
7881
parseTitle: parseTitle,
7982
parseDate: parseDate,
8083
formatTitle: formatTitle,
@@ -83,7 +86,7 @@ var File = {
8386
};
8487

8588
File.registerHook = function (Plugin, prefix, internals) {
86-
var plugin = Plugin.hook(prefix + ':hook:require');
89+
const plugin = Plugin.hook(prefix + ':hook:require');
8790
internals.parser = internals.parser || {
8891
filesRegEx: filesRegEx,
8992
extensions: 'js'

lib/walker.js

+22-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var dbmUtil = require('db-migrate-shared').util;
2-
var log = require('db-migrate-shared').log;
3-
var Promise = require('bluebird');
4-
var File = require('./file.js');
1+
const dbmUtil = require('db-migrate-shared').util;
2+
const log = require('db-migrate-shared').log;
3+
const Promise = require('bluebird');
4+
const File = require('./file.js');
55

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

18-
var Walker = function(driver, directory, interface, empty, intern) {
18+
const Walker = function(driver, directory, interface, empty, intern) {
1919
this.driver = dbmUtil.reduceToInterface(driver, interface);
2020
this._driver = driver;
2121
this.directory = directory;
@@ -70,16 +70,9 @@ Walker.prototype = {
7070
},
7171

7272
sync: function(options, callback) {
73-
return Migration.loadFromDatabase(
74-
this.directory,
75-
this._driver,
76-
this.internals
77-
)
78-
.then(completedMigrations => {
79-
var mode = dbmUtil.syncMode(
80-
completedMigrations,
81-
funcOrOpts.destination
82-
);
73+
return File.loadFromDatabase(this.directory, this._driver, this.internals)
74+
.then(completedFiles => {
75+
const mode = dbmUtil.syncMode(completedFiles, funcOrOpts.destination);
8376
if (mode === 1) {
8477
log.info(this.prefix + 'Syncing upwards.');
8578
return this.up(options);
@@ -96,10 +89,10 @@ Walker.prototype = {
9689
File.loadFromFilesystem(this.directory, this.internals),
9790
File.loadFromDatabase(this.directory, this._driver, this.internals)
9891
])
99-
.then(function(allMigrations, completedMigrations) {
100-
var toRun = dbmUtil.filterUp(
101-
allMigrations,
102-
completedMigrations,
92+
.then((allFiles, completedFiles) => {
93+
const toRun = dbmUtil.filterUp(
94+
allFiles,
95+
completedFiles,
10396
partialName,
10497
count
10598
);
@@ -110,29 +103,29 @@ Walker.prototype = {
110103

111104
return toRun;
112105
})
113-
.each(function(migration) {
114-
log.verbose(this.prefix + 'preparing to run up:', migration.name);
115-
var version = migration._meta.version || 1;
116-
require('./executors/versioned/v' + version).up(this.driver, migration);
106+
.each(file => {
107+
log.verbose(this.prefix + 'preparing to run up:', file.name);
108+
const version = file.get()._meta.version || 1;
109+
require('./executors/versioned/v' + version).up(this.driver, file);
117110
})
118111
.nodeify(callback);
119112
},
120113

121114
down: function({ partialName, count }, callback) {
122115
return File.loadFromDatabase(this.directory, this._driver, this.internals)
123-
.then(completedMigrations => {
124-
let toRun = dbmUtil.filterDown(completedMigrations, partialName, count);
116+
.then(completedFiles => {
117+
const toRun = dbmUtil.filterDown(completedFiles, partialName, count);
125118

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

130123
return toRun;
131124
})
132-
.each(migration => {
133-
log.verbose(this.prefix + 'preparing to run down:', migration.name);
134-
let version = migration._meta.version || 1;
135-
require('./executors/versioned/v' + version).up(this.driver, migration);
125+
.each(file => {
126+
log.verbose(this.prefix + 'preparing to run down:', file.name);
127+
const version = file.get()._meta.version || 1;
128+
require('./executors/versioned/v' + version).down(this.driver, file);
136129
})
137130
.nodeify(callback);
138131
}

0 commit comments

Comments
 (0)