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