knex-migrator 4.1.0 → 4.1.1

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.
Files changed (2) hide show
  1. package/lib/index.js +103 -129
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -286,11 +286,10 @@ KnexMigrator.prototype.init = function init(options) {
286
286
  * @param {Object} options - Custom options you can pass in (version, force, init, only, skip)
287
287
  * @returns {Bluebird<any>}
288
288
  */
289
- KnexMigrator.prototype.migrate = function migrate(options) {
289
+ KnexMigrator.prototype.migrate = async function migrate(options) {
290
290
  options = options || {};
291
291
 
292
- let self = this,
293
- onlyVersion = options.version,
292
+ let onlyVersion = options.version,
294
293
  force = options.force,
295
294
  init = options.init,
296
295
  onlyFile = options.only,
@@ -308,14 +307,11 @@ KnexMigrator.prototype.migrate = function migrate(options) {
308
307
 
309
308
  // CASE: `--init` flag is passed. Combo feature.
310
309
  if (init) {
311
- return this.init()
312
- .then(function () {
313
- return self.migrate(_.omit(options, 'init'));
314
- });
310
+ await this.init();
315
311
  }
316
312
 
317
313
  try {
318
- hooks = require(path.join(self.migrationPath, '/hooks/migrate'));
314
+ hooks = require(path.join(this.migrationPath, '/hooks/migrate'));
319
315
  } catch (err) {
320
316
  debug('Hook Error: ' + err.message);
321
317
  debug('No hooks found, no problem.');
@@ -323,152 +319,130 @@ KnexMigrator.prototype.migrate = function migrate(options) {
323
319
 
324
320
  this.connection = database.connect(this.dbConfig);
325
321
 
326
- return database.ensureConnectionWorks(this.connection)
327
- .then(function () {
328
- return migrations.run(self.connection);
329
- })
330
- .then(function () {
331
- return locking.lock(self.connection);
332
- })
333
- .then(function () {
334
- return self._integrityCheck({
335
- force: force
336
- });
337
- })
338
- .then(function (result) {
339
- _.each(result, function (value, version) {
340
- // CASE: Log which versions won't be executed based on the "only" flag
341
- if (onlyVersion && version !== onlyVersion) {
342
- debug('Do not execute: ' + version);
343
- return;
344
- }
345
- });
322
+ try {
323
+ await database.ensureConnectionWorks(this.connection);
346
324
 
347
- if (onlyVersion) {
348
- // CASE: filter out versions which should not run
349
- let containsVersion = _.find(result, function (obj, key) {
350
- return key === onlyVersion;
351
- });
325
+ await migrations.run(this.connection);
352
326
 
353
- if (!containsVersion) {
354
- logging.warn('Cannot find requested version: ' + onlyVersion);
355
- }
327
+ await locking.lock(this.connection);
328
+
329
+ const result = await this._integrityCheck({force});
330
+
331
+ _.each(result, function (_value, version) {
332
+ // CASE: Log which versions won't be executed based on the "only" flag
333
+ if (onlyVersion && version !== onlyVersion) {
334
+ debug('Do not execute: ' + version);
335
+ return;
356
336
  }
337
+ });
357
338
 
358
- _.each(result, function (value, version) {
359
- // CASE: compare files on disk with files in database
360
- if (value.expected !== value.actual) {
361
- debug('Need to execute migrations for: ' + version);
362
- versionsToMigrate.push(version);
363
- }
339
+ if (onlyVersion) {
340
+ // CASE: filter out versions which should not run
341
+ let containsVersion = _.find(result, function (_obj, key) {
342
+ return key === onlyVersion;
364
343
  });
365
- })
366
- .then(function executeBeforeHook() {
367
- if (!versionsToMigrate.length) {
368
- return;
344
+
345
+ if (!containsVersion) {
346
+ logging.warn('Cannot find requested version: ' + onlyVersion);
347
+ }
348
+ }
349
+
350
+ _.each(result, function (value, version) {
351
+ // CASE: compare files on disk with files in database
352
+ if (value.expected !== value.actual) {
353
+ debug('Need to execute migrations for: ' + version);
354
+ versionsToMigrate.push(version);
369
355
  }
356
+ });
370
357
 
358
+ if (versionsToMigrate.length) {
371
359
  if (hooks.before) {
372
360
  debug('Before hook');
373
- return hooks.before({
374
- connection: self.connection
361
+ await hooks.before({
362
+ connection: this.connection
375
363
  });
376
364
  }
377
- })
378
- .then(function executeMigrations() {
379
- if (!versionsToMigrate.length) {
380
- return;
381
- }
382
365
 
383
- return Promise.each(versionsToMigrate, function (versionToMigrate) {
384
- return self._migrateTo({
385
- version: versionToMigrate,
386
- only: onlyFile,
387
- hooks: hooks
388
- });
389
- });
390
- })
391
- .then(function executeAfterHook() {
392
- if (!versionsToMigrate.length) {
393
- return;
394
- }
366
+ logging.info('Running migrations.');
395
367
 
396
- if (hooks.after) {
397
- debug('After hook');
398
- return hooks.after({
399
- connection: self.connection
400
- });
401
- }
402
- })
403
- .then(function () {
404
- return locking.unlock(self.connection);
405
- })
406
- .catch(function (err) {
407
- // CASE: Do not rollback if migrations are locked
408
- if (err instanceof errors.MigrationsAreLockedError) {
409
- throw err;
410
- }
368
+ for (const versionToMigrate of versionsToMigrate) {
369
+ try {
370
+ await this._migrateTo({
371
+ version: versionToMigrate,
372
+ only: onlyFile,
373
+ hooks: hooks
374
+ });
375
+ } catch (err) {
376
+ // CASE: Do not rollback if migrations are locked
377
+ if (err instanceof errors.MigrationsAreLockedError) {
378
+ throw err;
379
+ }
411
380
 
412
- // CASE: Do not rollback migration scripts, if lock error
413
- if (err instanceof errors.LockError) {
414
- throw err;
415
- }
381
+ // CASE: Do not rollback migration scripts, if lock error
382
+ if (err instanceof errors.LockError) {
383
+ throw err;
384
+ }
416
385
 
417
- // CASE: ETIMEDOUT, ENOTFOUND
418
- if (err instanceof errors.DatabaseError) {
419
- throw err;
420
- }
386
+ // CASE: ETIMEDOUT, ENOTFOUND
387
+ if (err instanceof errors.DatabaseError) {
388
+ throw err;
389
+ }
421
390
 
422
- if (err.context && err.context.name) {
423
- debug(`Task failed: ${err.context.name}`);
424
- }
391
+ if (err.context && err.context.name) {
392
+ debug(`Task failed: ${err.context.name}`);
393
+ }
425
394
 
426
- debug(`Rolling back: ${err.message}`);
395
+ logging.info(`Rolling back: ${err.message}.`);
427
396
 
428
- versionsToMigrate.reverse();
397
+ const versionsMigrated = versionsToMigrate.slice(
398
+ 0,
399
+ versionsToMigrate.indexOf(versionToMigrate) + 1
400
+ );
429
401
 
430
- // CASE: rollback in reversed order
431
- return Promise.each(versionsToMigrate, function (version) {
432
- return self._rollback({version: version, task: err.context});
433
- }).then(function () {
434
- throw err;
435
- }).catch(function (innerErr) {
436
- if (errors.utils.isIgnitionError(innerErr)) {
437
- throw err;
438
- }
402
+ versionsMigrated.reverse();
439
403
 
440
- throw new errors.RollbackError({
441
- message: innerErr.message,
442
- err: innerErr,
443
- context: `OuterError: ${err.message}`
444
- });
445
- }).finally(function () {
446
- return locking.unlock(self.connection);
447
- });
448
- }).finally(function () {
449
- let ops = [];
404
+ try {
405
+ for (const versionMigrated of versionsMigrated) {
406
+ await this._rollback({version: versionMigrated, task: err.context});
407
+ }
408
+ logging.info(`Rollback was successful.`);
409
+ throw err;
410
+ } catch (innerErr) {
411
+ if (errors.utils.isIgnitionError(innerErr)) {
412
+ throw err;
413
+ }
414
+
415
+ throw new errors.RollbackError({
416
+ message: innerErr.message,
417
+ err: innerErr,
418
+ context: `OuterError: ${err.message}`
419
+ });
420
+ } finally {
421
+ await locking.unlock(this.connection);
422
+ }
423
+ }
424
+ }
450
425
 
451
- if (hooks.shutdown) {
452
- ops.push(function shutdownHook() {
453
- debug('Shutdown hook');
454
- return hooks.shutdown({
455
- executedFromShell: self.executedFromShell
456
- });
426
+ if (hooks.after) {
427
+ debug('After hook');
428
+ await hooks.after({
429
+ connection: this.connection
457
430
  });
458
431
  }
459
-
460
- ops.push(function destroyConnection() {
461
- debug('Destroy connection');
462
- return self.connection.destroy()
463
- .then(function () {
464
- debug('Destroyed connection');
465
- });
432
+ }
433
+ await locking.unlock(this.connection);
434
+ } finally {
435
+ if (hooks.shutdown) {
436
+ debug('Shutdown hook');
437
+ await hooks.shutdown({
438
+ executedFromShell: this.executedFromShell
466
439
  });
440
+ }
467
441
 
468
- return Promise.each(ops, function (op) {
469
- return op.bind(self)();
470
- });
471
- });
442
+ debug('Destroy connection');
443
+ await this.connection.destroy();
444
+ debug('Destroyed connection');
445
+ }
472
446
  };
473
447
 
474
448
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knex-migrator",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "Database migrations with knex.",
5
5
  "keywords": [
6
6
  "ghost",