/
Migrator.js
597 lines (534 loc) · 17.7 KB
/
Migrator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Migrator
// -------
const differenceWith = require('lodash/differenceWith');
const get = require('lodash/get');
const isEmpty = require('lodash/isEmpty');
const max = require('lodash/max');
const {
getLockTableName,
getTable,
getTableName,
} = require('./table-resolver');
const { getSchemaBuilder } = require('./table-creator');
const migrationListResolver = require('./migration-list-resolver');
const MigrationGenerator = require('./MigrationGenerator');
const { getMergedConfig } = require('./migrator-configuration-merger');
const { isBoolean, isFunction } = require('../../util/is');
class LockError extends Error {
constructor(msg) {
super(msg);
this.name = 'MigrationLocked';
}
}
// The new migration we're performing, typically called from the `knex.migrate`
// interface on the main `knex` object. Passes the `knex` instance performing
// the migration.
class Migrator {
constructor(knex) {
// Clone knex instance and remove post-processing that is unnecessary for internal queries from a cloned config
if (isFunction(knex)) {
if (!knex.isTransaction) {
this.knex = knex.withUserParams({
...knex.userParams,
});
} else {
this.knex = knex;
}
} else {
this.knex = Object.assign({}, knex);
this.knex.userParams = this.knex.userParams || {};
}
this.config = getMergedConfig(
this.knex.client.config.migrations,
undefined,
this.knex.client.logger
);
this.generator = new MigrationGenerator(
this.knex.client.config.migrations,
this.knex.client.logger
);
this._activeMigration = {
fileName: null,
};
}
// Migrators to the latest configuration.
async latest(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
const allAndCompleted = await migrationListResolver.listAllAndCompleted(
this.config,
this.knex
);
if (!this.config.disableMigrationsListValidation) {
validateMigrationList(this.config.migrationSource, allAndCompleted);
}
const [all, completed] = allAndCompleted;
const migrations = getNewMigrations(
this.config.migrationSource,
all,
completed
);
const transactionForAll =
!this.config.disableTransactions &&
!(
await Promise.all(
migrations.map(async (migration) => {
const migrationContents =
await this.config.migrationSource.getMigration(migration);
return !this._useTransaction(migrationContents);
})
)
).some((isTransactionUsed) => isTransactionUsed);
if (transactionForAll) {
return this.knex.transaction((trx) => {
return this._runBatch(migrations, 'up', trx);
});
} else {
return this._runBatch(migrations, 'up');
}
}
// Runs the next migration that has not yet been run
async up(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
const allAndCompleted = await migrationListResolver.listAllAndCompleted(
this.config,
this.knex
);
if (!this.config.disableMigrationsListValidation) {
validateMigrationList(this.config.migrationSource, allAndCompleted);
}
const [all, completed] = allAndCompleted;
const newMigrations = getNewMigrations(
this.config.migrationSource,
all,
completed
);
let migrationToRun;
const name = this.config.name;
if (name) {
if (!completed.includes(name)) {
migrationToRun = newMigrations.find((migration) => {
return (
this.config.migrationSource.getMigrationName(migration) === name
);
});
if (!migrationToRun) {
throw new Error(`Migration "${name}" not found.`);
}
}
} else {
migrationToRun = newMigrations[0];
}
const useTransaction =
!migrationToRun ||
this._useTransaction(
await this.config.migrationSource.getMigration(migrationToRun)
);
const migrationsToRun = [];
if (migrationToRun) {
migrationsToRun.push(migrationToRun);
}
const transactionForAll =
!this.config.disableTransactions && (!migrationToRun || useTransaction);
if (transactionForAll) {
return await this.knex.transaction((trx) => {
return this._runBatch(migrationsToRun, 'up', trx);
});
} else {
return await this._runBatch(migrationsToRun, 'up');
}
}
// Rollback the last "batch", or all, of migrations that were run.
rollback(config, all = false) {
this._disableProcessing();
return new Promise((resolve, reject) => {
try {
this.config = getMergedConfig(
config,
this.config,
this.knex.client.logger
);
} catch (e) {
reject(e);
}
migrationListResolver
.listAllAndCompleted(this.config, this.knex)
.then((value) => {
if (!this.config.disableMigrationsListValidation) {
validateMigrationList(this.config.migrationSource, value);
}
return value;
})
.then((val) => {
const [allMigrations, completedMigrations] = val;
return all
? allMigrations
.filter((migration) => {
return completedMigrations
.map((migration) => migration.name)
.includes(
this.config.migrationSource.getMigrationName(migration)
);
})
.reverse()
: this._getLastBatch(val);
})
.then((migrations) => {
return this._runBatch(migrations, 'down');
})
.then(resolve, reject);
});
}
down(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
return migrationListResolver
.listAllAndCompleted(this.config, this.knex)
.then((value) => {
if (!this.config.disableMigrationsListValidation) {
validateMigrationList(this.config.migrationSource, value);
}
return value;
})
.then(([all, completed]) => {
const completedMigrations = all.filter((migration) => {
return completed
.map((migration) => migration.name)
.includes(this.config.migrationSource.getMigrationName(migration));
});
let migrationToRun;
const name = this.config.name;
if (name) {
migrationToRun = completedMigrations.find((migration) => {
return (
this.config.migrationSource.getMigrationName(migration) === name
);
});
if (!migrationToRun) {
throw new Error(`Migration "${name}" was not run.`);
}
} else {
migrationToRun = completedMigrations[completedMigrations.length - 1];
}
const migrationsToRun = [];
if (migrationToRun) {
migrationsToRun.push(migrationToRun);
}
return this._runBatch(migrationsToRun, 'down');
});
}
status(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
return Promise.all([
getTable(this.knex, this.config.tableName, this.config.schemaName).select(
'*'
),
migrationListResolver.listAll(this.config.migrationSource),
]).then(([db, code]) => db.length - code.length);
}
// Retrieves and returns the current migration version we're on, as a promise.
// If no migrations have been run yet, return "none".
currentVersion(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
return migrationListResolver
.listCompleted(this.config.tableName, this.config.schemaName, this.knex)
.then((completed) => {
const val = max(completed.map((value) => value.name.split('_')[0]));
return val === undefined ? 'none' : val;
});
}
// list all migrations
async list(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
const [all, completed] = await migrationListResolver.listAllAndCompleted(
this.config,
this.knex
);
if (!this.config.disableMigrationsListValidation) {
validateMigrationList(this.config.migrationSource, [all, completed]);
}
const newMigrations = getNewMigrations(
this.config.migrationSource,
all,
completed
);
return [completed, newMigrations];
}
async forceFreeMigrationsLock(config) {
this.config = getMergedConfig(config, this.config, this.knex.client.logger);
const { schemaName, tableName } = this.config;
const lockTableName = getLockTableName(tableName);
const { knex } = this;
const getLockTable = () => getTable(knex, lockTableName, schemaName);
const tableExists = await getSchemaBuilder(knex, schemaName).hasTable(
lockTableName
);
if (tableExists) {
await getLockTable().del();
await getLockTable().insert({
is_locked: 0,
});
}
}
// Creates a new migration, with a given name.
make(name, config) {
return this.generator.make(name, config, this.knex.client.logger);
}
_disableProcessing() {
if (this.knex.disableProcessing) {
this.knex.disableProcessing();
}
}
_lockMigrations(trx) {
const tableName = getLockTableName(this.config.tableName);
return getTable(this.knex, tableName, this.config.schemaName)
.transacting(trx)
.where('is_locked', '=', 0)
.update({ is_locked: 1 })
.then((rowCount) => {
if (rowCount !== 1) {
throw new Error('Migration table is already locked');
}
});
}
_getLock(trx) {
const transact = trx ? (fn) => fn(trx) : (fn) => this.knex.transaction(fn);
return transact((trx) => {
return this._lockMigrations(trx);
}).catch((err) => {
throw new LockError(err.message);
});
}
_freeLock(trx = this.knex) {
const tableName = getLockTableName(this.config.tableName);
return getTable(trx, tableName, this.config.schemaName).update({
is_locked: 0,
});
}
// Run a batch of current migrations, in sequence.
async _runBatch(migrations, direction, trx) {
const canGetLockInTransaction =
this.knex.client.driverName !== 'cockroachdb';
try {
await this._getLock(canGetLockInTransaction ? trx : undefined);
// When there is a wrapping transaction, some migrations
// could have been done while waiting for the lock:
const completed = trx
? await migrationListResolver.listCompleted(
this.config.tableName,
this.config.schemaName,
trx
)
: [];
migrations = getNewMigrations(
this.config.migrationSource,
migrations,
completed
);
await Promise.all(
migrations.map(this._validateMigrationStructure.bind(this))
);
let batchNo = await this._latestBatchNumber(trx);
if (direction === 'up') batchNo++;
const res = await this._waterfallBatch(
batchNo,
migrations,
direction,
trx
);
await this._freeLock(canGetLockInTransaction ? trx : undefined);
return res;
} catch (error) {
let cleanupReady = Promise.resolve();
if (error instanceof LockError) {
// If locking error do not free the lock.
this.knex.client.logger.warn(
`Can't take lock to run migrations: ${error.message}`
);
this.knex.client.logger.warn(
'If you are sure migrations are not running you can release the ' +
"lock manually by running 'knex migrate:unlock'"
);
} else {
if (this._activeMigration.fileName) {
this.knex.client.logger.warn(
`migration file "${this._activeMigration.fileName}" failed`
);
}
this.knex.client.logger.warn(
`migration failed with error: ${error.message}`
);
// If the error was not due to a locking issue, then remove the lock.
cleanupReady = this._freeLock(
canGetLockInTransaction ? trx : undefined
);
}
try {
await cleanupReady;
// eslint-disable-next-line no-empty
} catch (e) {}
throw error;
}
}
// Validates some migrations by requiring and checking for an `up` and `down`
// function.
async _validateMigrationStructure(migration) {
const migrationName =
this.config.migrationSource.getMigrationName(migration);
// maybe promise
const migrationContent = await this.config.migrationSource.getMigration(
migration
);
if (
typeof migrationContent.up !== 'function' ||
typeof migrationContent.down !== 'function'
) {
throw new Error(
`Invalid migration: ${migrationName} must have both an up and down function`
);
}
return migration;
}
// Get the last batch of migrations, by name, ordered by insert id in reverse
// order.
async _getLastBatch([allMigrations]) {
const { tableName, schemaName } = this.config;
const migrationNames = await getTable(this.knex, tableName, schemaName)
.where('batch', function (qb) {
qb.max('batch').from(getTableName(tableName, schemaName));
})
.orderBy('id', 'desc');
const lastBatchMigrations = migrationNames.map((migration) => {
return allMigrations.find((entry) => {
return (
this.config.migrationSource.getMigrationName(entry) === migration.name
);
});
});
return Promise.all(lastBatchMigrations);
}
// Returns the latest batch number.
_latestBatchNumber(trx = this.knex) {
return trx
.from(getTableName(this.config.tableName, this.config.schemaName))
.max('batch as max_batch')
.then((obj) => obj[0].max_batch || 0);
}
// If transaction config for a single migration is defined, use that.
// Otherwise, rely on the common config. This allows enabling/disabling
// transaction for a single migration at will, regardless of the common
// config.
_useTransaction(migrationContent, allTransactionsDisabled) {
const singleTransactionValue = get(migrationContent, 'config.transaction');
return isBoolean(singleTransactionValue)
? singleTransactionValue
: !allTransactionsDisabled;
}
// Runs a batch of `migrations` in a specified `direction`, saving the
// appropriate database information as the migrations are run.
_waterfallBatch(batchNo, migrations, direction, trx) {
const trxOrKnex = trx || this.knex;
const { tableName, schemaName, disableTransactions } = this.config;
let current = Promise.resolve();
const log = [];
migrations.forEach((migration) => {
const name = this.config.migrationSource.getMigrationName(migration);
this._activeMigration.fileName = name;
const migrationContent =
this.config.migrationSource.getMigration(migration);
// We're going to run each of the migrations in the current "up".
current = current
.then(async () => await migrationContent) //maybe promise
.then((migrationContent) => {
this._activeMigration.fileName = name;
if (
!trx &&
this._useTransaction(migrationContent, disableTransactions)
) {
this.knex.enableProcessing();
return this._transaction(
this.knex,
migrationContent,
direction,
name
);
}
trxOrKnex.enableProcessing();
return checkPromise(
this.knex.client.logger,
migrationContent[direction](trxOrKnex),
name
);
})
.then(() => {
trxOrKnex.disableProcessing();
this.knex.disableProcessing();
log.push(name);
if (direction === 'up') {
return trxOrKnex.into(getTableName(tableName, schemaName)).insert({
name,
batch: batchNo,
migration_time: new Date(),
});
}
if (direction === 'down') {
return trxOrKnex
.from(getTableName(tableName, schemaName))
.where({ name })
.del();
}
});
});
return current.then(() => [batchNo, log]);
}
_transaction(knex, migrationContent, direction, name) {
return knex.transaction((trx) => {
return checkPromise(
knex.client.logger,
migrationContent[direction](trx),
name,
() => {
trx.commit();
}
);
});
}
}
// Validates that migrations are present in the appropriate directories.
function validateMigrationList(migrationSource, migrations) {
const [all, completed] = migrations;
const diff = getMissingMigrations(migrationSource, completed, all);
if (!isEmpty(diff)) {
throw new Error(
`The migration directory is corrupt, the following files are missing: ${diff.join(
', '
)}`
);
}
}
function getMissingMigrations(migrationSource, completed, all) {
return differenceWith(completed, all, (c, a) => {
return c.name === migrationSource.getMigrationName(a);
});
}
function getNewMigrations(migrationSource, all, completed) {
return differenceWith(all, completed, (a, c) => {
return c.name === migrationSource.getMigrationName(a);
});
}
function checkPromise(logger, migrationPromise, name, commitFn) {
if (!migrationPromise || typeof migrationPromise.then !== 'function') {
logger.warn(`migration ${name} did not return a promise`);
if (commitFn) {
commitFn();
}
}
return migrationPromise;
}
module.exports = {
Migrator,
};