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.
- package/lib/index.js +103 -129
- 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
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
327
|
-
.
|
|
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
|
-
|
|
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
|
-
|
|
354
|
-
|
|
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
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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
|
-
|
|
374
|
-
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
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
381
|
+
// CASE: Do not rollback migration scripts, if lock error
|
|
382
|
+
if (err instanceof errors.LockError) {
|
|
383
|
+
throw err;
|
|
384
|
+
}
|
|
416
385
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
386
|
+
// CASE: ETIMEDOUT, ENOTFOUND
|
|
387
|
+
if (err instanceof errors.DatabaseError) {
|
|
388
|
+
throw err;
|
|
389
|
+
}
|
|
421
390
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
391
|
+
if (err.context && err.context.name) {
|
|
392
|
+
debug(`Task failed: ${err.context.name}`);
|
|
393
|
+
}
|
|
425
394
|
|
|
426
|
-
|
|
395
|
+
logging.info(`Rolling back: ${err.message}.`);
|
|
427
396
|
|
|
428
|
-
|
|
397
|
+
const versionsMigrated = versionsToMigrate.slice(
|
|
398
|
+
0,
|
|
399
|
+
versionsToMigrate.indexOf(versionToMigrate) + 1
|
|
400
|
+
);
|
|
429
401
|
|
|
430
|
-
|
|
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
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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.
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
442
|
+
debug('Destroy connection');
|
|
443
|
+
await this.connection.destroy();
|
|
444
|
+
debug('Destroyed connection');
|
|
445
|
+
}
|
|
472
446
|
};
|
|
473
447
|
|
|
474
448
|
/**
|