Skip to content

Commit 624e9ab

Browse files
committed
refactor: prepare removal and adjust hooks and deprecate
Deprecate old hook 'migrator:migration:hook:require' and add a warning. Prepare to finally remove all other unnecessary files. Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 707abf3 commit 624e9ab

File tree

7 files changed

+174
-134
lines changed

7 files changed

+174
-134
lines changed

lib/commands/create-migration.js

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

3-
var _assert = require('./helper/assert');
4-
var log = require('db-migrate-shared').log;
5-
var mkdirp = require('mkdirp');
6-
var fs = require('fs');
7-
var optimist = require('optimist');
8-
var util = require('util');
3+
const _assert = require('./helper/assert');
4+
const log = require('db-migrate-shared').log;
5+
const mkdirp = require('mkdirp');
6+
const fs = require('fs');
7+
const optimist = require('optimist');
8+
const util = require('util');
99

1010
function createMigrationDir (dir, callback) {
1111
fs.stat(dir, function (err) {
@@ -18,7 +18,8 @@ function createMigrationDir (dir, callback) {
1818
}
1919

2020
function executeCreateMigration (internals, config, callback) {
21-
var migrationsDir = internals.argv['migrations-dir'];
21+
let migrationsDir = internals.argv['migrations-dir'];
22+
let folder, path;
2223

2324
internals.runTimestamp = new Date();
2425

@@ -27,8 +28,6 @@ function executeCreateMigration (internals, config, callback) {
2728
internals.argv['migrations-dir'] + '/' + internals.migrationMode;
2829
}
2930

30-
var folder, path;
31-
3231
if (internals.argv._.length === 0) {
3332
log.error("'migrationName' is required.");
3433
if (!internals.isModule) {
@@ -43,8 +42,8 @@ function executeCreateMigration (internals, config, callback) {
4342
}
4443

4544
createMigrationDir(migrationsDir, function (err) {
46-
var index = require('../../connect');
47-
var Migration = require('../migration.js');
45+
const index = require('../../connect');
46+
const Migration = require('../template.js');
4847

4948
if (err) {
5049
log.error('Failed to create migration directory at ', migrationsDir, err);
@@ -64,12 +63,12 @@ function executeCreateMigration (internals, config, callback) {
6463
if (folder.length > 1) {
6564
path += '/';
6665

67-
for (var i = 0; i < folder.length - 1; ++i) {
66+
for (let i = 0; i < folder.length - 1; ++i) {
6867
path += folder[i] + '/';
6968
}
7069
}
7170

72-
var templateType = Migration.TemplateType.DEFAULT_JS;
71+
let templateType = Migration.TemplateType.DEFAULT_JS;
7372
if (
7473
shouldCreateSqlFiles(internals, config) &&
7574
shouldCreateCoffeeFile(internals, config)
@@ -85,7 +84,7 @@ function executeCreateMigration (internals, config, callback) {
8584
} else if (shouldCreateCoffeeFile(internals, config)) {
8685
templateType = Migration.TemplateType.DEFAULT_COFFEE;
8786
}
88-
var migration = new Migration(
87+
const migration = new Migration(
8988
internals.argv.title +
9089
(shouldCreateCoffeeFile(internals, config) ? '.coffee' : '.js'),
9190
path,
@@ -94,7 +93,7 @@ function executeCreateMigration (internals, config, callback) {
9493
);
9594
index.createMigration(migration, function (err, migration) {
9695
if (_assert(err, callback)) {
97-
log.info(util.format('Created migration at %s', migration.path));
96+
log.info(util.format('Created migration at %s', migration.file.path));
9897
if (shouldCreateSqlFiles(internals, config)) {
9998
createSqlFiles(internals, config, callback);
10099
} else {
@@ -120,17 +119,17 @@ function shouldCreateCoffeeFile (internals, config) {
120119
}
121120

122121
function createSqlFiles (internals, config, callback) {
123-
var migrationsDir = internals.argv['migrations-dir'];
122+
let migrationsDir = internals.argv['migrations-dir'];
124123

125124
if (internals.migrationMode && internals.migrationMode !== 'all') {
126125
migrationsDir =
127126
internals.argv['migrations-dir'] + '/' + internals.migrationMode;
128127
}
129128

130-
var sqlDir = migrationsDir + '/sqls';
129+
const sqlDir = migrationsDir + '/sqls';
131130
createMigrationDir(sqlDir, function (err) {
132-
var index = require('../../connect');
133-
var Migration = require('../migration.js');
131+
const index = require('../../connect');
132+
const Migration = require('../template.js');
134133

135134
if (err) {
136135
log.error('Failed to create migration directory at ', sqlDir, err);
@@ -142,8 +141,8 @@ function createSqlFiles (internals, config, callback) {
142141
}
143142
}
144143

145-
var templateTypeDefaultSQL = Migration.TemplateType.DEFAULT_SQL;
146-
var migrationUpSQL = new Migration(
144+
let templateTypeDefaultSQL = Migration.TemplateType.DEFAULT_SQL;
145+
const migrationUpSQL = new Migration(
147146
internals.argv.title + '-up.sql',
148147
sqlDir,
149148
internals.runTimestamp,
@@ -152,10 +151,13 @@ function createSqlFiles (internals, config, callback) {
152151
index.createMigration(migrationUpSQL, function (err, migration) {
153152
if (_assert(err, callback)) {
154153
log.info(
155-
util.format('Created migration up sql file at %s', migration.path)
154+
util.format(
155+
'Created migration up sql file at %s',
156+
migration.file.path
157+
)
156158
);
157159

158-
var migrationDownSQL = new Migration(
160+
const migrationDownSQL = new Migration(
159161
internals.argv.title + '-down.sql',
160162
sqlDir,
161163
internals.runTimestamp,
@@ -166,7 +168,7 @@ function createSqlFiles (internals, config, callback) {
166168
log.info(
167169
util.format(
168170
'Created migration down sql file at %s',
169-
migration.path
171+
migration.file.path
170172
)
171173
);
172174
if (typeof callback === 'function') callback();

lib/commands/helper/migration-hook.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
21
module.exports = function (internals) {
3-
var Migration = require('../../migration.js');
2+
var Migration = require('../../file.js');
43
return Migration.registerHook(internals.plugins, internals);
54
};

lib/file.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const inflection = require('inflection');
77
const Promise = require('bluebird');
88
const lpad = require('db-migrate-shared').util.lpad;
99

10+
Promise.promisifyAll(fs);
11+
1012
const LOADER = {
1113
seeder: 'allLoadedSeedsAsync',
1214
migration: 'allLoadedMigrationsAsync'
@@ -60,7 +62,6 @@ const File = function () {
6062
this.date = arguments[2];
6163
this.name = this.formatName(this.title, this.date);
6264
this.path = this.formatPath(arguments[1], this.name);
63-
this.templateType = arguments[3];
6465
this.internals = arguments[4];
6566
} else if (arguments.length === 2) {
6667
this.path = arguments[0];
@@ -70,7 +71,7 @@ const File = function () {
7071
this.internals = arguments[1];
7172
}
7273

73-
this.internals = this.internals.safeOptions;
74+
if (this.internals) this.internals = this.internals.safeOptions;
7475
};
7576

7677
File.prototype = {
@@ -83,15 +84,22 @@ File.prototype = {
8384
return this._required || (this._required = require(this.path));
8485
},
8586

87+
write: function (data) {
88+
return fs.writeFileAsync(this.path, data);
89+
},
90+
8691
parseTitle: parseTitle,
8792
parseDate: parseDate,
8893
formatTitle: formatTitle,
8994
formatPath: formatPath,
9095
formatName: formatName
9196
};
9297

93-
File.registerHook = function (Plugin, prefix, internals) {
94-
const plugin = Plugin.hook(prefix + ':hook:require');
98+
File.registerHook = function (Plugin, internals) {
99+
const plugin = [].concat(Plugin.hook('file:hook:require') || []).concat(
100+
Plugin.hook('migrator:migration:hook:require') || [] // backwards compatible
101+
);
102+
95103
internals.parser = internals.parser || {
96104
filesRegEx: filesRegEx,
97105
extensions: 'js'
@@ -103,7 +111,29 @@ File.registerHook = function (Plugin, prefix, internals) {
103111

104112
return Promise.resolve(plugin)
105113
.map(function (plugin) {
106-
return plugin[prefix + ':hook:require']();
114+
// Backwards compatibility and notice for everyone so they
115+
// can bug the plugin maintainer
116+
if (plugin['migrator:migration:hook:require']) {
117+
log.warn(
118+
`The plugin '${plugin.name}' is outdated! The hook` +
119+
`migrator:migration:hook:require was deprecated!`
120+
);
121+
log.warn(
122+
`Report the maintainer of '${plugin.name}' to patch the ` +
123+
`hook instead with file:hook:require.`
124+
);
125+
126+
if (plugin.maintainer && plugin.maintainer.repository) {
127+
log.verbose(
128+
`The repo of the ${plugin.name} plugin is ${
129+
plugin.maintainer.repository
130+
}!`
131+
);
132+
}
133+
return plugin['migrator:migration:hook:require']();
134+
}
135+
136+
return plugin['file:hook:require']();
107137
})
108138
.each(function (parser) {
109139
if (parser && parser.extensions) {

lib/transitions/transitioner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Migration = require('../migration.js');
1+
var Migration = require('../file.js');
22
var tryRequire = require('./try-require.js');
33
var updateVersion = require('./update-version.js');
44
var ask = require('./ask.js');

lib/walker.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ Walker.prototype = {
130130
})
131131
.each(file => {
132132
log.verbose(this.title + 'preparing to run up:', file.name);
133-
const version = file.get()._meta.version || 1;
133+
const _meta = file.get()._meta || {};
134+
const version = _meta.version || 1;
134135
return require(`./executors/versioned/v${version}`).up(
135136
this,
136137
this.driver,
@@ -158,7 +159,8 @@ Walker.prototype = {
158159
})
159160
.each(file => {
160161
log.verbose(this.title + 'preparing to run down:', file.name);
161-
const version = file.get()._meta.version || 1;
162+
const _meta = file.get()._meta || {};
163+
const version = _meta.version || 1;
162164
return require(`./executors/versioned/v${version}`).down(
163165
this,
164166
this.driver,

0 commit comments

Comments
 (0)