orchid-orm 1.32.9 → 1.32.10
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/dist/migrations.js +414 -245
- package/dist/migrations.js.map +1 -1
- package/dist/migrations.mjs +415 -246
- package/dist/migrations.mjs.map +1 -1
- package/package.json +6 -6
package/dist/migrations.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { promptSelect, colors, getSchemaAndTableFromName, getDbTableColumnsChecks, dbColumnToAst, instantiateDbColumn, concatSchemaAndName, encodeColumnDefault, getIndexName, getConstraintName, tableToAst, getDbStructureTableData, makeDomainsMap, astToMigration, createMigrationInterface, introspectDbSchema, exhaustive, pluralize, makeStructureToAstCtx, makeFileVersion, writeMigrationFile, migrate, structureToAst, saveMigratedVersion, rakeDbCommands } from 'rake-db';
|
|
2
2
|
export * from 'rake-db';
|
|
3
|
-
import { toSnakeCase, deepCompare, toArray,
|
|
3
|
+
import { toSnakeCase, deepCompare, toArray, addCode, codeToString, toCamelCase, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
|
|
4
4
|
import { EnumColumn, ArrayColumn, DomainColumn, RawSQL, VirtualColumn, UnknownColumn, defaultSchemaConfig, columnsShapeToCode, pushTableDataCode, Adapter } from 'pqb';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { pathToFileURL } from 'url';
|
|
@@ -98,14 +98,14 @@ const promptCreateOrRename = (kind, name, drop, verifying) => {
|
|
|
98
98
|
]
|
|
99
99
|
});
|
|
100
100
|
};
|
|
101
|
-
const
|
|
101
|
+
const checkForColumnAddOrDrop = (shape, key) => {
|
|
102
102
|
const item = shape[key];
|
|
103
103
|
if (item) {
|
|
104
|
-
return item && (Array.isArray(item) || item.type
|
|
104
|
+
return item && (Array.isArray(item) || item.type === "add" || item.type === "drop");
|
|
105
105
|
}
|
|
106
106
|
for (const k in shape) {
|
|
107
107
|
const item2 = shape[k];
|
|
108
|
-
if (Array.isArray(item2) || item2.type
|
|
108
|
+
if (Array.isArray(item2) || item2.type === "add" || item2.type === "drop") {
|
|
109
109
|
return true;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
@@ -181,19 +181,19 @@ const renameSchemaInStructures = (items, from, to) => {
|
|
|
181
181
|
}
|
|
182
182
|
};
|
|
183
183
|
|
|
184
|
-
var __defProp$
|
|
185
|
-
var __getOwnPropSymbols$
|
|
186
|
-
var __hasOwnProp$
|
|
187
|
-
var __propIsEnum$
|
|
188
|
-
var __defNormalProp$
|
|
189
|
-
var __spreadValues$
|
|
184
|
+
var __defProp$9 = Object.defineProperty;
|
|
185
|
+
var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
|
|
186
|
+
var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
|
|
187
|
+
var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
|
|
188
|
+
var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
189
|
+
var __spreadValues$9 = (a, b) => {
|
|
190
190
|
for (var prop in b || (b = {}))
|
|
191
|
-
if (__hasOwnProp$
|
|
192
|
-
__defNormalProp$
|
|
193
|
-
if (__getOwnPropSymbols$
|
|
194
|
-
for (var prop of __getOwnPropSymbols$
|
|
195
|
-
if (__propIsEnum$
|
|
196
|
-
__defNormalProp$
|
|
191
|
+
if (__hasOwnProp$9.call(b, prop))
|
|
192
|
+
__defNormalProp$9(a, prop, b[prop]);
|
|
193
|
+
if (__getOwnPropSymbols$9)
|
|
194
|
+
for (var prop of __getOwnPropSymbols$9(b)) {
|
|
195
|
+
if (__propIsEnum$9.call(b, prop))
|
|
196
|
+
__defNormalProp$9(a, prop, b[prop]);
|
|
197
197
|
}
|
|
198
198
|
return a;
|
|
199
199
|
};
|
|
@@ -227,7 +227,7 @@ const processExtensions = (ast, dbStructure, currentSchema, extensions) => {
|
|
|
227
227
|
}
|
|
228
228
|
if (codeExtensions == null ? void 0 : codeExtensions.length) {
|
|
229
229
|
ast.push(
|
|
230
|
-
...codeExtensions.map((ext) => __spreadValues$
|
|
230
|
+
...codeExtensions.map((ext) => __spreadValues$9({
|
|
231
231
|
type: "extension",
|
|
232
232
|
action: "create"
|
|
233
233
|
}, ext))
|
|
@@ -235,22 +235,25 @@ const processExtensions = (ast, dbStructure, currentSchema, extensions) => {
|
|
|
235
235
|
}
|
|
236
236
|
};
|
|
237
237
|
|
|
238
|
-
var __defProp$
|
|
239
|
-
var
|
|
240
|
-
var
|
|
241
|
-
var
|
|
242
|
-
var
|
|
243
|
-
var
|
|
238
|
+
var __defProp$8 = Object.defineProperty;
|
|
239
|
+
var __defProps$7 = Object.defineProperties;
|
|
240
|
+
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
241
|
+
var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
|
|
242
|
+
var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
|
|
243
|
+
var __propIsEnum$8 = Object.prototype.propertyIsEnumerable;
|
|
244
|
+
var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
245
|
+
var __spreadValues$8 = (a, b) => {
|
|
244
246
|
for (var prop in b || (b = {}))
|
|
245
|
-
if (__hasOwnProp$
|
|
246
|
-
__defNormalProp$
|
|
247
|
-
if (__getOwnPropSymbols$
|
|
248
|
-
for (var prop of __getOwnPropSymbols$
|
|
249
|
-
if (__propIsEnum$
|
|
250
|
-
__defNormalProp$
|
|
247
|
+
if (__hasOwnProp$8.call(b, prop))
|
|
248
|
+
__defNormalProp$8(a, prop, b[prop]);
|
|
249
|
+
if (__getOwnPropSymbols$8)
|
|
250
|
+
for (var prop of __getOwnPropSymbols$8(b)) {
|
|
251
|
+
if (__propIsEnum$8.call(b, prop))
|
|
252
|
+
__defNormalProp$8(a, prop, b[prop]);
|
|
251
253
|
}
|
|
252
254
|
return a;
|
|
253
255
|
};
|
|
256
|
+
var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
254
257
|
const processColumns = async (adapter, config, structureToAstCtx, dbStructure, domainsMap, changeTableData, ast, currentSchema, compareSql, typeCastsCache, verifying) => {
|
|
255
258
|
const { dbTable } = changeTableData;
|
|
256
259
|
const dbColumns = Object.fromEntries(
|
|
@@ -335,7 +338,7 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
335
338
|
schema,
|
|
336
339
|
changeTableAst: { name: tableName, shape }
|
|
337
340
|
}, columnsToAdd, columnsToDrop, columnsToChange, verifying) => {
|
|
338
|
-
var _a, _b, _c
|
|
341
|
+
var _a, _b, _c;
|
|
339
342
|
for (const { key, column } of columnsToAdd) {
|
|
340
343
|
if (columnsToDrop.length) {
|
|
341
344
|
const codeName = (_a = column.data.name) != null ? _a : key;
|
|
@@ -349,9 +352,9 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
349
352
|
const drop = columnsToDrop[index - 1];
|
|
350
353
|
columnsToDrop.splice(index - 1, 1);
|
|
351
354
|
const from = (_b = drop.column.data.name) != null ? _b : drop.key;
|
|
352
|
-
columnsToChange.set(
|
|
355
|
+
columnsToChange.set(from, {
|
|
353
356
|
key,
|
|
354
|
-
dbName:
|
|
357
|
+
dbName: from,
|
|
355
358
|
column: column.name(codeName)
|
|
356
359
|
});
|
|
357
360
|
const to = config.snakeCase ? toSnakeCase(key) : key;
|
|
@@ -366,7 +369,7 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
366
369
|
}
|
|
367
370
|
}
|
|
368
371
|
for (const c of dbTableData.constraints) {
|
|
369
|
-
if ((
|
|
372
|
+
if ((_c = c.check) == null ? void 0 : _c.columns) {
|
|
370
373
|
renameColumn(c.check.columns, from, to);
|
|
371
374
|
}
|
|
372
375
|
if (c.references) {
|
|
@@ -402,7 +405,6 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
402
405
|
{ key: codeKey, dbName, column: codeColumn }
|
|
403
406
|
] of columnsToChange) {
|
|
404
407
|
const dbColumnStructure = dbColumns[dbName];
|
|
405
|
-
const { shape } = changeTableData.changeTableAst;
|
|
406
408
|
const dbColumn = instantiateDbColumn(
|
|
407
409
|
structureToAstCtx,
|
|
408
410
|
dbStructure,
|
|
@@ -419,11 +421,12 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
419
421
|
typeCastsCache,
|
|
420
422
|
verifying,
|
|
421
423
|
key,
|
|
424
|
+
dbName,
|
|
422
425
|
dbColumn,
|
|
423
426
|
codeColumn
|
|
424
427
|
);
|
|
425
428
|
if (action === "change") {
|
|
426
|
-
changeColumn(
|
|
429
|
+
changeColumn(changeTableData, key, dbName, dbColumn, codeColumn);
|
|
427
430
|
} else if (action === "recreate") {
|
|
428
431
|
changeTableData.changeTableAst.shape[key] = [
|
|
429
432
|
{
|
|
@@ -438,7 +441,7 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
438
441
|
} else if (action !== "recreate") {
|
|
439
442
|
const to = (_a = codeColumn.data.name) != null ? _a : codeKey;
|
|
440
443
|
if (dbName !== to) {
|
|
441
|
-
shape[config.snakeCase ? dbName === toSnakeCase(codeKey) ? codeKey : dbName : dbName] = {
|
|
444
|
+
changeTableData.changeTableAst.shape[config.snakeCase ? dbName === toSnakeCase(codeKey) ? codeKey : dbName : dbName] = {
|
|
442
445
|
type: "rename",
|
|
443
446
|
name: config.snakeCase ? to === toSnakeCase(codeKey) ? codeKey : to : to
|
|
444
447
|
};
|
|
@@ -446,7 +449,7 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
446
449
|
}
|
|
447
450
|
}
|
|
448
451
|
};
|
|
449
|
-
const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbColumn, codeColumn) => {
|
|
452
|
+
const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
|
|
450
453
|
var _a, _b, _c, _d;
|
|
451
454
|
const dbType = getColumnDbType(dbColumn, currentSchema);
|
|
452
455
|
const codeType = getColumnDbType(codeColumn, currentSchema);
|
|
@@ -533,7 +536,7 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
533
536
|
}
|
|
534
537
|
if (!deepCompare(
|
|
535
538
|
dbData.identity,
|
|
536
|
-
codeData.identity ? __spreadValues$
|
|
539
|
+
codeData.identity ? __spreadValues$8({
|
|
537
540
|
always: false,
|
|
538
541
|
start: 1,
|
|
539
542
|
increment: 1,
|
|
@@ -565,12 +568,7 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
565
568
|
inDb: dbDefault,
|
|
566
569
|
inCode: codeDefault,
|
|
567
570
|
change: () => {
|
|
568
|
-
changeColumn(
|
|
569
|
-
changeTableData.changeTableAst.shape,
|
|
570
|
-
key,
|
|
571
|
-
dbColumn,
|
|
572
|
-
codeColumn
|
|
573
|
-
);
|
|
571
|
+
changeColumn(changeTableData, key, dbName, dbColumn, codeColumn);
|
|
574
572
|
if (!changeTableData.pushedAst) {
|
|
575
573
|
changeTableData.pushedAst = true;
|
|
576
574
|
ast.push(changeTableData.changeTableAst);
|
|
@@ -581,12 +579,23 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
581
579
|
}
|
|
582
580
|
return;
|
|
583
581
|
};
|
|
584
|
-
const changeColumn = (
|
|
582
|
+
const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
|
|
585
583
|
dbColumn.data.as = codeColumn.data.as = void 0;
|
|
586
|
-
|
|
584
|
+
const simpleCodeColumn = Object.create(codeColumn);
|
|
585
|
+
simpleCodeColumn.data = __spreadProps$7(__spreadValues$8({}, codeColumn.data), {
|
|
586
|
+
primaryKey: void 0,
|
|
587
|
+
indexes: void 0,
|
|
588
|
+
foreignKeys: void 0,
|
|
589
|
+
check: void 0
|
|
590
|
+
});
|
|
591
|
+
changeTableData.changingColumns[dbName] = {
|
|
592
|
+
from: dbColumn,
|
|
593
|
+
to: simpleCodeColumn
|
|
594
|
+
};
|
|
595
|
+
changeTableData.changeTableAst.shape[key] = {
|
|
587
596
|
type: "change",
|
|
588
597
|
from: { column: dbColumn },
|
|
589
|
-
to: { column:
|
|
598
|
+
to: { column: simpleCodeColumn }
|
|
590
599
|
};
|
|
591
600
|
};
|
|
592
601
|
const getColumnDbType = (column, currentSchema) => {
|
|
@@ -772,19 +781,19 @@ const createAst = (codeDomain) => ({
|
|
|
772
781
|
baseType: codeDomain.column
|
|
773
782
|
});
|
|
774
783
|
|
|
775
|
-
var __defProp$
|
|
776
|
-
var __getOwnPropSymbols$
|
|
777
|
-
var __hasOwnProp$
|
|
778
|
-
var __propIsEnum$
|
|
779
|
-
var __defNormalProp$
|
|
780
|
-
var __spreadValues$
|
|
784
|
+
var __defProp$7 = Object.defineProperty;
|
|
785
|
+
var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
|
|
786
|
+
var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
|
|
787
|
+
var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
|
|
788
|
+
var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
789
|
+
var __spreadValues$7 = (a, b) => {
|
|
781
790
|
for (var prop in b || (b = {}))
|
|
782
|
-
if (__hasOwnProp$
|
|
783
|
-
__defNormalProp$
|
|
784
|
-
if (__getOwnPropSymbols$
|
|
785
|
-
for (var prop of __getOwnPropSymbols$
|
|
786
|
-
if (__propIsEnum$
|
|
787
|
-
__defNormalProp$
|
|
791
|
+
if (__hasOwnProp$7.call(b, prop))
|
|
792
|
+
__defNormalProp$7(a, prop, b[prop]);
|
|
793
|
+
if (__getOwnPropSymbols$7)
|
|
794
|
+
for (var prop of __getOwnPropSymbols$7(b)) {
|
|
795
|
+
if (__propIsEnum$7.call(b, prop))
|
|
796
|
+
__defNormalProp$7(a, prop, b[prop]);
|
|
788
797
|
}
|
|
789
798
|
return a;
|
|
790
799
|
};
|
|
@@ -864,7 +873,7 @@ const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) =
|
|
|
864
873
|
continue;
|
|
865
874
|
}
|
|
866
875
|
}
|
|
867
|
-
ast.push(__spreadValues$
|
|
876
|
+
ast.push(__spreadValues$7({
|
|
868
877
|
type: "enum",
|
|
869
878
|
action: "create"
|
|
870
879
|
}, codeEnum));
|
|
@@ -925,7 +934,26 @@ const renameColumnsTypeSchema = (dbStructure, from, to) => {
|
|
|
925
934
|
}
|
|
926
935
|
};
|
|
927
936
|
|
|
928
|
-
|
|
937
|
+
var __defProp$6 = Object.defineProperty;
|
|
938
|
+
var __defProps$6 = Object.defineProperties;
|
|
939
|
+
var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
|
|
940
|
+
var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
|
|
941
|
+
var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
|
|
942
|
+
var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
|
|
943
|
+
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
944
|
+
var __spreadValues$6 = (a, b) => {
|
|
945
|
+
for (var prop in b || (b = {}))
|
|
946
|
+
if (__hasOwnProp$6.call(b, prop))
|
|
947
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
948
|
+
if (__getOwnPropSymbols$6)
|
|
949
|
+
for (var prop of __getOwnPropSymbols$6(b)) {
|
|
950
|
+
if (__propIsEnum$6.call(b, prop))
|
|
951
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
952
|
+
}
|
|
953
|
+
return a;
|
|
954
|
+
};
|
|
955
|
+
var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
956
|
+
const processPrimaryKey = (config, changeTableData) => {
|
|
929
957
|
var _a;
|
|
930
958
|
const { codeTable } = changeTableData;
|
|
931
959
|
const columnsPrimaryKey = [];
|
|
@@ -936,14 +964,15 @@ const processPrimaryKey = (config, ast, changeTableData) => {
|
|
|
936
964
|
}
|
|
937
965
|
}
|
|
938
966
|
changePrimaryKey(config, columnsPrimaryKey, changeTableData);
|
|
939
|
-
renamePrimaryKey(
|
|
967
|
+
renamePrimaryKey(changeTableData);
|
|
940
968
|
};
|
|
941
969
|
const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
942
970
|
codeTable,
|
|
943
971
|
dbTableData: { primaryKey: dbPrimaryKey },
|
|
944
|
-
changeTableAst: { shape, add, drop }
|
|
972
|
+
changeTableAst: { shape, add, drop },
|
|
973
|
+
changingColumns
|
|
945
974
|
}) => {
|
|
946
|
-
var _a;
|
|
975
|
+
var _a, _b, _c;
|
|
947
976
|
const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
|
|
948
977
|
const primaryKey = [
|
|
949
978
|
.../* @__PURE__ */ new Set([
|
|
@@ -954,19 +983,48 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
954
983
|
})) : tablePrimaryKey == null ? void 0 : tablePrimaryKey.columns.map((key) => ({ key, name: key }))) != null ? _a : []
|
|
955
984
|
])
|
|
956
985
|
];
|
|
957
|
-
if (
|
|
986
|
+
if (dbPrimaryKey && primaryKey.length === dbPrimaryKey.columns.length && !primaryKey.some(
|
|
958
987
|
({ name }) => !dbPrimaryKey.columns.some((dbName) => name === dbName)
|
|
959
988
|
)) {
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
989
|
+
if (primaryKey.length === 1) {
|
|
990
|
+
const { key } = primaryKey[0];
|
|
991
|
+
const changes = shape[key] && toArray(shape[key]);
|
|
992
|
+
if (changes) {
|
|
993
|
+
for (const change of changes) {
|
|
994
|
+
if (change.type !== "change")
|
|
995
|
+
continue;
|
|
996
|
+
if (change.from.column) {
|
|
997
|
+
change.from.column.data.primaryKey = void 0;
|
|
998
|
+
}
|
|
999
|
+
if (change.to.column) {
|
|
1000
|
+
const column = Object.create(change.to.column);
|
|
1001
|
+
column.data = __spreadProps$6(__spreadValues$6({}, column.data), { primaryKey: void 0 });
|
|
1002
|
+
change.to.column = column;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
const toDrop = dbPrimaryKey == null ? void 0 : dbPrimaryKey.columns.filter(
|
|
1010
|
+
(key) => !checkForColumnAddOrDrop(shape, key)
|
|
1011
|
+
);
|
|
1012
|
+
if (toDrop == null ? void 0 : toDrop.length) {
|
|
1013
|
+
if (toDrop.length === 1 && changingColumns[toDrop[0]]) {
|
|
1014
|
+
const column = changingColumns[toDrop[0]];
|
|
1015
|
+
column.from.data.primaryKey = (_b = dbPrimaryKey == null ? void 0 : dbPrimaryKey.name) != null ? _b : true;
|
|
1016
|
+
} else {
|
|
964
1017
|
drop.primaryKey = { columns: toDrop, name: dbPrimaryKey == null ? void 0 : dbPrimaryKey.name };
|
|
965
1018
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
)
|
|
969
|
-
|
|
1019
|
+
}
|
|
1020
|
+
const toAdd = primaryKey.filter(
|
|
1021
|
+
({ key }) => !checkForColumnAddOrDrop(shape, key)
|
|
1022
|
+
);
|
|
1023
|
+
if (toAdd.length) {
|
|
1024
|
+
if (toAdd.length === 1 && changingColumns[toAdd[0].name]) {
|
|
1025
|
+
const column = changingColumns[toAdd[0].name];
|
|
1026
|
+
column.to.data.primaryKey = (_c = tablePrimaryKey == null ? void 0 : tablePrimaryKey.name) != null ? _c : true;
|
|
1027
|
+
} else {
|
|
970
1028
|
add.primaryKey = {
|
|
971
1029
|
columns: toAdd.map((c) => c.key),
|
|
972
1030
|
name: tablePrimaryKey == null ? void 0 : tablePrimaryKey.name
|
|
@@ -974,15 +1032,16 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
974
1032
|
}
|
|
975
1033
|
}
|
|
976
1034
|
};
|
|
977
|
-
const renamePrimaryKey = (
|
|
1035
|
+
const renamePrimaryKey = ({
|
|
978
1036
|
codeTable,
|
|
979
1037
|
dbTableData: { primaryKey: dbPrimaryKey },
|
|
980
|
-
schema
|
|
1038
|
+
schema,
|
|
1039
|
+
delayedAst
|
|
981
1040
|
}) => {
|
|
982
1041
|
var _a, _b;
|
|
983
1042
|
const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
|
|
984
1043
|
if (dbPrimaryKey && tablePrimaryKey && (dbPrimaryKey == null ? void 0 : dbPrimaryKey.name) !== (tablePrimaryKey == null ? void 0 : tablePrimaryKey.name)) {
|
|
985
|
-
|
|
1044
|
+
delayedAst.push({
|
|
986
1045
|
type: "renameTableItem",
|
|
987
1046
|
kind: "CONSTRAINT",
|
|
988
1047
|
tableSchema: schema,
|
|
@@ -993,38 +1052,38 @@ const renamePrimaryKey = (ast, {
|
|
|
993
1052
|
}
|
|
994
1053
|
};
|
|
995
1054
|
|
|
996
|
-
var __defProp$
|
|
997
|
-
var __defProps$
|
|
998
|
-
var __getOwnPropDescs$
|
|
999
|
-
var __getOwnPropSymbols$
|
|
1000
|
-
var __hasOwnProp$
|
|
1001
|
-
var __propIsEnum$
|
|
1002
|
-
var __defNormalProp$
|
|
1003
|
-
var __spreadValues$
|
|
1055
|
+
var __defProp$5 = Object.defineProperty;
|
|
1056
|
+
var __defProps$5 = Object.defineProperties;
|
|
1057
|
+
var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
|
|
1058
|
+
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
1059
|
+
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
1060
|
+
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
1061
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1062
|
+
var __spreadValues$5 = (a, b) => {
|
|
1004
1063
|
for (var prop in b || (b = {}))
|
|
1005
|
-
if (__hasOwnProp$
|
|
1006
|
-
__defNormalProp$
|
|
1007
|
-
if (__getOwnPropSymbols$
|
|
1008
|
-
for (var prop of __getOwnPropSymbols$
|
|
1009
|
-
if (__propIsEnum$
|
|
1010
|
-
__defNormalProp$
|
|
1064
|
+
if (__hasOwnProp$5.call(b, prop))
|
|
1065
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
1066
|
+
if (__getOwnPropSymbols$5)
|
|
1067
|
+
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
1068
|
+
if (__propIsEnum$5.call(b, prop))
|
|
1069
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
1011
1070
|
}
|
|
1012
1071
|
return a;
|
|
1013
1072
|
};
|
|
1014
|
-
var __spreadProps$
|
|
1073
|
+
var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
1015
1074
|
var __objRest = (source, exclude) => {
|
|
1016
1075
|
var target = {};
|
|
1017
1076
|
for (var prop in source)
|
|
1018
|
-
if (__hasOwnProp$
|
|
1077
|
+
if (__hasOwnProp$5.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1019
1078
|
target[prop] = source[prop];
|
|
1020
|
-
if (source != null && __getOwnPropSymbols$
|
|
1021
|
-
for (var prop of __getOwnPropSymbols$
|
|
1022
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$
|
|
1079
|
+
if (source != null && __getOwnPropSymbols$5)
|
|
1080
|
+
for (var prop of __getOwnPropSymbols$5(source)) {
|
|
1081
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$5.call(source, prop))
|
|
1023
1082
|
target[prop] = source[prop];
|
|
1024
1083
|
}
|
|
1025
1084
|
return target;
|
|
1026
1085
|
};
|
|
1027
|
-
const processIndexes = (config, changeTableData,
|
|
1086
|
+
const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
1028
1087
|
const codeIndexes = collectCodeIndexes(config, changeTableData);
|
|
1029
1088
|
const codeComparableIndexes = collectCodeComparableIndexes(
|
|
1030
1089
|
config,
|
|
@@ -1037,97 +1096,95 @@ const processIndexes = (config, changeTableData, delayedAst, ast, compareExpress
|
|
|
1037
1096
|
changeTableAst: { shape }
|
|
1038
1097
|
} = changeTableData;
|
|
1039
1098
|
for (const dbIndex of changeTableData.dbTableData.indexes) {
|
|
1040
|
-
const
|
|
1041
|
-
(column) => "column" in column &&
|
|
1099
|
+
const hasAddedOrDroppedColumn = dbIndex.columns.some(
|
|
1100
|
+
(column) => "column" in column && checkForColumnAddOrDrop(shape, column.column)
|
|
1042
1101
|
);
|
|
1043
|
-
if (
|
|
1102
|
+
if (hasAddedOrDroppedColumn)
|
|
1044
1103
|
continue;
|
|
1045
1104
|
normalizeIndex(dbIndex);
|
|
1046
|
-
const
|
|
1047
|
-
|
|
1048
|
-
const { found, rename } = findMatchingIndexWithoutSql(
|
|
1049
|
-
dbComparableIndex,
|
|
1105
|
+
const { found, rename, foundAndHasSql } = findMatchingIndex(
|
|
1106
|
+
dbIndex,
|
|
1050
1107
|
codeComparableIndexes,
|
|
1051
1108
|
codeIndexes,
|
|
1052
1109
|
skipCodeIndexes,
|
|
1053
1110
|
changeTableData.codeTable.table,
|
|
1054
1111
|
config
|
|
1055
1112
|
);
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
holdCodeIndexes.set(codeIndex, true);
|
|
1059
|
-
}
|
|
1060
|
-
const compare = [];
|
|
1061
|
-
for (let i = 0; i < dbIndex.columns.length; i++) {
|
|
1062
|
-
const column = dbIndex.columns[i];
|
|
1063
|
-
if (!("expression" in column))
|
|
1064
|
-
continue;
|
|
1065
|
-
compare.push({
|
|
1066
|
-
inDb: column.expression,
|
|
1067
|
-
inCode: found.map(
|
|
1068
|
-
(index) => index.columns[i].expression
|
|
1069
|
-
)
|
|
1070
|
-
});
|
|
1071
|
-
}
|
|
1072
|
-
if (dbIndex.with) {
|
|
1073
|
-
compare.push({
|
|
1074
|
-
inDb: dbIndex.with,
|
|
1075
|
-
inCode: found.map((index) => index.options.with)
|
|
1076
|
-
});
|
|
1077
|
-
}
|
|
1078
|
-
if (dbIndex.where) {
|
|
1079
|
-
compare.push({
|
|
1080
|
-
inDb: dbIndex.where,
|
|
1081
|
-
inCode: found.map((index) => index.options.where)
|
|
1082
|
-
});
|
|
1083
|
-
}
|
|
1084
|
-
wait++;
|
|
1085
|
-
compareExpressions.push({
|
|
1086
|
-
compare,
|
|
1087
|
-
handle(index) {
|
|
1088
|
-
var _a, _b;
|
|
1089
|
-
const codeIndex = index === void 0 ? void 0 : found[index];
|
|
1090
|
-
handleIndexChange(
|
|
1091
|
-
ast,
|
|
1092
|
-
changeTableData,
|
|
1093
|
-
dbIndex,
|
|
1094
|
-
dbColumns,
|
|
1095
|
-
codeIndex,
|
|
1096
|
-
index === void 0 ? void 0 : rename[index]
|
|
1097
|
-
);
|
|
1098
|
-
if (codeIndex) {
|
|
1099
|
-
holdCodeIndexes.delete(codeIndex);
|
|
1100
|
-
}
|
|
1101
|
-
if (!--wait && holdCodeIndexes.size) {
|
|
1102
|
-
((_b = (_a = changeTableData.changeTableAst.add).indexes) != null ? _b : _a.indexes = []).push(
|
|
1103
|
-
...holdCodeIndexes.keys()
|
|
1104
|
-
);
|
|
1105
|
-
if (!changeTableData.pushedAst) {
|
|
1106
|
-
changeTableData.pushedAst = true;
|
|
1107
|
-
ast.push(changeTableData.changeTableAst);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
});
|
|
1112
|
-
} else {
|
|
1113
|
+
const { columns: dbColumns } = dbIndex;
|
|
1114
|
+
if (!foundAndHasSql) {
|
|
1113
1115
|
handleIndexChange(
|
|
1114
|
-
delayedAst,
|
|
1115
1116
|
changeTableData,
|
|
1116
1117
|
dbIndex,
|
|
1117
1118
|
dbColumns,
|
|
1118
1119
|
found[0],
|
|
1119
1120
|
rename[0]
|
|
1120
1121
|
);
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1124
|
+
for (const codeIndex of found) {
|
|
1125
|
+
holdCodeIndexes.set(codeIndex, true);
|
|
1126
|
+
}
|
|
1127
|
+
const compare = [];
|
|
1128
|
+
for (let i = 0; i < dbIndex.columns.length; i++) {
|
|
1129
|
+
const column = dbIndex.columns[i];
|
|
1130
|
+
if (!("expression" in column))
|
|
1131
|
+
continue;
|
|
1132
|
+
compare.push({
|
|
1133
|
+
inDb: column.expression,
|
|
1134
|
+
inCode: found.map(
|
|
1135
|
+
(index) => index.columns[i].expression
|
|
1136
|
+
)
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
if (dbIndex.with) {
|
|
1140
|
+
compare.push({
|
|
1141
|
+
inDb: dbIndex.with,
|
|
1142
|
+
inCode: found.map((index) => index.options.with)
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
if (dbIndex.where) {
|
|
1146
|
+
compare.push({
|
|
1147
|
+
inDb: dbIndex.where,
|
|
1148
|
+
inCode: found.map((index) => index.options.where)
|
|
1149
|
+
});
|
|
1121
1150
|
}
|
|
1151
|
+
wait++;
|
|
1152
|
+
compareExpressions.push({
|
|
1153
|
+
compare,
|
|
1154
|
+
handle(index) {
|
|
1155
|
+
const codeIndex = index === void 0 ? void 0 : found[index];
|
|
1156
|
+
handleIndexChange(
|
|
1157
|
+
changeTableData,
|
|
1158
|
+
dbIndex,
|
|
1159
|
+
dbColumns,
|
|
1160
|
+
codeIndex,
|
|
1161
|
+
index === void 0 ? void 0 : rename[index]
|
|
1162
|
+
);
|
|
1163
|
+
if (codeIndex) {
|
|
1164
|
+
holdCodeIndexes.delete(codeIndex);
|
|
1165
|
+
}
|
|
1166
|
+
if (!--wait && holdCodeIndexes.size) {
|
|
1167
|
+
addIndexes(changeTableData, [...holdCodeIndexes.keys()]);
|
|
1168
|
+
if (!changeTableData.pushedAst) {
|
|
1169
|
+
changeTableData.pushedAst = true;
|
|
1170
|
+
ast.push(changeTableData.changeTableAst);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
});
|
|
1122
1175
|
}
|
|
1123
|
-
const
|
|
1176
|
+
const indexesToAdd = codeIndexes.filter(
|
|
1124
1177
|
(index, i) => !skipCodeIndexes.has(i) && !holdCodeIndexes.has(index)
|
|
1125
1178
|
);
|
|
1126
|
-
if (
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1179
|
+
if (indexesToAdd.length) {
|
|
1180
|
+
addIndexes(
|
|
1181
|
+
changeTableData,
|
|
1182
|
+
indexesToAdd.map((x) => __spreadProps$5(__spreadValues$5({}, x), {
|
|
1183
|
+
columns: x.columnKeys,
|
|
1184
|
+
columnNames: x.columns,
|
|
1185
|
+
options: x.options.include ? __spreadProps$5(__spreadValues$5({}, x.options), { include: x.includeKeys }) : x.options
|
|
1186
|
+
}))
|
|
1187
|
+
);
|
|
1131
1188
|
}
|
|
1132
1189
|
};
|
|
1133
1190
|
const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) => {
|
|
@@ -1138,7 +1195,7 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1138
1195
|
if (!column.data.indexes)
|
|
1139
1196
|
continue;
|
|
1140
1197
|
const name = (_a = column.data.name) != null ? _a : key;
|
|
1141
|
-
if (
|
|
1198
|
+
if (checkForColumnAddOrDrop(shape, name))
|
|
1142
1199
|
continue;
|
|
1143
1200
|
codeIndexes.push(
|
|
1144
1201
|
...column.data.indexes.map(
|
|
@@ -1148,7 +1205,7 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1148
1205
|
} = _c, _e = _d, { collate, opclass, order, weight } = _e, options = __objRest(_e, ["collate", "opclass", "order", "weight"]), index = __objRest(_c, [
|
|
1149
1206
|
"options"
|
|
1150
1207
|
]);
|
|
1151
|
-
return __spreadProps$
|
|
1208
|
+
return __spreadProps$5(__spreadValues$5({
|
|
1152
1209
|
columns: [
|
|
1153
1210
|
{
|
|
1154
1211
|
collate,
|
|
@@ -1159,7 +1216,7 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1159
1216
|
}
|
|
1160
1217
|
]
|
|
1161
1218
|
}, index), {
|
|
1162
|
-
options: options.include ? config.snakeCase ? __spreadProps$
|
|
1219
|
+
options: options.include ? config.snakeCase ? __spreadProps$5(__spreadValues$5({}, options), {
|
|
1163
1220
|
include: toArray(options.include).map(toSnakeCase)
|
|
1164
1221
|
}) : options : options,
|
|
1165
1222
|
columnKeys: [{ collate, opclass, order, weight, column: key }],
|
|
@@ -1171,12 +1228,12 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1171
1228
|
}
|
|
1172
1229
|
if (codeTable.internal.tableData.indexes) {
|
|
1173
1230
|
codeIndexes.push(
|
|
1174
|
-
...codeTable.internal.tableData.indexes.map((x) => __spreadProps$
|
|
1231
|
+
...codeTable.internal.tableData.indexes.map((x) => __spreadProps$5(__spreadValues$5({}, x), {
|
|
1175
1232
|
columns: config.snakeCase ? x.columns.map(
|
|
1176
|
-
(c) => "column" in c ? __spreadProps$
|
|
1233
|
+
(c) => "column" in c ? __spreadProps$5(__spreadValues$5({}, c), { column: toSnakeCase(c.column) }) : c
|
|
1177
1234
|
) : x.columns,
|
|
1178
1235
|
columnKeys: x.columns,
|
|
1179
|
-
options: x.options.include && config.snakeCase ? __spreadProps$
|
|
1236
|
+
options: x.options.include && config.snakeCase ? __spreadProps$5(__spreadValues$5({}, x.options), {
|
|
1180
1237
|
include: toArray(x.options.include).map(toSnakeCase)
|
|
1181
1238
|
}) : x.options,
|
|
1182
1239
|
includeKeys: x.options.include
|
|
@@ -1188,7 +1245,7 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1188
1245
|
const collectCodeComparableIndexes = (config, codeIndexes) => {
|
|
1189
1246
|
return codeIndexes.map((codeIndex) => {
|
|
1190
1247
|
normalizeIndex(codeIndex.options);
|
|
1191
|
-
return indexToComparable(__spreadProps$
|
|
1248
|
+
return indexToComparable(__spreadProps$5(__spreadValues$5({}, codeIndex.options), {
|
|
1192
1249
|
include: codeIndex.options.include === void 0 ? void 0 : config.snakeCase ? toArray(codeIndex.options.include).map(toSnakeCase) : toArray(codeIndex.options.include),
|
|
1193
1250
|
columns: codeIndex.columns,
|
|
1194
1251
|
name: codeIndex.name,
|
|
@@ -1208,7 +1265,7 @@ const normalizeIndex = (index) => {
|
|
|
1208
1265
|
const indexToComparable = (index) => {
|
|
1209
1266
|
let hasExpression = false;
|
|
1210
1267
|
const columns = index.columns.map((column) => {
|
|
1211
|
-
const result = __spreadProps$
|
|
1268
|
+
const result = __spreadProps$5(__spreadValues$5({}, column), {
|
|
1212
1269
|
expression: void 0,
|
|
1213
1270
|
hasExpression: "expression" in column
|
|
1214
1271
|
});
|
|
@@ -1216,7 +1273,7 @@ const indexToComparable = (index) => {
|
|
|
1216
1273
|
hasExpression = true;
|
|
1217
1274
|
return result;
|
|
1218
1275
|
});
|
|
1219
|
-
return __spreadProps$
|
|
1276
|
+
return __spreadProps$5(__spreadValues$5({}, index), {
|
|
1220
1277
|
schemaName: void 0,
|
|
1221
1278
|
tableName: void 0,
|
|
1222
1279
|
with: void 0,
|
|
@@ -1227,6 +1284,19 @@ const indexToComparable = (index) => {
|
|
|
1227
1284
|
hasExpression
|
|
1228
1285
|
});
|
|
1229
1286
|
};
|
|
1287
|
+
const findMatchingIndex = (dbIndex, codeComparableIndexes, codeIndexes, skipCodeIndexes, tableName, config) => {
|
|
1288
|
+
const dbComparableIndex = indexToComparable(dbIndex);
|
|
1289
|
+
const { found, rename } = findMatchingIndexWithoutSql(
|
|
1290
|
+
dbComparableIndex,
|
|
1291
|
+
codeComparableIndexes,
|
|
1292
|
+
codeIndexes,
|
|
1293
|
+
skipCodeIndexes,
|
|
1294
|
+
tableName,
|
|
1295
|
+
config
|
|
1296
|
+
);
|
|
1297
|
+
const foundAndHasSql = found.length && checkIfIndexHasSql(dbComparableIndex);
|
|
1298
|
+
return { found, rename, foundAndHasSql };
|
|
1299
|
+
};
|
|
1230
1300
|
const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes, skipCodeIndexes, tableName, config) => {
|
|
1231
1301
|
var _c, _d;
|
|
1232
1302
|
const found = [];
|
|
@@ -1241,8 +1311,8 @@ const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes
|
|
|
1241
1311
|
let b = codeIndex;
|
|
1242
1312
|
const codeName = (_c = b.name) != null ? _c : getIndexName(tableName, dbColumns);
|
|
1243
1313
|
if (a.name !== b.name) {
|
|
1244
|
-
a = __spreadProps$
|
|
1245
|
-
b = __spreadProps$
|
|
1314
|
+
a = __spreadProps$5(__spreadValues$5({}, a), { name: void 0 });
|
|
1315
|
+
b = __spreadProps$5(__spreadValues$5({}, b), {
|
|
1246
1316
|
name: void 0,
|
|
1247
1317
|
columnKeys: void 0,
|
|
1248
1318
|
includeKeys: void 0
|
|
@@ -1275,16 +1345,33 @@ const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes
|
|
|
1275
1345
|
return { found, rename };
|
|
1276
1346
|
};
|
|
1277
1347
|
const checkIfIndexHasSql = (index) => index.hasWith || index.hasWhere || index.hasExpression;
|
|
1278
|
-
const handleIndexChange = (
|
|
1279
|
-
|
|
1348
|
+
const handleIndexChange = ({
|
|
1349
|
+
changeTableAst,
|
|
1350
|
+
schema,
|
|
1351
|
+
codeTable,
|
|
1352
|
+
changingColumns,
|
|
1353
|
+
delayedAst
|
|
1354
|
+
}, dbIndex, dbColumns, found, rename) => {
|
|
1355
|
+
var _a, _b, _c, _d;
|
|
1280
1356
|
if (!found) {
|
|
1281
|
-
|
|
1357
|
+
const indexName = dbIndex.name === getIndexName(changeTableAst.name, dbColumns) ? void 0 : dbIndex.name;
|
|
1358
|
+
if (dbColumns.length === 1 && "column" in dbColumns[0]) {
|
|
1359
|
+
const column = changingColumns[dbColumns[0].column];
|
|
1360
|
+
if (column) {
|
|
1361
|
+
((_b = (_a = column.from.data).indexes) != null ? _b : _a.indexes = []).push({
|
|
1362
|
+
options: dbIndex,
|
|
1363
|
+
name: indexName
|
|
1364
|
+
});
|
|
1365
|
+
return;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
((_d = (_c = changeTableAst.drop).indexes) != null ? _d : _c.indexes = []).push({
|
|
1282
1369
|
columns: dbColumns,
|
|
1283
1370
|
options: dbIndex,
|
|
1284
|
-
name:
|
|
1371
|
+
name: indexName
|
|
1285
1372
|
});
|
|
1286
1373
|
} else if (rename) {
|
|
1287
|
-
|
|
1374
|
+
delayedAst.push({
|
|
1288
1375
|
type: "renameTableItem",
|
|
1289
1376
|
kind: "INDEX",
|
|
1290
1377
|
tableSchema: schema,
|
|
@@ -1294,26 +1381,40 @@ const handleIndexChange = (ast, { changeTableAst, schema, codeTable }, dbIndex,
|
|
|
1294
1381
|
});
|
|
1295
1382
|
}
|
|
1296
1383
|
};
|
|
1384
|
+
const addIndexes = ({ changeTableAst, changingColumns }, add) => {
|
|
1385
|
+
var _a, _b, _c, _d;
|
|
1386
|
+
const indexes = (_b = (_a = changeTableAst.add).indexes) != null ? _b : _a.indexes = [];
|
|
1387
|
+
for (const index of add) {
|
|
1388
|
+
if (index.columns.length === 1 && "column" in index.columns[0]) {
|
|
1389
|
+
const column = changingColumns[(index.columnNames || index.columns)[0].column];
|
|
1390
|
+
if (column) {
|
|
1391
|
+
((_d = (_c = column.to.data).indexes) != null ? _d : _c.indexes = []).push(index);
|
|
1392
|
+
continue;
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
indexes.push(index);
|
|
1396
|
+
}
|
|
1397
|
+
};
|
|
1297
1398
|
|
|
1298
|
-
var __defProp$
|
|
1299
|
-
var __defProps$
|
|
1300
|
-
var __getOwnPropDescs$
|
|
1301
|
-
var __getOwnPropSymbols$
|
|
1302
|
-
var __hasOwnProp$
|
|
1303
|
-
var __propIsEnum$
|
|
1304
|
-
var __defNormalProp$
|
|
1305
|
-
var __spreadValues$
|
|
1399
|
+
var __defProp$4 = Object.defineProperty;
|
|
1400
|
+
var __defProps$4 = Object.defineProperties;
|
|
1401
|
+
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
1402
|
+
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
1403
|
+
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
1404
|
+
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
1405
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1406
|
+
var __spreadValues$4 = (a, b) => {
|
|
1306
1407
|
for (var prop in b || (b = {}))
|
|
1307
|
-
if (__hasOwnProp$
|
|
1308
|
-
__defNormalProp$
|
|
1309
|
-
if (__getOwnPropSymbols$
|
|
1310
|
-
for (var prop of __getOwnPropSymbols$
|
|
1311
|
-
if (__propIsEnum$
|
|
1312
|
-
__defNormalProp$
|
|
1408
|
+
if (__hasOwnProp$4.call(b, prop))
|
|
1409
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
1410
|
+
if (__getOwnPropSymbols$4)
|
|
1411
|
+
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
1412
|
+
if (__propIsEnum$4.call(b, prop))
|
|
1413
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
1313
1414
|
}
|
|
1314
1415
|
return a;
|
|
1315
1416
|
};
|
|
1316
|
-
var __spreadProps$
|
|
1417
|
+
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
1317
1418
|
const mapMatchToDb = {
|
|
1318
1419
|
FULL: "f",
|
|
1319
1420
|
PARTIAL: "p",
|
|
@@ -1335,14 +1436,14 @@ for (const key in mapActionToDb) {
|
|
|
1335
1436
|
mapActionToCode[mapActionToDb[key]] = key;
|
|
1336
1437
|
}
|
|
1337
1438
|
const processForeignKeys = (config, ast, changeTables, currentSchema, tableShapes) => {
|
|
1338
|
-
var _a, _b, _c;
|
|
1439
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1339
1440
|
for (const changeTableData of changeTables) {
|
|
1340
1441
|
const codeForeignKeys = collectCodeFkeys(
|
|
1341
1442
|
config,
|
|
1342
1443
|
changeTableData,
|
|
1343
1444
|
currentSchema
|
|
1344
1445
|
);
|
|
1345
|
-
const { codeTable, dbTableData, changeTableAst, schema } = changeTableData;
|
|
1446
|
+
const { codeTable, dbTableData, changeTableAst, schema, changingColumns } = changeTableData;
|
|
1346
1447
|
const { shape, add, drop } = changeTableAst;
|
|
1347
1448
|
let changed = false;
|
|
1348
1449
|
for (const dbConstraint of dbTableData.constraints) {
|
|
@@ -1350,13 +1451,13 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1350
1451
|
if (!dbReferences)
|
|
1351
1452
|
continue;
|
|
1352
1453
|
const hasChangedColumn = dbReferences.columns.some(
|
|
1353
|
-
(column) =>
|
|
1454
|
+
(column) => checkForColumnAddOrDrop(shape, column)
|
|
1354
1455
|
);
|
|
1355
1456
|
if (hasChangedColumn)
|
|
1356
1457
|
continue;
|
|
1357
1458
|
const foreignShape = tableShapes[`${dbReferences.foreignSchema}.${dbReferences.foreignTable}`];
|
|
1358
1459
|
const hasForeignChangedColumn = foreignShape && dbReferences.foreignColumns.some(
|
|
1359
|
-
(column) =>
|
|
1460
|
+
(column) => checkForColumnAddOrDrop(foreignShape, column)
|
|
1360
1461
|
);
|
|
1361
1462
|
if (hasForeignChangedColumn)
|
|
1362
1463
|
continue;
|
|
@@ -1379,9 +1480,21 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1379
1480
|
}
|
|
1380
1481
|
}
|
|
1381
1482
|
if (!found) {
|
|
1382
|
-
|
|
1383
|
-
|
|
1483
|
+
const foreignKey = dbForeignKeyToCodeForeignKey(
|
|
1484
|
+
config,
|
|
1485
|
+
dbConstraint,
|
|
1486
|
+
dbReferences
|
|
1384
1487
|
);
|
|
1488
|
+
if (dbReferences.columns.length === 1 && changingColumns[dbReferences.columns[0]]) {
|
|
1489
|
+
const column = changingColumns[dbReferences.columns[0]];
|
|
1490
|
+
((_c = (_b = column.from.data).foreignKeys) != null ? _c : _b.foreignKeys = []).push({
|
|
1491
|
+
fnOrTable: foreignKey.references.fnOrTable,
|
|
1492
|
+
foreignColumns: foreignKey.references.foreignColumns,
|
|
1493
|
+
options: foreignKey.references.options
|
|
1494
|
+
});
|
|
1495
|
+
} else {
|
|
1496
|
+
((_d = drop.constraints) != null ? _d : drop.constraints = []).push(foreignKey);
|
|
1497
|
+
}
|
|
1385
1498
|
changed = true;
|
|
1386
1499
|
} else if (rename) {
|
|
1387
1500
|
ast.push({
|
|
@@ -1395,9 +1508,19 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1395
1508
|
}
|
|
1396
1509
|
}
|
|
1397
1510
|
if (codeForeignKeys.length) {
|
|
1398
|
-
(
|
|
1399
|
-
|
|
1400
|
-
|
|
1511
|
+
const constraints = (_e = add.constraints) != null ? _e : add.constraints = [];
|
|
1512
|
+
for (const { codeConstraint, references } of codeForeignKeys) {
|
|
1513
|
+
if (references.columns.length === 1 && changingColumns[references.columns[0]]) {
|
|
1514
|
+
const column = changingColumns[references.columns[0]];
|
|
1515
|
+
((_g = (_f = column.to.data).foreignKeys) != null ? _g : _f.foreignKeys = []).push({
|
|
1516
|
+
fnOrTable: references.foreignTable,
|
|
1517
|
+
foreignColumns: codeConstraint.references.foreignColumns,
|
|
1518
|
+
options: codeConstraint.references.options
|
|
1519
|
+
});
|
|
1520
|
+
} else {
|
|
1521
|
+
constraints.push(codeConstraint);
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1401
1524
|
changed = true;
|
|
1402
1525
|
}
|
|
1403
1526
|
if (changed && !changeTableData.pushedAst) {
|
|
@@ -1414,7 +1537,7 @@ const collectCodeFkeys = (config, { codeTable, changeTableAst: { shape } }, curr
|
|
|
1414
1537
|
if (!column.data.foreignKeys)
|
|
1415
1538
|
continue;
|
|
1416
1539
|
const name = (_a = column.data.name) != null ? _a : key;
|
|
1417
|
-
if (
|
|
1540
|
+
if (checkForColumnAddOrDrop(shape, name))
|
|
1418
1541
|
continue;
|
|
1419
1542
|
codeForeignKeys.push(
|
|
1420
1543
|
...column.data.foreignKeys.map((x) => {
|
|
@@ -1452,12 +1575,12 @@ const collectCodeFkeys = (config, { codeTable, changeTableAst: { shape } }, curr
|
|
|
1452
1575
|
codeForeignKeys.push(
|
|
1453
1576
|
parseForeignKey(
|
|
1454
1577
|
config,
|
|
1455
|
-
__spreadProps$
|
|
1456
|
-
references: __spreadProps$
|
|
1578
|
+
__spreadProps$4(__spreadValues$4({}, tableConstraint), {
|
|
1579
|
+
references: __spreadProps$4(__spreadValues$4({}, refs), {
|
|
1457
1580
|
fnOrTable
|
|
1458
1581
|
})
|
|
1459
1582
|
}),
|
|
1460
|
-
__spreadProps$
|
|
1583
|
+
__spreadProps$4(__spreadValues$4({}, refs), {
|
|
1461
1584
|
fnOrTable,
|
|
1462
1585
|
columns: config.snakeCase ? refs.columns.map(toSnakeCase) : refs.columns,
|
|
1463
1586
|
foreignColumns: config.snakeCase ? refs.foreignColumns.map(toSnakeCase) : refs.foreignColumns
|
|
@@ -1510,19 +1633,44 @@ const dbForeignKeyToCodeForeignKey = (config, dbConstraint, dbReferences) => ({
|
|
|
1510
1633
|
}
|
|
1511
1634
|
});
|
|
1512
1635
|
|
|
1636
|
+
var __defProp$3 = Object.defineProperty;
|
|
1637
|
+
var __defProps$3 = Object.defineProperties;
|
|
1638
|
+
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
1639
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
1640
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
1641
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
1642
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1643
|
+
var __spreadValues$3 = (a, b) => {
|
|
1644
|
+
for (var prop in b || (b = {}))
|
|
1645
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
1646
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
1647
|
+
if (__getOwnPropSymbols$3)
|
|
1648
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
1649
|
+
if (__propIsEnum$3.call(b, prop))
|
|
1650
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
1651
|
+
}
|
|
1652
|
+
return a;
|
|
1653
|
+
};
|
|
1654
|
+
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
1513
1655
|
const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
1514
1656
|
var _a, _b;
|
|
1515
1657
|
const codeChecks = collectCodeChecks(changeTableData);
|
|
1516
1658
|
const {
|
|
1517
1659
|
dbTableData,
|
|
1518
|
-
changeTableAst: { add,
|
|
1660
|
+
changeTableAst: { add, shape }
|
|
1519
1661
|
} = changeTableData;
|
|
1520
1662
|
const hasDbChecks = dbTableData.constraints.some((c) => c.check);
|
|
1521
1663
|
if (!hasDbChecks) {
|
|
1522
1664
|
if (codeChecks.length) {
|
|
1523
|
-
(
|
|
1524
|
-
|
|
1525
|
-
|
|
1665
|
+
const constraints = (_a = add.constraints) != null ? _a : add.constraints = [];
|
|
1666
|
+
for (const check of codeChecks) {
|
|
1667
|
+
if (check.column && changeTableData.changingColumns[check.column]) {
|
|
1668
|
+
const column = changeTableData.changingColumns[check.column];
|
|
1669
|
+
column.to.data.check = check;
|
|
1670
|
+
} else {
|
|
1671
|
+
constraints.push({ check: check.sql, name: check.name });
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1526
1674
|
}
|
|
1527
1675
|
return;
|
|
1528
1676
|
}
|
|
@@ -1533,7 +1681,7 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1533
1681
|
if (!dbCheck)
|
|
1534
1682
|
continue;
|
|
1535
1683
|
const hasChangedColumn = (_b = dbCheck.columns) == null ? void 0 : _b.some(
|
|
1536
|
-
(column) =>
|
|
1684
|
+
(column) => checkForColumnAddOrDrop(shape, column)
|
|
1537
1685
|
);
|
|
1538
1686
|
if (hasChangedColumn)
|
|
1539
1687
|
continue;
|
|
@@ -1550,7 +1698,7 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1550
1698
|
var _a2;
|
|
1551
1699
|
if (index !== void 0)
|
|
1552
1700
|
return;
|
|
1553
|
-
dropCheck(
|
|
1701
|
+
dropCheck(changeTableData, dbCheck, name);
|
|
1554
1702
|
if (--wait === 0 && !changeTableData.pushedAst) {
|
|
1555
1703
|
changeTableData.pushedAst = true;
|
|
1556
1704
|
((_a2 = add.constraints) != null ? _a2 : add.constraints = []).push(
|
|
@@ -1564,7 +1712,7 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1564
1712
|
}
|
|
1565
1713
|
});
|
|
1566
1714
|
} else {
|
|
1567
|
-
dropCheck(
|
|
1715
|
+
dropCheck(changeTableData, dbCheck, name);
|
|
1568
1716
|
}
|
|
1569
1717
|
}
|
|
1570
1718
|
};
|
|
@@ -1579,9 +1727,11 @@ const collectCodeChecks = ({
|
|
|
1579
1727
|
if (!column.data.check)
|
|
1580
1728
|
continue;
|
|
1581
1729
|
const name = (_a = column.data.name) != null ? _a : key;
|
|
1582
|
-
if (
|
|
1730
|
+
if (checkForColumnAddOrDrop(shape, name))
|
|
1583
1731
|
continue;
|
|
1584
|
-
codeChecks.push(column.data.check)
|
|
1732
|
+
codeChecks.push(__spreadProps$3(__spreadValues$3({}, column.data.check), {
|
|
1733
|
+
column: name
|
|
1734
|
+
}));
|
|
1585
1735
|
}
|
|
1586
1736
|
if (codeTable.internal.tableData.constraints) {
|
|
1587
1737
|
for (const constraint of codeTable.internal.tableData.constraints) {
|
|
@@ -1593,12 +1743,25 @@ const collectCodeChecks = ({
|
|
|
1593
1743
|
}
|
|
1594
1744
|
return codeChecks;
|
|
1595
1745
|
};
|
|
1596
|
-
const dropCheck = (drop, dbCheck, name) => {
|
|
1597
|
-
var _a;
|
|
1598
|
-
(
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1746
|
+
const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name) => {
|
|
1747
|
+
var _a, _b;
|
|
1748
|
+
const constraints = (_a = drop.constraints) != null ? _a : drop.constraints = [];
|
|
1749
|
+
const sql = new RawSQL([
|
|
1750
|
+
[dbCheck.expression]
|
|
1751
|
+
]);
|
|
1752
|
+
if (((_b = dbCheck.columns) == null ? void 0 : _b.length) === 1 && changingColumns[dbCheck.columns[0]]) {
|
|
1753
|
+
const column = changingColumns[dbCheck.columns[0]];
|
|
1754
|
+
column.from.data.name = "i_d";
|
|
1755
|
+
column.from.data.check = {
|
|
1756
|
+
name,
|
|
1757
|
+
sql
|
|
1758
|
+
};
|
|
1759
|
+
} else {
|
|
1760
|
+
constraints.push({
|
|
1761
|
+
name,
|
|
1762
|
+
check: sql
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1602
1765
|
};
|
|
1603
1766
|
|
|
1604
1767
|
var __defProp$2 = Object.defineProperty;
|
|
@@ -1841,7 +2004,9 @@ const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, d
|
|
|
1841
2004
|
add: {},
|
|
1842
2005
|
drop: {}
|
|
1843
2006
|
},
|
|
1844
|
-
pushedAst: false
|
|
2007
|
+
pushedAst: false,
|
|
2008
|
+
changingColumns: {},
|
|
2009
|
+
delayedAst: []
|
|
1845
2010
|
});
|
|
1846
2011
|
tableShapes[`${schema}.${codeTable.table}`] = shape;
|
|
1847
2012
|
};
|
|
@@ -1880,17 +2045,17 @@ const processTableChange = async (adapter, structureToAstCtx, dbStructure, domai
|
|
|
1880
2045
|
typeCastsCache,
|
|
1881
2046
|
verifying
|
|
1882
2047
|
);
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
processIndexes(config, changeTableData, delayedAst, ast, compareExpressions);
|
|
2048
|
+
processPrimaryKey(config, changeTableData);
|
|
2049
|
+
processIndexes(config, changeTableData, ast, compareExpressions);
|
|
1886
2050
|
processChecks(ast, changeTableData, compareExpressions);
|
|
1887
2051
|
const { changeTableAst } = changeTableData;
|
|
1888
2052
|
if (Object.keys(changeTableAst.shape).length || Object.keys(changeTableAst.add).length || Object.keys(changeTableAst.drop).length) {
|
|
1889
2053
|
changeTableData.pushedAst = true;
|
|
1890
2054
|
ast.push(changeTableAst);
|
|
1891
2055
|
}
|
|
1892
|
-
if (delayedAst.length)
|
|
1893
|
-
ast.push(...delayedAst);
|
|
2056
|
+
if (changeTableData.delayedAst.length) {
|
|
2057
|
+
ast.push(...changeTableData.delayedAst);
|
|
2058
|
+
}
|
|
1894
2059
|
};
|
|
1895
2060
|
|
|
1896
2061
|
const composeMigration = async (adapter, config, ast, dbStructure, {
|
|
@@ -2032,6 +2197,12 @@ const report = (ast, config, currentSchema) => {
|
|
|
2032
2197
|
}
|
|
2033
2198
|
case "changeTable": {
|
|
2034
2199
|
const inner = [];
|
|
2200
|
+
const toCodeCtx = {
|
|
2201
|
+
t: "t",
|
|
2202
|
+
table: a.name,
|
|
2203
|
+
migration: true,
|
|
2204
|
+
snakeCase: config.snakeCase
|
|
2205
|
+
};
|
|
2035
2206
|
for (const key in a.shape) {
|
|
2036
2207
|
const changes = toArray(a.shape[key]);
|
|
2037
2208
|
for (const change of changes) {
|
|
@@ -2050,20 +2221,12 @@ const report = (ast, config, currentSchema) => {
|
|
|
2050
2221
|
const changes2 = [];
|
|
2051
2222
|
inner.push(`${yellow("~ change column")} ${name}:`, changes2);
|
|
2052
2223
|
changes2.push(`${yellow("from")}: `);
|
|
2053
|
-
const fromCode =
|
|
2054
|
-
key,
|
|
2055
|
-
change.from.column,
|
|
2056
|
-
config.snakeCase
|
|
2057
|
-
);
|
|
2224
|
+
const fromCode = change.from.column.toCode(toCodeCtx, key);
|
|
2058
2225
|
for (const code2 of fromCode) {
|
|
2059
2226
|
addCode(changes2, code2);
|
|
2060
2227
|
}
|
|
2061
2228
|
changes2.push(` ${yellow("to")}: `);
|
|
2062
|
-
const toCode =
|
|
2063
|
-
key,
|
|
2064
|
-
change.to.column,
|
|
2065
|
-
config.snakeCase
|
|
2066
|
-
);
|
|
2229
|
+
const toCode = change.to.column.toCode(toCodeCtx, key);
|
|
2067
2230
|
for (const code2 of toCode) {
|
|
2068
2231
|
addCode(changes2, code2);
|
|
2069
2232
|
}
|
|
@@ -2672,7 +2835,7 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
2672
2835
|
const hasTableData = Boolean(
|
|
2673
2836
|
ast.primaryKey || ((_a = ast.indexes) == null ? void 0 : _a.length) || ((_b = ast.constraints) == null ? void 0 : _b.length)
|
|
2674
2837
|
);
|
|
2675
|
-
const shapeCode = columnsShapeToCode(ast.
|
|
2838
|
+
const shapeCode = columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
|
|
2676
2839
|
props.push(
|
|
2677
2840
|
`columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
|
|
2678
2841
|
hasTableData ? [shapeCode] : shapeCode,
|
|
@@ -2763,9 +2926,15 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
|
|
|
2763
2926
|
code += `
|
|
2764
2927
|
domains: {
|
|
2765
2928
|
${domains.sort((a, b) => a.name > b.name ? 1 : -1).map(
|
|
2766
|
-
(ast) =>
|
|
2767
|
-
|
|
2768
|
-
|
|
2929
|
+
(ast) => {
|
|
2930
|
+
var _a;
|
|
2931
|
+
return `${quoteObjectKey(
|
|
2932
|
+
ast.schema ? `${ast.schema}.${ast.name}` : ast.name
|
|
2933
|
+
)}: (t) => ${ast.baseType.toCode(
|
|
2934
|
+
{ t: "t", table: ast.name },
|
|
2935
|
+
(_a = ast.baseType.data.name) != null ? _a : ""
|
|
2936
|
+
)},`;
|
|
2937
|
+
}
|
|
2769
2938
|
).join("\n ")}
|
|
2770
2939
|
},`;
|
|
2771
2940
|
}
|