db-migrate 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/api.js CHANGED
@@ -192,6 +192,18 @@ dbmigrate.prototype = {
192
192
  this.internals.argv = argv;
193
193
  },
194
194
 
195
+ /**
196
+ * Every call starts from the options of the instance: the count,
197
+ * destination and scope of an earlier call do not carry over.
198
+ */
199
+ _begin: function () {
200
+ delete this.internals.argv.count;
201
+ delete this.internals.argv.destination;
202
+ this.internals.migrationMode = undefined;
203
+ this.internals.matching = '';
204
+ this.internals.locTitle = undefined;
205
+ },
206
+
195
207
  /**
196
208
  * Executes up a given number of migrations or a specific one.
197
209
  *
@@ -199,6 +211,7 @@ dbmigrate.prototype = {
199
211
  */
200
212
  up: function (specification, opts, callback) {
201
213
  var executeUp = load('up');
214
+ this._begin();
202
215
 
203
216
  if (arguments.length > 0) {
204
217
  if (typeof specification === 'string') {
@@ -229,6 +242,7 @@ dbmigrate.prototype = {
229
242
  */
230
243
  down: function (specification, opts, callback) {
231
244
  var executeDown = load('down');
245
+ this._begin();
232
246
 
233
247
  if (arguments.length > 0) {
234
248
  if (typeof specification === 'number') {
@@ -255,6 +269,7 @@ dbmigrate.prototype = {
255
269
 
256
270
  fix: function (specification, opts, callback) {
257
271
  var executeFix = load('fix');
272
+ this._begin();
258
273
 
259
274
  if (arguments.length > 0) {
260
275
  if (typeof specification === 'string') {
@@ -280,6 +295,7 @@ dbmigrate.prototype = {
280
295
 
281
296
  check: function (specification, opts, callback) {
282
297
  var executeCheck = load('check');
298
+ this._begin();
283
299
 
284
300
  if (arguments.length > 0) {
285
301
  if (typeof specification === 'number') {
@@ -308,6 +324,7 @@ dbmigrate.prototype = {
308
324
  */
309
325
  sync: function (specification, opts, callback) {
310
326
  var executeSync = load('sync');
327
+ this._begin();
311
328
 
312
329
  if (arguments.length > 0) {
313
330
  if (typeof specification === 'string') {
@@ -332,6 +349,7 @@ dbmigrate.prototype = {
332
349
  */
333
350
  reset: function (scope, callback) {
334
351
  var executeDown = load('down');
352
+ this._begin();
335
353
 
336
354
  if (typeof scope === 'string') {
337
355
  this.internals.migrationMode = scope;
@@ -427,6 +445,7 @@ dbmigrate.prototype = {
427
445
  * or due, see lib/status.js.
428
446
  */
429
447
  status: function (scope, callback) {
448
+ this._begin();
430
449
  if (typeof scope === 'function') {
431
450
  callback = scope;
432
451
  scope = undefined;
package/connect.js CHANGED
@@ -71,9 +71,8 @@ exports.connect = async function (config, PassedClass) {
71
71
  await Promise.promisify(db2.switchDatabase, { context: db2 })(switchTo);
72
72
  }
73
73
 
74
- if (scope) {
75
- internals.locTitle = scope;
76
- }
74
+ // of this run only, an instance of the API runs several
75
+ internals.locTitle = scope || undefined;
77
76
 
78
77
  return new PassedClass(
79
78
  db,
package/index.js CHANGED
@@ -45,7 +45,16 @@ function loadPlugins (options) {
45
45
  var hooks = {};
46
46
 
47
47
  for (; i < length; ++i) {
48
- var plugin = require(path.join(options.cwd, 'node_modules', plugins[i]));
48
+ var plugin;
49
+
50
+ try {
51
+ // resolved like node does from the project, so plugins hoisted in a
52
+ // monorepo are found as well
53
+ plugin = require(require.resolve(plugins[i], { paths: [options.cwd] }));
54
+ } catch (err) {
55
+ log.warn(`Could not load the plugin ${plugins[i]}: ${err.message}`);
56
+ continue;
57
+ }
49
58
 
50
59
  if (!plugin.hooks || !plugin.loadPlugin) {
51
60
  continue;
@@ -23,7 +23,10 @@ async function prepare (internals, config) {
23
23
  Migrator
24
24
  );
25
25
 
26
- migrator.directory = path.resolve(internals.argv['migrations-dir']);
26
+ migrator.directory = path.resolve(
27
+ internals.argv['migrations-dir'],
28
+ internals.locTitle || ''
29
+ );
27
30
 
28
31
  await migrator.createMigrationsTable();
29
32
  log.verbose('migration table created');
package/lib/config.js CHANGED
@@ -97,16 +97,28 @@ exports.loadFile = function (fileName, currentEnv, plugins) {
97
97
  // resolves { "ENV": ... } entries of a config, in place
98
98
  exports.resolveEnv = walkConfig;
99
99
 
100
+ /**
101
+ * The value of { "ENV": "NAME", "default": value }, the default if the
102
+ * variable is not set or empty.
103
+ */
104
+ function fromEnv (entry) {
105
+ var value = process.env[entry.ENV];
106
+
107
+ if (value === undefined || value === '') {
108
+ if (Object.prototype.hasOwnProperty.call(entry, 'default')) {
109
+ return entry.default;
110
+ }
111
+
112
+ log.verbose('Environment variable ' + entry.ENV + ' is empty!');
113
+ }
114
+
115
+ return value;
116
+ }
117
+
100
118
  function walkConfig (level) {
101
119
  for (var configEntry in level) {
102
120
  if (level[configEntry] && level[configEntry].ENV) {
103
- if (!process.env[level[configEntry].ENV]) {
104
- log.verbose(
105
- 'Environment variable ' + level[configEntry].ENV + ' is empty!'
106
- );
107
- }
108
-
109
- level[configEntry] = process.env[level[configEntry].ENV];
121
+ level[configEntry] = fromEnv(level[configEntry]);
110
122
  } else if (level[configEntry] && typeof level[configEntry] === 'object') {
111
123
  level[configEntry] = walkConfig(level[configEntry]);
112
124
  }
@@ -121,11 +133,19 @@ exports.loadObject = function (_config, currentEnv) {
121
133
  var config = JSON.parse(JSON.stringify(_config));
122
134
 
123
135
  for (var env in config) {
136
+ if (env === 'defaultEnv' || env === 'default') {
137
+ continue;
138
+ }
139
+
124
140
  if (config[env].ENV) {
125
- if (!process.env[config[env].ENV]) {
126
- log.verbose('Environment variable ' + config[env].ENV + ' is empty!');
127
- } else {
128
- out[env] = parseDatabaseUrl(process.env[config[env].ENV]);
141
+ // a database url from the environment, the other keys next to it
142
+ // are added to it
143
+ var url = fromEnv(config[env]);
144
+ if (url) {
145
+ var rest = Object.assign({}, config[env]);
146
+ delete rest.ENV;
147
+ delete rest.default;
148
+ out[env] = Object.assign(parseDatabaseUrl(url), walkConfig(rest));
129
149
  }
130
150
  } else if (typeof config[env] === 'string') {
131
151
  out[env] = parseDatabaseUrl(config[env]);
@@ -183,7 +203,8 @@ exports.loadObject = function (_config, currentEnv) {
183
203
  out.setCurrent(config.default);
184
204
  } else if (config.defaultEnv) {
185
205
  if (config.defaultEnv.ENV) {
186
- out.setCurrent(process.env[config.defaultEnv.ENV]);
206
+ // without the variable and a default, the default environments
207
+ out.setCurrent(fromEnv(config.defaultEnv) || ['dev', 'development']);
187
208
  } else {
188
209
  out.setCurrent(config.defaultEnv);
189
210
  }
package/lib/file.js CHANGED
@@ -221,10 +221,25 @@ File.loadFromDatabase = function (dir, prefix, driver, internals) {
221
221
  );
222
222
  })
223
223
  .map(function (result) {
224
- return new File(path.join(dir, result.name), internals);
224
+ // the names are recorded with their scope, dir is the directory of the
225
+ // scope already
226
+ return new File(path.join(rootOf(dir, internals.matching), result.name), internals);
225
227
  });
226
228
  };
227
229
 
230
+ /**
231
+ * The migrations directory of a scope directory.
232
+ */
233
+ function rootOf (dir, scope) {
234
+ const resolved = path.resolve(dir);
235
+
236
+ if (scope && resolved.endsWith(path.sep + path.normalize(scope))) {
237
+ return resolved.slice(0, -(path.normalize(scope).length + 1));
238
+ }
239
+
240
+ return dir;
241
+ }
242
+
228
243
  Promise.promisifyAll(fs);
229
244
 
230
245
  module.exports = File;
package/lib/template.js CHANGED
@@ -105,8 +105,6 @@ Template.prototype.sqlFileLoaderTemplate = function () {
105
105
  ' return new Promise( function( resolve, reject ) {',
106
106
  " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){",
107
107
  ' if (err) return reject(err);',
108
- " console.log('received data: ' + data);",
109
- '',
110
108
  ' resolve(data);',
111
109
  ' });',
112
110
  ' })',
@@ -122,8 +120,6 @@ Template.prototype.sqlFileLoaderTemplate = function () {
122
120
  ' return new Promise( function( resolve, reject ) {',
123
121
  " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){",
124
122
  ' if (err) return reject(err);',
125
- " console.log('received data: ' + data);",
126
- '',
127
123
  ' resolve(data);',
128
124
  ' });',
129
125
  ' })',
@@ -171,8 +167,6 @@ Template.prototype.sqlFileLoaderIgnoreOnInitTemplate = function () {
171
167
  ' return new Promise( function( resolve, reject ) {',
172
168
  " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){",
173
169
  ' if (err) return reject(err);',
174
- " console.log('received data: ' + data);",
175
- '',
176
170
  ' resolve(data);',
177
171
  ' });',
178
172
  ' })',
@@ -181,7 +175,6 @@ Template.prototype.sqlFileLoaderIgnoreOnInitTemplate = function () {
181
175
  ' });',
182
176
  ' }',
183
177
  ' else {',
184
- " console.log('ignoring on init: ' + filePath)",
185
178
  ' return null;',
186
179
  ' }',
187
180
  '};',
@@ -193,8 +186,6 @@ Template.prototype.sqlFileLoaderIgnoreOnInitTemplate = function () {
193
186
  ' return new Promise( function( resolve, reject ) {',
194
187
  " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){",
195
188
  ' if (err) return reject(err);',
196
- " console.log('received data: ' + data);",
197
- '',
198
189
  ' resolve(data);',
199
190
  ' });',
200
191
  ' })',
package/lib/walker.js CHANGED
@@ -48,6 +48,12 @@ const Walker = function (driver, directory, mode, intern, prefix, opts = {}) {
48
48
  * c: schema
49
49
  */
50
50
  this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
51
+ // the state of an earlier run of the same instance of the API, e.g. a
52
+ // down leaves unlearn set, which would keep up from recording anything
53
+ this.internals.unlearn = false;
54
+ this.internals.rollback = false;
55
+ this.internals.rollbackContinue = false;
56
+ this.internals.learnFromScratch = false;
51
57
  this.mode = mode;
52
58
 
53
59
  if (!this.mode) this.prefix = `static-${prefix}`;
package/package.json CHANGED
@@ -31,7 +31,7 @@
31
31
  "programmable",
32
32
  "api"
33
33
  ],
34
- "version": "1.7.0",
34
+ "version": "1.8.0",
35
35
  "bugs": {
36
36
  "url": "https://github.com/db-migrate/node-db-migrate/issues"
37
37
  },