rake-db 2.2.1 → 2.2.4
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/CHANGELOG.md +25 -0
- package/db.ts +8 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.esm.js +172 -120
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +172 -119
- package/dist/index.js.map +1 -1
- package/migrations/20221017200326_createChatUser.ts +3 -0
- package/package.json +2 -2
- package/src/commands/migrateOrRollback.test.ts +86 -12
- package/src/commands/migrateOrRollback.ts +23 -4
- package/src/common.ts +7 -1
- package/src/migration/changeTable.ts +2 -1
- package/src/migration/createJoinTable.ts +4 -0
- package/src/migration/createTable.ts +2 -1
- package/src/migration/migration.ts +14 -3
- package/src/rakeDb.ts +19 -6
- package/src/test-utils.ts +15 -7
package/dist/index.js
CHANGED
|
@@ -13,21 +13,21 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
13
13
|
var Enquirer__default = /*#__PURE__*/_interopDefaultLegacy(Enquirer);
|
|
14
14
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
15
15
|
|
|
16
|
-
var __defProp$
|
|
16
|
+
var __defProp$6 = Object.defineProperty;
|
|
17
17
|
var __defProps$5 = Object.defineProperties;
|
|
18
18
|
var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
|
|
19
|
-
var __getOwnPropSymbols$
|
|
20
|
-
var __hasOwnProp$
|
|
21
|
-
var __propIsEnum$
|
|
22
|
-
var __defNormalProp$
|
|
23
|
-
var __spreadValues$
|
|
19
|
+
var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
|
|
20
|
+
var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
|
|
21
|
+
var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
|
|
22
|
+
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
23
|
+
var __spreadValues$6 = (a, b) => {
|
|
24
24
|
for (var prop in b || (b = {}))
|
|
25
|
-
if (__hasOwnProp$
|
|
26
|
-
__defNormalProp$
|
|
27
|
-
if (__getOwnPropSymbols$
|
|
28
|
-
for (var prop of __getOwnPropSymbols$
|
|
29
|
-
if (__propIsEnum$
|
|
30
|
-
__defNormalProp$
|
|
25
|
+
if (__hasOwnProp$6.call(b, prop))
|
|
26
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
27
|
+
if (__getOwnPropSymbols$6)
|
|
28
|
+
for (var prop of __getOwnPropSymbols$6(b)) {
|
|
29
|
+
if (__propIsEnum$6.call(b, prop))
|
|
30
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
31
31
|
}
|
|
32
32
|
return a;
|
|
33
33
|
};
|
|
@@ -37,10 +37,11 @@ const migrationConfigDefaults = {
|
|
|
37
37
|
migrationsTable: "schemaMigrations",
|
|
38
38
|
requireTs: require,
|
|
39
39
|
log: true,
|
|
40
|
-
logger: console
|
|
40
|
+
logger: console,
|
|
41
|
+
useCodeUpdaterByDefault: true
|
|
41
42
|
};
|
|
42
43
|
const getMigrationConfigWithDefaults = (config) => {
|
|
43
|
-
return __spreadValues$
|
|
44
|
+
return __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
|
|
44
45
|
};
|
|
45
46
|
const getDatabaseAndUserFromOptions = (options) => {
|
|
46
47
|
if (options.databaseURL) {
|
|
@@ -68,9 +69,9 @@ const setAdapterOptions = (options, set) => {
|
|
|
68
69
|
if (set.password !== void 0) {
|
|
69
70
|
url.password = set.password;
|
|
70
71
|
}
|
|
71
|
-
return __spreadProps$5(__spreadValues$
|
|
72
|
+
return __spreadProps$5(__spreadValues$6({}, options), { databaseURL: url.toString() });
|
|
72
73
|
} else {
|
|
73
|
-
return __spreadValues$
|
|
74
|
+
return __spreadValues$6(__spreadValues$6({}, options), set);
|
|
74
75
|
}
|
|
75
76
|
};
|
|
76
77
|
const askAdminCredentials = async () => {
|
|
@@ -209,21 +210,21 @@ const setCurrentMigrationUp = (up) => {
|
|
|
209
210
|
const getCurrentPromise = () => currentPromise;
|
|
210
211
|
const getCurrentChangeCallback = () => currentChangeCallback;
|
|
211
212
|
|
|
212
|
-
var __defProp$
|
|
213
|
+
var __defProp$5 = Object.defineProperty;
|
|
213
214
|
var __defProps$4 = Object.defineProperties;
|
|
214
215
|
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
215
|
-
var __getOwnPropSymbols$
|
|
216
|
-
var __hasOwnProp$
|
|
217
|
-
var __propIsEnum$
|
|
218
|
-
var __defNormalProp$
|
|
219
|
-
var __spreadValues$
|
|
216
|
+
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
217
|
+
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
218
|
+
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
219
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
220
|
+
var __spreadValues$5 = (a, b) => {
|
|
220
221
|
for (var prop in b || (b = {}))
|
|
221
|
-
if (__hasOwnProp$
|
|
222
|
-
__defNormalProp$
|
|
223
|
-
if (__getOwnPropSymbols$
|
|
224
|
-
for (var prop of __getOwnPropSymbols$
|
|
225
|
-
if (__propIsEnum$
|
|
226
|
-
__defNormalProp$
|
|
222
|
+
if (__hasOwnProp$5.call(b, prop))
|
|
223
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
224
|
+
if (__getOwnPropSymbols$5)
|
|
225
|
+
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
226
|
+
if (__propIsEnum$5.call(b, prop))
|
|
227
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
227
228
|
}
|
|
228
229
|
return a;
|
|
229
230
|
};
|
|
@@ -264,7 +265,7 @@ const addColumnIndex = (indexes, key, item) => {
|
|
|
264
265
|
if (item.data) {
|
|
265
266
|
if (item.data.index) {
|
|
266
267
|
indexes.push({
|
|
267
|
-
columns: [__spreadProps$4(__spreadValues$
|
|
268
|
+
columns: [__spreadProps$4(__spreadValues$5({}, item.data.index), { column: key })],
|
|
268
269
|
options: item.data.index
|
|
269
270
|
});
|
|
270
271
|
}
|
|
@@ -400,21 +401,21 @@ WHERE
|
|
|
400
401
|
return rows;
|
|
401
402
|
};
|
|
402
403
|
|
|
403
|
-
var __defProp$
|
|
404
|
+
var __defProp$4 = Object.defineProperty;
|
|
404
405
|
var __defProps$3 = Object.defineProperties;
|
|
405
406
|
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
406
|
-
var __getOwnPropSymbols$
|
|
407
|
-
var __hasOwnProp$
|
|
408
|
-
var __propIsEnum$
|
|
409
|
-
var __defNormalProp$
|
|
410
|
-
var __spreadValues$
|
|
407
|
+
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
408
|
+
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
409
|
+
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
410
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
411
|
+
var __spreadValues$4 = (a, b) => {
|
|
411
412
|
for (var prop in b || (b = {}))
|
|
412
|
-
if (__hasOwnProp$
|
|
413
|
-
__defNormalProp$
|
|
414
|
-
if (__getOwnPropSymbols$
|
|
415
|
-
for (var prop of __getOwnPropSymbols$
|
|
416
|
-
if (__propIsEnum$
|
|
417
|
-
__defNormalProp$
|
|
413
|
+
if (__hasOwnProp$4.call(b, prop))
|
|
414
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
415
|
+
if (__getOwnPropSymbols$4)
|
|
416
|
+
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
417
|
+
if (__propIsEnum$4.call(b, prop))
|
|
418
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
418
419
|
}
|
|
419
420
|
return a;
|
|
420
421
|
};
|
|
@@ -423,7 +424,6 @@ const types = Object.assign(Object.create(pqb.columnTypes), {
|
|
|
423
424
|
raw: pqb.raw
|
|
424
425
|
});
|
|
425
426
|
const createTable = async (migration, up, tableName, options, fn) => {
|
|
426
|
-
var _a, _b;
|
|
427
427
|
const shape = pqb.getColumnTypes(types, fn);
|
|
428
428
|
const tableData = pqb.getTableData();
|
|
429
429
|
const ast = makeAst$1(
|
|
@@ -439,7 +439,7 @@ const createTable = async (migration, up, tableName, options, fn) => {
|
|
|
439
439
|
for (const query of queries) {
|
|
440
440
|
await migration.query(query);
|
|
441
441
|
}
|
|
442
|
-
await (
|
|
442
|
+
await runCodeUpdater(migration, ast);
|
|
443
443
|
};
|
|
444
444
|
const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
|
|
445
445
|
const shapePKeys = [];
|
|
@@ -449,13 +449,13 @@ const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
|
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
const primaryKey = tableData.primaryKey;
|
|
452
|
-
return __spreadProps$3(__spreadValues$
|
|
452
|
+
return __spreadProps$3(__spreadValues$4(__spreadProps$3(__spreadValues$4({
|
|
453
453
|
type: "table",
|
|
454
454
|
action: up ? "create" : "drop",
|
|
455
455
|
name: tableName,
|
|
456
456
|
shape
|
|
457
457
|
}, tableData), {
|
|
458
|
-
primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$
|
|
458
|
+
primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$4({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
|
|
459
459
|
}), options), {
|
|
460
460
|
noPrimaryKey: options.noPrimaryKey ? "ignore" : noPrimaryKey || "error"
|
|
461
461
|
});
|
|
@@ -529,21 +529,21 @@ const astToQueries$1 = (ast) => {
|
|
|
529
529
|
return result;
|
|
530
530
|
};
|
|
531
531
|
|
|
532
|
-
var __defProp$
|
|
532
|
+
var __defProp$3 = Object.defineProperty;
|
|
533
533
|
var __defProps$2 = Object.defineProperties;
|
|
534
534
|
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
535
|
-
var __getOwnPropSymbols$
|
|
536
|
-
var __hasOwnProp$
|
|
537
|
-
var __propIsEnum$
|
|
538
|
-
var __defNormalProp$
|
|
539
|
-
var __spreadValues$
|
|
535
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
536
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
537
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
538
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
539
|
+
var __spreadValues$3 = (a, b) => {
|
|
540
540
|
for (var prop in b || (b = {}))
|
|
541
|
-
if (__hasOwnProp$
|
|
542
|
-
__defNormalProp$
|
|
543
|
-
if (__getOwnPropSymbols$
|
|
544
|
-
for (var prop of __getOwnPropSymbols$
|
|
545
|
-
if (__propIsEnum$
|
|
546
|
-
__defNormalProp$
|
|
541
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
542
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
543
|
+
if (__getOwnPropSymbols$3)
|
|
544
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
545
|
+
if (__propIsEnum$3.call(b, prop))
|
|
546
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
547
547
|
}
|
|
548
548
|
return a;
|
|
549
549
|
};
|
|
@@ -563,7 +563,7 @@ const mergeTableData = (a, b) => {
|
|
|
563
563
|
} else {
|
|
564
564
|
a.primaryKey = {
|
|
565
565
|
columns: [...a.primaryKey.columns, ...b.primaryKey.columns],
|
|
566
|
-
options: __spreadValues$
|
|
566
|
+
options: __spreadValues$3(__spreadValues$3({}, a.primaryKey.options), b.primaryKey.options)
|
|
567
567
|
};
|
|
568
568
|
}
|
|
569
569
|
}
|
|
@@ -614,7 +614,7 @@ const columnTypeToColumnChange = (item) => {
|
|
|
614
614
|
if (foreignKey && "fn" in foreignKey) {
|
|
615
615
|
throw new Error("Callback in foreignKey is not allowed in migration");
|
|
616
616
|
}
|
|
617
|
-
return __spreadProps$2(__spreadValues$
|
|
617
|
+
return __spreadProps$2(__spreadValues$3({
|
|
618
618
|
column: item,
|
|
619
619
|
type: item.toSQL(),
|
|
620
620
|
nullable: item.isNullable,
|
|
@@ -630,7 +630,7 @@ const tableChangeMethods = {
|
|
|
630
630
|
add,
|
|
631
631
|
drop,
|
|
632
632
|
change(from, to, options) {
|
|
633
|
-
return __spreadValues$
|
|
633
|
+
return __spreadValues$3({
|
|
634
634
|
type: "change",
|
|
635
635
|
from: columnTypeToColumnChange(from),
|
|
636
636
|
to: columnTypeToColumnChange(to)
|
|
@@ -661,7 +661,6 @@ const tableChangeMethods = {
|
|
|
661
661
|
}
|
|
662
662
|
};
|
|
663
663
|
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
664
|
-
var _a, _b;
|
|
665
664
|
pqb.resetTableData();
|
|
666
665
|
resetChangeTableData();
|
|
667
666
|
const tableChanger = Object.create(pqb.columnTypes);
|
|
@@ -672,7 +671,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
672
671
|
for (const query of queries) {
|
|
673
672
|
await migration.query(query);
|
|
674
673
|
}
|
|
675
|
-
await (
|
|
674
|
+
await runCodeUpdater(migration, ast);
|
|
676
675
|
};
|
|
677
676
|
const makeAst = (up, name, changeData, changeTableData2, options) => {
|
|
678
677
|
const { comment } = options;
|
|
@@ -681,17 +680,17 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
|
|
|
681
680
|
const item = changeData[key];
|
|
682
681
|
if ("type" in item) {
|
|
683
682
|
if (up) {
|
|
684
|
-
shape[key] = item.type === "change" && item.usingUp ? __spreadProps$2(__spreadValues$
|
|
683
|
+
shape[key] = item.type === "change" && item.usingUp ? __spreadProps$2(__spreadValues$3({}, item), { using: item.usingUp }) : item;
|
|
685
684
|
} else {
|
|
686
685
|
if (item.type === "rename") {
|
|
687
|
-
shape[item.name] = __spreadProps$2(__spreadValues$
|
|
686
|
+
shape[item.name] = __spreadProps$2(__spreadValues$3({}, item), { name: key });
|
|
688
687
|
} else {
|
|
689
|
-
shape[key] = item.type === "add" ? __spreadProps$2(__spreadValues$
|
|
688
|
+
shape[key] = item.type === "add" ? __spreadProps$2(__spreadValues$3({}, item), { type: "drop" }) : item.type === "drop" ? __spreadProps$2(__spreadValues$3({}, item), { type: "add" }) : item.type === "change" ? __spreadProps$2(__spreadValues$3({}, item), { from: item.to, to: item.from, using: item.usingDown }) : item;
|
|
690
689
|
}
|
|
691
690
|
}
|
|
692
691
|
}
|
|
693
692
|
}
|
|
694
|
-
return __spreadValues$
|
|
693
|
+
return __spreadValues$3({
|
|
695
694
|
type: "changeTable",
|
|
696
695
|
name,
|
|
697
696
|
comment: comment ? up ? Array.isArray(comment) ? comment[1] : comment : Array.isArray(comment) ? comment[0] : null : void 0,
|
|
@@ -707,10 +706,10 @@ const astToQueries = (ast) => {
|
|
|
707
706
|
values: []
|
|
708
707
|
});
|
|
709
708
|
}
|
|
710
|
-
const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$
|
|
709
|
+
const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
|
|
711
710
|
columns: []
|
|
712
711
|
};
|
|
713
|
-
const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$
|
|
712
|
+
const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$3({}, ast.drop.primaryKey) : {
|
|
714
713
|
columns: []
|
|
715
714
|
};
|
|
716
715
|
for (const key in ast.shape) {
|
|
@@ -807,7 +806,7 @@ const astToQueries = (ast) => {
|
|
|
807
806
|
if (fromIndex) {
|
|
808
807
|
dropIndexes.push({
|
|
809
808
|
columns: [
|
|
810
|
-
__spreadValues$
|
|
809
|
+
__spreadValues$3({
|
|
811
810
|
column: key
|
|
812
811
|
}, fromIndex)
|
|
813
812
|
],
|
|
@@ -817,7 +816,7 @@ const astToQueries = (ast) => {
|
|
|
817
816
|
if (toIndex) {
|
|
818
817
|
addIndexes.push({
|
|
819
818
|
columns: [
|
|
820
|
-
__spreadValues$
|
|
819
|
+
__spreadValues$3({
|
|
821
820
|
column: key
|
|
822
821
|
}, toIndex)
|
|
823
822
|
],
|
|
@@ -866,21 +865,21 @@ const astToQueries = (ast) => {
|
|
|
866
865
|
return result;
|
|
867
866
|
};
|
|
868
867
|
|
|
869
|
-
var __defProp$
|
|
868
|
+
var __defProp$2 = Object.defineProperty;
|
|
870
869
|
var __defProps$1 = Object.defineProperties;
|
|
871
870
|
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
872
|
-
var __getOwnPropSymbols$
|
|
873
|
-
var __hasOwnProp$
|
|
874
|
-
var __propIsEnum$
|
|
875
|
-
var __defNormalProp$
|
|
876
|
-
var __spreadValues$
|
|
871
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
872
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
873
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
874
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
875
|
+
var __spreadValues$2 = (a, b) => {
|
|
877
876
|
for (var prop in b || (b = {}))
|
|
878
|
-
if (__hasOwnProp$
|
|
879
|
-
__defNormalProp$
|
|
880
|
-
if (__getOwnPropSymbols$
|
|
881
|
-
for (var prop of __getOwnPropSymbols$
|
|
882
|
-
if (__propIsEnum$
|
|
883
|
-
__defNormalProp$
|
|
877
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
878
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
879
|
+
if (__getOwnPropSymbols$2)
|
|
880
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
881
|
+
if (__propIsEnum$2.call(b, prop))
|
|
882
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
884
883
|
}
|
|
885
884
|
return a;
|
|
886
885
|
};
|
|
@@ -891,6 +890,9 @@ class UnknownColumn extends pqb.ColumnType {
|
|
|
891
890
|
this.dataType = dataType;
|
|
892
891
|
this.operators = pqb.Operators.any;
|
|
893
892
|
}
|
|
893
|
+
toCode() {
|
|
894
|
+
return "unknown";
|
|
895
|
+
}
|
|
894
896
|
}
|
|
895
897
|
const createJoinTable = async (migration, up, tables, options, fn) => {
|
|
896
898
|
const tableName = options.tableName || joinWords(...tables);
|
|
@@ -899,14 +901,14 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
|
|
|
899
901
|
migration,
|
|
900
902
|
up,
|
|
901
903
|
tableName,
|
|
902
|
-
__spreadProps$1(__spreadValues$
|
|
904
|
+
__spreadProps$1(__spreadValues$2({}, options), { noPrimaryKey: true }),
|
|
903
905
|
() => ({})
|
|
904
906
|
);
|
|
905
907
|
}
|
|
906
908
|
const tablesWithPrimaryKeys = await Promise.all(
|
|
907
909
|
tables.map(async (table) => {
|
|
908
910
|
const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
|
|
909
|
-
(items) => items.map((item) => __spreadProps$1(__spreadValues$
|
|
911
|
+
(items) => items.map((item) => __spreadProps$1(__spreadValues$2({}, item), {
|
|
910
912
|
joinedName: joinWords(pluralize.singular(table), item.name)
|
|
911
913
|
}))
|
|
912
914
|
);
|
|
@@ -948,30 +950,32 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
|
|
|
948
950
|
});
|
|
949
951
|
};
|
|
950
952
|
|
|
951
|
-
var __defProp = Object.defineProperty;
|
|
953
|
+
var __defProp$1 = Object.defineProperty;
|
|
952
954
|
var __defProps = Object.defineProperties;
|
|
953
955
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
954
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
955
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
956
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
957
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
958
|
-
var __spreadValues = (a, b) => {
|
|
956
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
957
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
958
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
959
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
960
|
+
var __spreadValues$1 = (a, b) => {
|
|
959
961
|
for (var prop in b || (b = {}))
|
|
960
|
-
if (__hasOwnProp.call(b, prop))
|
|
961
|
-
__defNormalProp(a, prop, b[prop]);
|
|
962
|
-
if (__getOwnPropSymbols)
|
|
963
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
964
|
-
if (__propIsEnum.call(b, prop))
|
|
965
|
-
__defNormalProp(a, prop, b[prop]);
|
|
962
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
963
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
964
|
+
if (__getOwnPropSymbols$1)
|
|
965
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
966
|
+
if (__propIsEnum$1.call(b, prop))
|
|
967
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
966
968
|
}
|
|
967
969
|
return a;
|
|
968
970
|
};
|
|
969
971
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
970
972
|
class Migration extends pqb.TransactionAdapter {
|
|
971
|
-
constructor(tx, up, options) {
|
|
973
|
+
constructor(tx, up, options, adapterOptions, appCodeUpdaterCache) {
|
|
972
974
|
super(tx, tx.client, tx.types);
|
|
973
975
|
this.up = up;
|
|
974
976
|
this.options = options;
|
|
977
|
+
this.adapterOptions = adapterOptions;
|
|
978
|
+
this.appCodeUpdaterCache = appCodeUpdaterCache;
|
|
975
979
|
this.log = pqb.logParamToLogObject(options.logger || console, options.log);
|
|
976
980
|
}
|
|
977
981
|
async query(query, types = this.types, log = this.log) {
|
|
@@ -1005,7 +1009,6 @@ class Migration extends pqb.TransactionAdapter {
|
|
|
1005
1009
|
return changeTable(this, this.up, tableName, options, fn);
|
|
1006
1010
|
}
|
|
1007
1011
|
async renameTable(from, to) {
|
|
1008
|
-
var _a, _b;
|
|
1009
1012
|
const ast = {
|
|
1010
1013
|
type: "renameTable",
|
|
1011
1014
|
from: this.up ? from : to,
|
|
@@ -1014,7 +1017,7 @@ class Migration extends pqb.TransactionAdapter {
|
|
|
1014
1017
|
await this.query(
|
|
1015
1018
|
`ALTER TABLE ${quoteTable(ast.from)} RENAME TO "${ast.to}"`
|
|
1016
1019
|
);
|
|
1017
|
-
await (
|
|
1020
|
+
await runCodeUpdater(this, ast);
|
|
1018
1021
|
}
|
|
1019
1022
|
addColumn(tableName, columnName, fn) {
|
|
1020
1023
|
return addColumn(this, this.up, tableName, columnName, fn);
|
|
@@ -1096,7 +1099,7 @@ const wrapWithLog = async (log, query, fn) => {
|
|
|
1096
1099
|
if (!log) {
|
|
1097
1100
|
return fn();
|
|
1098
1101
|
} else {
|
|
1099
|
-
const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query : __spreadProps(__spreadValues({}, query), { values: [] });
|
|
1102
|
+
const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query : __spreadProps(__spreadValues$1({}, query), { values: [] });
|
|
1100
1103
|
const logData = log.beforeQuery(sql);
|
|
1101
1104
|
try {
|
|
1102
1105
|
const result = await fn();
|
|
@@ -1114,16 +1117,15 @@ const addColumn = (migration, up, tableName, columnName, fn) => {
|
|
|
1114
1117
|
}));
|
|
1115
1118
|
};
|
|
1116
1119
|
const addIndex = (migration, up, tableName, columns, options) => {
|
|
1117
|
-
return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.index(columns, options))));
|
|
1120
|
+
return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.index(columns, options))));
|
|
1118
1121
|
};
|
|
1119
1122
|
const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
|
|
1120
|
-
return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))));
|
|
1123
|
+
return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))));
|
|
1121
1124
|
};
|
|
1122
1125
|
const addPrimaryKey = (migration, up, tableName, columns, options) => {
|
|
1123
|
-
return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.primaryKey(columns, options))));
|
|
1126
|
+
return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.primaryKey(columns, options))));
|
|
1124
1127
|
};
|
|
1125
1128
|
const createSchema = async (migration, up, name) => {
|
|
1126
|
-
var _a, _b;
|
|
1127
1129
|
const ast = {
|
|
1128
1130
|
type: "schema",
|
|
1129
1131
|
action: up ? "create" : "drop",
|
|
@@ -1132,11 +1134,10 @@ const createSchema = async (migration, up, name) => {
|
|
|
1132
1134
|
await migration.query(
|
|
1133
1135
|
`${ast.action === "create" ? "CREATE" : "DROP"} SCHEMA "${name}"`
|
|
1134
1136
|
);
|
|
1135
|
-
await (
|
|
1137
|
+
await runCodeUpdater(migration, ast);
|
|
1136
1138
|
};
|
|
1137
1139
|
const createExtension = async (migration, up, name, options) => {
|
|
1138
|
-
|
|
1139
|
-
const ast = __spreadValues({
|
|
1140
|
+
const ast = __spreadValues$1({
|
|
1140
1141
|
type: "extension",
|
|
1141
1142
|
action: up ? "create" : "drop",
|
|
1142
1143
|
name
|
|
@@ -1148,17 +1149,54 @@ const createExtension = async (migration, up, name, options) => {
|
|
|
1148
1149
|
query = `CREATE EXTENSION${ast.ifExists ? " IF NOT EXISTS" : ""} "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ""}${ast.version ? ` VERSION '${ast.version}'` : ""}${ast.cascade ? " CASCADE" : ""}`;
|
|
1149
1150
|
}
|
|
1150
1151
|
await migration.query(query);
|
|
1151
|
-
await (
|
|
1152
|
+
await runCodeUpdater(migration, ast);
|
|
1152
1153
|
};
|
|
1153
1154
|
const queryExists = (db, sql) => {
|
|
1154
1155
|
return db.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
1155
1156
|
};
|
|
1157
|
+
const runCodeUpdater = (migration, ast) => {
|
|
1158
|
+
var _a, _b;
|
|
1159
|
+
return (_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, {
|
|
1160
|
+
ast,
|
|
1161
|
+
options: migration.adapterOptions,
|
|
1162
|
+
cache: migration.appCodeUpdaterCache
|
|
1163
|
+
});
|
|
1164
|
+
};
|
|
1156
1165
|
|
|
1166
|
+
var __defProp = Object.defineProperty;
|
|
1167
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
1168
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
1169
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
1170
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1171
|
+
var __spreadValues = (a, b) => {
|
|
1172
|
+
for (var prop in b || (b = {}))
|
|
1173
|
+
if (__hasOwnProp.call(b, prop))
|
|
1174
|
+
__defNormalProp(a, prop, b[prop]);
|
|
1175
|
+
if (__getOwnPropSymbols)
|
|
1176
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
1177
|
+
if (__propIsEnum.call(b, prop))
|
|
1178
|
+
__defNormalProp(a, prop, b[prop]);
|
|
1179
|
+
}
|
|
1180
|
+
return a;
|
|
1181
|
+
};
|
|
1157
1182
|
const migrateOrRollback = async (options, config, args, up) => {
|
|
1158
1183
|
var _a;
|
|
1184
|
+
config = __spreadValues({}, config);
|
|
1159
1185
|
const files = await getMigrationFiles(config, up);
|
|
1160
|
-
|
|
1161
|
-
let
|
|
1186
|
+
let count = up ? Infinity : 1;
|
|
1187
|
+
let argI = 0;
|
|
1188
|
+
const num = args[0] === "all" ? Infinity : parseInt(args[0]);
|
|
1189
|
+
if (!isNaN(num)) {
|
|
1190
|
+
argI++;
|
|
1191
|
+
count = num;
|
|
1192
|
+
}
|
|
1193
|
+
const arg = args[argI];
|
|
1194
|
+
if (arg === "--code") {
|
|
1195
|
+
config.useCodeUpdater = args[argI + 1] !== "false";
|
|
1196
|
+
}
|
|
1197
|
+
if (!config.useCodeUpdater)
|
|
1198
|
+
delete config.appCodeUpdater;
|
|
1199
|
+
const appCodeUpdaterCache = {};
|
|
1162
1200
|
for (const opts of pqb.toArray(options)) {
|
|
1163
1201
|
const db = new pqb.Adapter(opts);
|
|
1164
1202
|
const migratedVersions = await getMigratedVersionsMap(db, config);
|
|
@@ -1169,18 +1207,19 @@ const migrateOrRollback = async (options, config, args, up) => {
|
|
|
1169
1207
|
}
|
|
1170
1208
|
if (count-- <= 0)
|
|
1171
1209
|
break;
|
|
1172
|
-
await processMigration(db, up, file, config);
|
|
1210
|
+
await processMigration(db, up, file, config, opts, appCodeUpdaterCache);
|
|
1173
1211
|
(_a = config.logger) == null ? void 0 : _a.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
|
|
1174
1212
|
}
|
|
1175
1213
|
} finally {
|
|
1176
1214
|
await db.close();
|
|
1177
1215
|
}
|
|
1216
|
+
delete config.appCodeUpdater;
|
|
1178
1217
|
}
|
|
1179
1218
|
};
|
|
1180
1219
|
const changeCache = {};
|
|
1181
|
-
const processMigration = async (db, up, file, config) => {
|
|
1220
|
+
const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
|
|
1182
1221
|
await db.transaction(async (tx) => {
|
|
1183
|
-
const db2 = new Migration(tx, up, config);
|
|
1222
|
+
const db2 = new Migration(tx, up, config, options, appCodeUpdaterCache);
|
|
1184
1223
|
setCurrentMigration(db2);
|
|
1185
1224
|
setCurrentMigrationUp(up);
|
|
1186
1225
|
const callback = changeCache[file.path];
|
|
@@ -1403,17 +1442,30 @@ Commands:
|
|
|
1403
1442
|
drop drop databases
|
|
1404
1443
|
reset drop, create and migrate databases
|
|
1405
1444
|
g, generate generate migration file, see below
|
|
1406
|
-
migrate migrate
|
|
1445
|
+
migrate migrate pending migrations
|
|
1407
1446
|
rollback rollback the last migrated
|
|
1408
1447
|
no or unknown command prints this message
|
|
1409
1448
|
|
|
1449
|
+
Migrate arguments:
|
|
1450
|
+
no arguments run all pending migrations
|
|
1451
|
+
number run specific number of pending migrations
|
|
1452
|
+
|
|
1453
|
+
Rollback arguments:
|
|
1454
|
+
no arguments rollback one last applied migration
|
|
1455
|
+
number rollback specific number of applied migrations
|
|
1456
|
+
all rollback all applied migrations
|
|
1457
|
+
|
|
1458
|
+
Migrate and rollback common arguments:
|
|
1459
|
+
--code run code updater, overrides \`useCodeUpdater\` option
|
|
1460
|
+
--code false do not run code updater
|
|
1461
|
+
|
|
1410
1462
|
Generate arguments:
|
|
1411
1463
|
- (required) first argument is migration name
|
|
1412
|
-
* create*
|
|
1413
|
-
* change*
|
|
1414
|
-
* add*To*
|
|
1415
|
-
* remove*From*
|
|
1416
|
-
* drop*
|
|
1464
|
+
* create* template for create table
|
|
1465
|
+
* change* template for change table
|
|
1466
|
+
* add*To* template for add columns
|
|
1467
|
+
* remove*From* template for remove columns
|
|
1468
|
+
* drop* template for drop table
|
|
1417
1469
|
|
|
1418
1470
|
- other arguments considered as columns with types and optional methods:
|
|
1419
1471
|
rake-db g createTable id:serial.primaryKey name:text.nullable
|
|
@@ -1429,4 +1481,5 @@ exports.migrate = migrate;
|
|
|
1429
1481
|
exports.rakeDb = rakeDb;
|
|
1430
1482
|
exports.resetDb = resetDb;
|
|
1431
1483
|
exports.rollback = rollback;
|
|
1484
|
+
exports.runCodeUpdater = runCodeUpdater;
|
|
1432
1485
|
//# sourceMappingURL=index.js.map
|