orchid-orm 1.37.10 → 1.37.12
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 +310 -175
- package/dist/migrations.js.map +1 -1
- package/dist/migrations.mjs +311 -176
- package/dist/migrations.mjs.map +1 -1
- package/package.json +5 -5
package/dist/migrations.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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';
|
|
1
|
+
import { promptSelect, colors, getSchemaAndTableFromName, getDbTableColumnsChecks, dbColumnToAst, instantiateDbColumn, concatSchemaAndName, encodeColumnDefault, getIndexName, getExcludeName, 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
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';
|
|
@@ -127,18 +127,19 @@ const processSchemas = async (ast, schemas, dbStructure, verifying) => {
|
|
|
127
127
|
}
|
|
128
128
|
for (const schema of createSchemas) {
|
|
129
129
|
if (dropSchemas.length) {
|
|
130
|
-
const
|
|
130
|
+
const i = await promptCreateOrRename(
|
|
131
131
|
"schema",
|
|
132
132
|
schema,
|
|
133
133
|
dropSchemas,
|
|
134
134
|
verifying
|
|
135
135
|
);
|
|
136
|
-
if (
|
|
137
|
-
const from = dropSchemas[
|
|
138
|
-
dropSchemas.splice(
|
|
136
|
+
if (i) {
|
|
137
|
+
const from = dropSchemas[i - 1];
|
|
138
|
+
dropSchemas.splice(i - 1, 1);
|
|
139
139
|
renameSchemaInStructures(dbStructure.tables, from, schema);
|
|
140
140
|
renameSchemaInStructures(dbStructure.views, from, schema);
|
|
141
141
|
renameSchemaInStructures(dbStructure.indexes, from, schema);
|
|
142
|
+
renameSchemaInStructures(dbStructure.excludes, from, schema);
|
|
142
143
|
renameSchemaInStructures(dbStructure.constraints, from, schema);
|
|
143
144
|
renameSchemaInStructures(dbStructure.triggers, from, schema);
|
|
144
145
|
renameSchemaInStructures(dbStructure.enums, from, schema);
|
|
@@ -302,15 +303,15 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
302
303
|
for (const { key, column } of columnsToAdd) {
|
|
303
304
|
if (columnsToDrop.length) {
|
|
304
305
|
const codeName = column.data.name ?? key;
|
|
305
|
-
const
|
|
306
|
+
const i = await promptCreateOrRename(
|
|
306
307
|
"column",
|
|
307
308
|
codeName,
|
|
308
309
|
columnsToDrop.map((x) => x.key),
|
|
309
310
|
verifying
|
|
310
311
|
);
|
|
311
|
-
if (
|
|
312
|
-
const drop = columnsToDrop[
|
|
313
|
-
columnsToDrop.splice(
|
|
312
|
+
if (i) {
|
|
313
|
+
const drop = columnsToDrop[i - 1];
|
|
314
|
+
columnsToDrop.splice(i - 1, 1);
|
|
314
315
|
const from = drop.column.data.name ?? drop.key;
|
|
315
316
|
columnsToChange.set(from, {
|
|
316
317
|
key,
|
|
@@ -321,8 +322,15 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
321
322
|
if (dbTableData.primaryKey) {
|
|
322
323
|
renameColumn(dbTableData.primaryKey.columns, from, to);
|
|
323
324
|
}
|
|
324
|
-
for (const
|
|
325
|
-
for (const column2 of
|
|
325
|
+
for (const index of dbTableData.indexes) {
|
|
326
|
+
for (const column2 of index.columns) {
|
|
327
|
+
if ("column" in column2 && column2.column === from) {
|
|
328
|
+
column2.column = to;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
for (const exclude of dbTableData.excludes) {
|
|
333
|
+
for (const column2 of exclude.columns) {
|
|
326
334
|
if ("column" in column2 && column2.column === from) {
|
|
327
335
|
column2.column = to;
|
|
328
336
|
}
|
|
@@ -544,6 +552,7 @@ const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
|
|
|
544
552
|
...codeColumn.data,
|
|
545
553
|
primaryKey: void 0,
|
|
546
554
|
indexes: void 0,
|
|
555
|
+
excludes: void 0,
|
|
547
556
|
foreignKeys: void 0,
|
|
548
557
|
check: void 0
|
|
549
558
|
};
|
|
@@ -637,8 +646,8 @@ const processDomains = async (ast, adapter, structureToAstCtx, domainsMap, dbStr
|
|
|
637
646
|
tableExpressions.push({
|
|
638
647
|
compare,
|
|
639
648
|
source,
|
|
640
|
-
handle(
|
|
641
|
-
const codeDomain =
|
|
649
|
+
handle(i) {
|
|
650
|
+
const codeDomain = i === void 0 ? void 0 : found[i];
|
|
642
651
|
if (!codeDomain) {
|
|
643
652
|
ast.push(dropAst(dbDomain));
|
|
644
653
|
} else {
|
|
@@ -780,15 +789,15 @@ const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) =
|
|
|
780
789
|
}
|
|
781
790
|
for (const codeEnum of createEnums) {
|
|
782
791
|
if (dropEnums.length) {
|
|
783
|
-
const
|
|
792
|
+
const i = await promptCreateOrRename(
|
|
784
793
|
"enum",
|
|
785
794
|
codeEnum.name,
|
|
786
795
|
dropEnums.map((x) => x.name),
|
|
787
796
|
verifying
|
|
788
797
|
);
|
|
789
|
-
if (
|
|
790
|
-
const dbEnum = dropEnums[
|
|
791
|
-
dropEnums.splice(
|
|
798
|
+
if (i) {
|
|
799
|
+
const dbEnum = dropEnums[i - 1];
|
|
800
|
+
dropEnums.splice(i - 1, 1);
|
|
792
801
|
const fromSchema = dbEnum.schemaName;
|
|
793
802
|
const from = dbEnum.name;
|
|
794
803
|
const toSchema = codeEnum.schema ?? currentSchema;
|
|
@@ -972,86 +981,132 @@ const renamePrimaryKey = ({
|
|
|
972
981
|
}
|
|
973
982
|
};
|
|
974
983
|
|
|
975
|
-
const
|
|
976
|
-
const
|
|
977
|
-
const
|
|
984
|
+
const processIndexesAndExcludes = (config, changeTableData, ast, compareExpressions) => {
|
|
985
|
+
const codeItems = collectCodeIndexes(config, changeTableData);
|
|
986
|
+
const codeComparableItems = collectCodeComparableItems(config, codeItems);
|
|
987
|
+
const skipCodeItems = {
|
|
988
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
989
|
+
excludes: /* @__PURE__ */ new Map()
|
|
990
|
+
};
|
|
991
|
+
const holdCodeItems = {
|
|
992
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
993
|
+
excludes: /* @__PURE__ */ new Map()
|
|
994
|
+
};
|
|
995
|
+
const processParams = {
|
|
978
996
|
config,
|
|
979
|
-
|
|
997
|
+
changeTableData,
|
|
998
|
+
codeComparableItems,
|
|
999
|
+
codeItems,
|
|
1000
|
+
skipCodeItems,
|
|
1001
|
+
holdCodeItems,
|
|
1002
|
+
// counter for async SQL comparisons that are in progress
|
|
1003
|
+
wait: { indexes: 0, excludes: 0 },
|
|
1004
|
+
ast,
|
|
1005
|
+
compareExpressions
|
|
1006
|
+
};
|
|
1007
|
+
processItems(processParams, "indexes");
|
|
1008
|
+
processItems(processParams, "excludes");
|
|
1009
|
+
addMainItems(
|
|
1010
|
+
changeTableData,
|
|
1011
|
+
codeItems,
|
|
1012
|
+
skipCodeItems,
|
|
1013
|
+
holdCodeItems,
|
|
1014
|
+
"indexes"
|
|
980
1015
|
);
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1016
|
+
addMainItems(
|
|
1017
|
+
changeTableData,
|
|
1018
|
+
codeItems,
|
|
1019
|
+
skipCodeItems,
|
|
1020
|
+
holdCodeItems,
|
|
1021
|
+
"excludes"
|
|
1022
|
+
);
|
|
1023
|
+
};
|
|
1024
|
+
const processItems = ({
|
|
1025
|
+
config,
|
|
1026
|
+
changeTableData,
|
|
1027
|
+
codeComparableItems,
|
|
1028
|
+
codeItems,
|
|
1029
|
+
skipCodeItems,
|
|
1030
|
+
holdCodeItems,
|
|
1031
|
+
wait,
|
|
1032
|
+
ast,
|
|
1033
|
+
compareExpressions
|
|
1034
|
+
}, key) => {
|
|
984
1035
|
const {
|
|
985
1036
|
changeTableAst: { shape }
|
|
986
1037
|
} = changeTableData;
|
|
987
|
-
|
|
988
|
-
|
|
1038
|
+
const dbItems = changeTableData.dbTableData[key];
|
|
1039
|
+
for (const dbItem of dbItems) {
|
|
1040
|
+
const hasAddedOrDroppedColumn = dbItem.columns.some(
|
|
989
1041
|
(column) => "column" in column && checkForColumnAddOrDrop(shape, column.column)
|
|
990
1042
|
);
|
|
991
1043
|
if (hasAddedOrDroppedColumn) continue;
|
|
992
|
-
|
|
993
|
-
const { found, rename, foundAndHasSql } =
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1044
|
+
normalizeItem(dbItem);
|
|
1045
|
+
const { found, rename, foundAndHasSql } = findMatchingItem(
|
|
1046
|
+
dbItem,
|
|
1047
|
+
codeComparableItems,
|
|
1048
|
+
codeItems,
|
|
1049
|
+
skipCodeItems,
|
|
998
1050
|
changeTableData.codeTable.table,
|
|
999
|
-
config
|
|
1051
|
+
config,
|
|
1052
|
+
key
|
|
1000
1053
|
);
|
|
1001
|
-
const { columns: dbColumns } =
|
|
1054
|
+
const { columns: dbColumns } = dbItem;
|
|
1002
1055
|
if (!foundAndHasSql) {
|
|
1003
|
-
|
|
1056
|
+
handleItemChange(
|
|
1004
1057
|
changeTableData,
|
|
1005
|
-
|
|
1058
|
+
dbItem,
|
|
1006
1059
|
dbColumns,
|
|
1007
1060
|
found[0],
|
|
1008
|
-
rename[0]
|
|
1061
|
+
rename[0],
|
|
1062
|
+
key
|
|
1009
1063
|
);
|
|
1010
1064
|
continue;
|
|
1011
1065
|
}
|
|
1012
|
-
for (const
|
|
1013
|
-
|
|
1066
|
+
for (const codeItem of found) {
|
|
1067
|
+
holdCodeItems[key].set(codeItem, true);
|
|
1014
1068
|
}
|
|
1015
1069
|
const compare = [];
|
|
1016
|
-
for (let i = 0; i <
|
|
1017
|
-
const column =
|
|
1070
|
+
for (let i = 0; i < dbItem.columns.length; i++) {
|
|
1071
|
+
const column = dbItem.columns[i];
|
|
1018
1072
|
if (!("expression" in column)) continue;
|
|
1019
1073
|
compare.push({
|
|
1020
1074
|
inDb: column.expression,
|
|
1021
1075
|
inCode: found.map(
|
|
1022
|
-
(
|
|
1076
|
+
(x) => x.columns[i].expression
|
|
1023
1077
|
)
|
|
1024
1078
|
});
|
|
1025
1079
|
}
|
|
1026
|
-
if (
|
|
1080
|
+
if (dbItem.with) {
|
|
1027
1081
|
compare.push({
|
|
1028
|
-
inDb:
|
|
1029
|
-
inCode: found.map((
|
|
1082
|
+
inDb: dbItem.with,
|
|
1083
|
+
inCode: found.map((x) => x.options.with)
|
|
1030
1084
|
});
|
|
1031
1085
|
}
|
|
1032
|
-
if (
|
|
1086
|
+
if (dbItem.where) {
|
|
1033
1087
|
compare.push({
|
|
1034
|
-
inDb:
|
|
1035
|
-
inCode: found.map((
|
|
1088
|
+
inDb: dbItem.where,
|
|
1089
|
+
inCode: found.map((x) => x.options.where)
|
|
1036
1090
|
});
|
|
1037
1091
|
}
|
|
1038
|
-
wait++;
|
|
1092
|
+
wait[key]++;
|
|
1039
1093
|
compareExpressions.push({
|
|
1040
1094
|
compare,
|
|
1041
|
-
handle(
|
|
1042
|
-
const
|
|
1043
|
-
|
|
1095
|
+
handle(i) {
|
|
1096
|
+
const codeItem = i === void 0 ? void 0 : found[i];
|
|
1097
|
+
handleItemChange(
|
|
1044
1098
|
changeTableData,
|
|
1045
|
-
|
|
1099
|
+
dbItem,
|
|
1046
1100
|
dbColumns,
|
|
1047
|
-
|
|
1048
|
-
|
|
1101
|
+
codeItem,
|
|
1102
|
+
i === void 0 ? void 0 : rename[i],
|
|
1103
|
+
key
|
|
1049
1104
|
);
|
|
1050
|
-
if (
|
|
1051
|
-
|
|
1105
|
+
if (codeItem) {
|
|
1106
|
+
holdCodeItems[key].delete(codeItem);
|
|
1052
1107
|
}
|
|
1053
|
-
if (!--wait &&
|
|
1054
|
-
|
|
1108
|
+
if (!--wait[key] && holdCodeItems[key].size) {
|
|
1109
|
+
addItems(changeTableData, [...holdCodeItems[key].keys()], key);
|
|
1055
1110
|
if (!changeTableData.pushedAst) {
|
|
1056
1111
|
changeTableData.pushedAst = true;
|
|
1057
1112
|
ast.push(changeTableData.changeTableAst);
|
|
@@ -1060,41 +1115,41 @@ const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
|
1060
1115
|
}
|
|
1061
1116
|
});
|
|
1062
1117
|
}
|
|
1063
|
-
const indexesToAdd = codeIndexes.filter(
|
|
1064
|
-
(index, i) => !skipCodeIndexes.has(i) && !holdCodeIndexes.has(index)
|
|
1065
|
-
);
|
|
1066
|
-
if (indexesToAdd.length) {
|
|
1067
|
-
addIndexes(
|
|
1068
|
-
changeTableData,
|
|
1069
|
-
indexesToAdd.map((x) => ({
|
|
1070
|
-
...x,
|
|
1071
|
-
columns: x.columnKeys,
|
|
1072
|
-
columnNames: x.columns,
|
|
1073
|
-
options: x.options.include ? { ...x.options, include: x.includeKeys } : x.options
|
|
1074
|
-
}))
|
|
1075
|
-
);
|
|
1076
|
-
}
|
|
1077
1118
|
};
|
|
1078
1119
|
const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) => {
|
|
1079
|
-
const
|
|
1120
|
+
const codeItems = { indexes: [], excludes: [] };
|
|
1080
1121
|
for (const key in codeTable.shape) {
|
|
1081
1122
|
const column = codeTable.shape[key];
|
|
1082
|
-
if (!column.data.indexes) continue;
|
|
1123
|
+
if (!column.data.indexes && !column.data.excludes) continue;
|
|
1083
1124
|
const name = column.data.name ?? key;
|
|
1084
1125
|
if (checkForColumnAddOrDrop(shape, name)) continue;
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1126
|
+
pushCodeColumnItems(config, codeItems, key, name, column, "indexes");
|
|
1127
|
+
pushCodeColumnItems(config, codeItems, key, name, column, "excludes");
|
|
1128
|
+
}
|
|
1129
|
+
pushCodeCompositeItems(config, codeTable, codeItems, "indexes");
|
|
1130
|
+
pushCodeCompositeItems(config, codeTable, codeItems, "excludes");
|
|
1131
|
+
return codeItems;
|
|
1132
|
+
};
|
|
1133
|
+
const pushCodeColumnItems = (config, codeItems, columnKey, name, column, key) => {
|
|
1134
|
+
const items = column.data[key];
|
|
1135
|
+
if (!items) return;
|
|
1136
|
+
codeItems[key].push(
|
|
1137
|
+
...items.map(
|
|
1138
|
+
({
|
|
1139
|
+
options: { collate, opclass, order, weight, ...options },
|
|
1140
|
+
with: wi,
|
|
1141
|
+
...index
|
|
1142
|
+
}) => {
|
|
1143
|
+
const w = key === "excludes" ? wi : void 0;
|
|
1144
|
+
return {
|
|
1091
1145
|
columns: [
|
|
1092
1146
|
{
|
|
1093
1147
|
collate,
|
|
1094
1148
|
opclass,
|
|
1095
1149
|
order,
|
|
1096
1150
|
weight,
|
|
1097
|
-
column: name
|
|
1151
|
+
column: name,
|
|
1152
|
+
with: w
|
|
1098
1153
|
}
|
|
1099
1154
|
],
|
|
1100
1155
|
...index,
|
|
@@ -1102,49 +1157,70 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1102
1157
|
...options,
|
|
1103
1158
|
include: toArray(options.include).map(toSnakeCase)
|
|
1104
1159
|
} : options : options,
|
|
1105
|
-
columnKeys: [
|
|
1160
|
+
columnKeys: [
|
|
1161
|
+
{
|
|
1162
|
+
collate,
|
|
1163
|
+
opclass,
|
|
1164
|
+
order,
|
|
1165
|
+
weight,
|
|
1166
|
+
column: columnKey,
|
|
1167
|
+
with: w
|
|
1168
|
+
}
|
|
1169
|
+
],
|
|
1106
1170
|
includeKeys: options.include
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
)
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
)
|
|
1174
|
+
);
|
|
1175
|
+
};
|
|
1176
|
+
const pushCodeCompositeItems = (config, codeTable, codeItems, key) => {
|
|
1177
|
+
const items = codeTable.internal.tableData[key];
|
|
1178
|
+
if (!items) return;
|
|
1179
|
+
codeItems[key].push(
|
|
1180
|
+
...items.map((x) => ({
|
|
1181
|
+
...x,
|
|
1182
|
+
columns: config.snakeCase ? x.columns.map(
|
|
1183
|
+
(c) => "column" in c ? { ...c, column: toSnakeCase(c.column) } : c
|
|
1184
|
+
) : x.columns,
|
|
1185
|
+
columnKeys: x.columns,
|
|
1186
|
+
options: x.options.include && config.snakeCase ? {
|
|
1187
|
+
...x.options,
|
|
1188
|
+
include: toArray(x.options.include).map(toSnakeCase)
|
|
1189
|
+
} : x.options,
|
|
1190
|
+
includeKeys: x.options.include
|
|
1191
|
+
}))
|
|
1192
|
+
);
|
|
1193
|
+
};
|
|
1194
|
+
const collectCodeComparableItems = (config, codeItems) => {
|
|
1195
|
+
return {
|
|
1196
|
+
indexes: collectCodeComparableItemsType(config, codeItems, "indexes"),
|
|
1197
|
+
excludes: collectCodeComparableItemsType(config, codeItems, "excludes")
|
|
1198
|
+
};
|
|
1199
|
+
};
|
|
1200
|
+
const collectCodeComparableItemsType = (config, codeItems, key) => {
|
|
1201
|
+
return codeItems[key].map((codeItem) => {
|
|
1202
|
+
normalizeItem(codeItem.options);
|
|
1203
|
+
return itemToComparable({
|
|
1204
|
+
...codeItem.options,
|
|
1205
|
+
include: codeItem.options.include === void 0 ? void 0 : config.snakeCase ? toArray(codeItem.options.include).map(toSnakeCase) : toArray(codeItem.options.include),
|
|
1206
|
+
columns: codeItem.columns,
|
|
1207
|
+
name: codeItem.name,
|
|
1208
|
+
columnKeys: codeItem.columnKeys,
|
|
1209
|
+
includeKeys: codeItem.includeKeys
|
|
1139
1210
|
});
|
|
1140
1211
|
});
|
|
1141
1212
|
};
|
|
1142
|
-
const
|
|
1143
|
-
if (
|
|
1144
|
-
if (!
|
|
1145
|
-
if (
|
|
1213
|
+
const normalizeItem = (item) => {
|
|
1214
|
+
if (item.using === "btree") item.using = void 0;
|
|
1215
|
+
if (!item.unique) item.unique = void 0;
|
|
1216
|
+
if (item.nullsNotDistinct === false) item.nullsNotDistinct = void 0;
|
|
1217
|
+
if (item.exclude) {
|
|
1218
|
+
for (let i = 0; i < item.columns.length; i++) {
|
|
1219
|
+
item.columns[i].with = item.exclude[i];
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1146
1222
|
};
|
|
1147
|
-
const
|
|
1223
|
+
const itemToComparable = (index) => {
|
|
1148
1224
|
let hasExpression = false;
|
|
1149
1225
|
const columns = index.columns.map((column) => {
|
|
1150
1226
|
const result = {
|
|
@@ -1167,30 +1243,43 @@ const indexToComparable = (index) => {
|
|
|
1167
1243
|
hasExpression
|
|
1168
1244
|
};
|
|
1169
1245
|
};
|
|
1170
|
-
const
|
|
1171
|
-
const
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1246
|
+
const findMatchingItem = (dbItem, codeComparableItems, codeItems, skipCodeItems, tableName, config, key) => {
|
|
1247
|
+
const dbComparableItem = itemToComparable(
|
|
1248
|
+
key === "indexes" ? dbItem : {
|
|
1249
|
+
...dbItem,
|
|
1250
|
+
exclude: void 0,
|
|
1251
|
+
columns: dbItem.columns.map((column, i) => ({
|
|
1252
|
+
...column,
|
|
1253
|
+
with: dbItem.exclude[i]
|
|
1254
|
+
}))
|
|
1255
|
+
}
|
|
1256
|
+
);
|
|
1257
|
+
const { found, rename } = findMatchingItemWithoutSql(
|
|
1258
|
+
dbComparableItem,
|
|
1259
|
+
codeComparableItems,
|
|
1260
|
+
codeItems,
|
|
1261
|
+
skipCodeItems,
|
|
1177
1262
|
tableName,
|
|
1178
|
-
config
|
|
1263
|
+
config,
|
|
1264
|
+
key
|
|
1179
1265
|
);
|
|
1180
|
-
const foundAndHasSql = found.length &&
|
|
1266
|
+
const foundAndHasSql = found.length && checkIfItemHasSql(dbComparableItem);
|
|
1181
1267
|
return { found, rename, foundAndHasSql };
|
|
1182
1268
|
};
|
|
1183
|
-
const
|
|
1269
|
+
const findMatchingItemWithoutSql = (dbItem, codeComparableItems, codeItems, skipCodeItems, tableName, config, key) => {
|
|
1184
1270
|
const found = [];
|
|
1185
1271
|
const rename = [];
|
|
1186
|
-
const { columns: dbColumns, ...
|
|
1187
|
-
for (let i = 0; i <
|
|
1188
|
-
if (
|
|
1189
|
-
const { columns: codeColumns, ...
|
|
1272
|
+
const { columns: dbColumns, ...dbItemWithoutColumns } = dbItem;
|
|
1273
|
+
for (let i = 0; i < codeComparableItems[key].length; i++) {
|
|
1274
|
+
if (skipCodeItems[key].has(i)) continue;
|
|
1275
|
+
const { columns: codeColumns, ...codeItem } = codeComparableItems[key][i];
|
|
1190
1276
|
if (dbColumns.length === codeColumns.length && !dbColumns.some((dbColumn, i2) => !deepCompare(dbColumn, codeColumns[i2]))) {
|
|
1191
|
-
let a =
|
|
1192
|
-
let b =
|
|
1193
|
-
const codeName = b.name ??
|
|
1277
|
+
let a = dbItemWithoutColumns;
|
|
1278
|
+
let b = codeItem;
|
|
1279
|
+
const codeName = b.name ?? (key === "indexes" ? getIndexName : getExcludeName)(
|
|
1280
|
+
tableName,
|
|
1281
|
+
dbColumns
|
|
1282
|
+
);
|
|
1194
1283
|
if (a.name !== b.name) {
|
|
1195
1284
|
a = { ...a, name: void 0 };
|
|
1196
1285
|
b = {
|
|
@@ -1203,9 +1292,9 @@ const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes
|
|
|
1203
1292
|
b.language = config.language ?? "english";
|
|
1204
1293
|
}
|
|
1205
1294
|
if (deepCompare(a, b)) {
|
|
1206
|
-
found.push(
|
|
1295
|
+
found.push(codeItems[key][i]);
|
|
1207
1296
|
rename.push(
|
|
1208
|
-
|
|
1297
|
+
dbItemWithoutColumns.name !== codeName ? codeName : void 0
|
|
1209
1298
|
);
|
|
1210
1299
|
}
|
|
1211
1300
|
} else {
|
|
@@ -1213,69 +1302,96 @@ const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes
|
|
|
1213
1302
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1214
1303
|
columnKeys,
|
|
1215
1304
|
includeKeys,
|
|
1216
|
-
...
|
|
1217
|
-
} =
|
|
1218
|
-
if (deepCompare(
|
|
1219
|
-
found.push(
|
|
1305
|
+
...codeItemWithoutKeys
|
|
1306
|
+
} = codeItem;
|
|
1307
|
+
if (deepCompare(dbItemWithoutColumns, codeItemWithoutKeys)) {
|
|
1308
|
+
found.push(codeItems[key][i]);
|
|
1220
1309
|
rename.push(void 0);
|
|
1221
1310
|
}
|
|
1222
1311
|
}
|
|
1223
|
-
if (found.length && !
|
|
1224
|
-
|
|
1312
|
+
if (found.length && !checkIfItemHasSql(codeItem)) {
|
|
1313
|
+
skipCodeItems[key].set(i, true);
|
|
1225
1314
|
}
|
|
1226
1315
|
}
|
|
1227
1316
|
}
|
|
1228
1317
|
return { found, rename };
|
|
1229
1318
|
};
|
|
1230
|
-
const
|
|
1231
|
-
const
|
|
1319
|
+
const checkIfItemHasSql = (x) => x.hasWith || x.hasWhere || x.hasExpression;
|
|
1320
|
+
const handleItemChange = ({
|
|
1232
1321
|
changeTableAst,
|
|
1233
1322
|
schema,
|
|
1234
1323
|
codeTable,
|
|
1235
1324
|
changingColumns,
|
|
1236
1325
|
delayedAst
|
|
1237
|
-
},
|
|
1326
|
+
}, dbItem, dbColumns, found, rename, key) => {
|
|
1238
1327
|
var _a, _b;
|
|
1239
1328
|
if (!found) {
|
|
1240
|
-
const
|
|
1329
|
+
const name = dbItem.name === (key === "indexes" ? getIndexName : getExcludeName)(
|
|
1330
|
+
changeTableAst.name,
|
|
1331
|
+
dbColumns
|
|
1332
|
+
) ? void 0 : dbItem.name;
|
|
1241
1333
|
if (dbColumns.length === 1 && "column" in dbColumns[0]) {
|
|
1242
|
-
const
|
|
1334
|
+
const dbColumn = dbColumns[0];
|
|
1335
|
+
const column = changingColumns[dbColumn.column];
|
|
1243
1336
|
if (column) {
|
|
1244
|
-
((_a = column.from.data)
|
|
1245
|
-
options:
|
|
1246
|
-
name
|
|
1337
|
+
((_a = column.from.data)[key] ?? (_a[key] = [])).push({
|
|
1338
|
+
options: dbItem,
|
|
1339
|
+
name,
|
|
1340
|
+
with: key === "indexes" ? void 0 : dbColumn.with
|
|
1247
1341
|
});
|
|
1248
1342
|
return;
|
|
1249
1343
|
}
|
|
1250
1344
|
}
|
|
1251
|
-
((_b = changeTableAst.drop)
|
|
1345
|
+
((_b = changeTableAst.drop)[key] ?? (_b[key] = [])).push({
|
|
1252
1346
|
columns: dbColumns,
|
|
1253
|
-
options:
|
|
1254
|
-
name
|
|
1347
|
+
options: dbItem,
|
|
1348
|
+
name
|
|
1255
1349
|
});
|
|
1256
1350
|
} else if (rename) {
|
|
1257
1351
|
delayedAst.push({
|
|
1258
1352
|
type: "renameTableItem",
|
|
1259
|
-
kind: "INDEX",
|
|
1353
|
+
kind: key === "indexes" ? "INDEX" : "CONSTRAINT",
|
|
1260
1354
|
tableSchema: schema,
|
|
1261
1355
|
tableName: codeTable.table,
|
|
1262
|
-
from:
|
|
1356
|
+
from: dbItem.name,
|
|
1263
1357
|
to: rename
|
|
1264
1358
|
});
|
|
1265
1359
|
}
|
|
1266
1360
|
};
|
|
1267
|
-
const
|
|
1361
|
+
const addMainItems = (changeTableData, codeItems, skipCodeItems, holdCodeItems, key) => {
|
|
1362
|
+
const itemsToAdd = codeItems[key].filter(
|
|
1363
|
+
(item, i) => !skipCodeItems[key].has(i) && !holdCodeItems[key].has(item)
|
|
1364
|
+
);
|
|
1365
|
+
if (itemsToAdd.length) {
|
|
1366
|
+
addItems(
|
|
1367
|
+
changeTableData,
|
|
1368
|
+
itemsToAdd.map((x) => ({
|
|
1369
|
+
...x,
|
|
1370
|
+
columns: x.columnKeys,
|
|
1371
|
+
columnNames: x.columns,
|
|
1372
|
+
options: x.options.include ? { ...x.options, include: x.includeKeys } : x.options
|
|
1373
|
+
})),
|
|
1374
|
+
key
|
|
1375
|
+
);
|
|
1376
|
+
}
|
|
1377
|
+
};
|
|
1378
|
+
const addItems = ({ changeTableAst, changingColumns }, add, key) => {
|
|
1268
1379
|
var _a, _b;
|
|
1269
|
-
const
|
|
1270
|
-
for (const
|
|
1271
|
-
if (
|
|
1272
|
-
const column = changingColumns[(
|
|
1380
|
+
const items = (_a = changeTableAst.add)[key] ?? (_a[key] = []);
|
|
1381
|
+
for (const item of add) {
|
|
1382
|
+
if (item.columns.length === 1 && "column" in item.columns[0]) {
|
|
1383
|
+
const column = changingColumns[(item.columnNames || item.columns)[0].column];
|
|
1273
1384
|
if (column) {
|
|
1274
|
-
((_b = column.to.data)
|
|
1385
|
+
((_b = column.to.data)[key] ?? (_b[key] = [])).push(
|
|
1386
|
+
key === "indexes" ? item : {
|
|
1387
|
+
...item,
|
|
1388
|
+
with: item.columns[0].with
|
|
1389
|
+
}
|
|
1390
|
+
);
|
|
1275
1391
|
continue;
|
|
1276
1392
|
}
|
|
1277
1393
|
}
|
|
1278
|
-
|
|
1394
|
+
items.push(item);
|
|
1279
1395
|
}
|
|
1280
1396
|
};
|
|
1281
1397
|
|
|
@@ -1532,13 +1648,13 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1532
1648
|
inCode: codeChecks.map((check) => check.sql)
|
|
1533
1649
|
}
|
|
1534
1650
|
],
|
|
1535
|
-
handle(
|
|
1536
|
-
if (
|
|
1651
|
+
handle(i) {
|
|
1652
|
+
if (i !== void 0) return;
|
|
1537
1653
|
dropCheck(changeTableData, dbCheck, name);
|
|
1538
1654
|
if (--wait === 0 && !changeTableData.pushedAst) {
|
|
1539
1655
|
changeTableData.pushedAst = true;
|
|
1540
1656
|
(add.constraints ?? (add.constraints = [])).push(
|
|
1541
|
-
...codeChecks.filter((_,
|
|
1657
|
+
...codeChecks.filter((_, i2) => !foundCodeChecks.has(i2)).map((check) => ({
|
|
1542
1658
|
name: check.name,
|
|
1543
1659
|
check: check.sql
|
|
1544
1660
|
}))
|
|
@@ -1769,15 +1885,15 @@ const applyCompareSql = async (compareSql, adapter) => {
|
|
|
1769
1885
|
const applyCreateOrRenameTables = async (dbStructure, createTables, dropTables, changeTables, tableShapes, currentSchema, ast, verifying) => {
|
|
1770
1886
|
for (const codeTable of createTables) {
|
|
1771
1887
|
if (dropTables.length) {
|
|
1772
|
-
const
|
|
1888
|
+
const i = await promptCreateOrRename(
|
|
1773
1889
|
"table",
|
|
1774
1890
|
codeTable.table,
|
|
1775
1891
|
dropTables.map((x) => x.name),
|
|
1776
1892
|
verifying
|
|
1777
1893
|
);
|
|
1778
|
-
if (
|
|
1779
|
-
const dbTable = dropTables[
|
|
1780
|
-
dropTables.splice(
|
|
1894
|
+
if (i) {
|
|
1895
|
+
const dbTable = dropTables[i - 1];
|
|
1896
|
+
dropTables.splice(i - 1, 1);
|
|
1781
1897
|
ast.push({
|
|
1782
1898
|
type: "renameType",
|
|
1783
1899
|
kind: "TABLE",
|
|
@@ -1859,7 +1975,7 @@ const processTableChange = async (adapter, structureToAstCtx, dbStructure, domai
|
|
|
1859
1975
|
verifying
|
|
1860
1976
|
);
|
|
1861
1977
|
processPrimaryKey(config, changeTableData);
|
|
1862
|
-
|
|
1978
|
+
processIndexesAndExcludes(config, changeTableData, ast, compareExpressions);
|
|
1863
1979
|
processChecks(ast, changeTableData, compareExpressions);
|
|
1864
1980
|
const { changeTableAst } = changeTableData;
|
|
1865
1981
|
if (Object.keys(changeTableAst.shape).length || Object.keys(changeTableAst.add).length || Object.keys(changeTableAst.drop).length) {
|
|
@@ -1965,6 +2081,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
1965
2081
|
const counters = {
|
|
1966
2082
|
column: 0,
|
|
1967
2083
|
index: a.indexes?.length ?? 0,
|
|
2084
|
+
exclude: a.excludes?.length ?? 0,
|
|
1968
2085
|
"foreign key": a.constraints?.reduce(
|
|
1969
2086
|
(sum, c) => c.references ? sum + 1 : sum,
|
|
1970
2087
|
0
|
|
@@ -1983,6 +2100,9 @@ const report = (ast, config, currentSchema) => {
|
|
|
1983
2100
|
if (column.data.indexes) {
|
|
1984
2101
|
counters.index += column.data.indexes.length;
|
|
1985
2102
|
}
|
|
2103
|
+
if (column.data.excludes) {
|
|
2104
|
+
counters.exclude += column.data.excludes.length;
|
|
2105
|
+
}
|
|
1986
2106
|
if (column.data.foreignKeys) {
|
|
1987
2107
|
counters["foreign key"] += column.data.foreignKeys.length;
|
|
1988
2108
|
}
|
|
@@ -2020,13 +2140,13 @@ const report = (ast, config, currentSchema) => {
|
|
|
2020
2140
|
for (const change of changes) {
|
|
2021
2141
|
if (change.type === "add" || change.type === "drop") {
|
|
2022
2142
|
const column = change.item;
|
|
2023
|
-
const { primaryKey, indexes, foreignKeys, check } = column.data;
|
|
2143
|
+
const { primaryKey, indexes, excludes, foreignKeys, check } = column.data;
|
|
2024
2144
|
inner.push(
|
|
2025
2145
|
`${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
|
|
2026
2146
|
return `${fnOrTableToString(
|
|
2027
2147
|
fk.fnOrTable
|
|
2028
2148
|
)}(${fk.foreignColumns.join(", ")})`;
|
|
2029
|
-
}).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${check ? `, checks ${check.sql.toSQL({ values: [] })}` : ""}`
|
|
2149
|
+
}).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${excludes?.length ? excludes.length === 1 ? ", has exclude" : `, has ${excludes.length} excludes` : ""}${check ? `, checks ${check.sql.toSQL({ values: [] })}` : ""}`
|
|
2030
2150
|
);
|
|
2031
2151
|
} else if (change.type === "change") {
|
|
2032
2152
|
const name = change.from.column?.data.name ?? key;
|
|
@@ -2067,6 +2187,13 @@ const report = (ast, config, currentSchema) => {
|
|
|
2067
2187
|
);
|
|
2068
2188
|
}
|
|
2069
2189
|
}
|
|
2190
|
+
if (a.drop.excludes) {
|
|
2191
|
+
for (const exclude of a.drop.excludes) {
|
|
2192
|
+
inner.push(
|
|
2193
|
+
`${red(`- drop exclude`)} on (${exclude.columns.map((c) => "column" in c ? c.column : c.expression).join(", ")})`
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2070
2197
|
if (a.drop.constraints) {
|
|
2071
2198
|
for (const { references } of a.drop.constraints) {
|
|
2072
2199
|
if (!references) continue;
|
|
@@ -2106,6 +2233,13 @@ const report = (ast, config, currentSchema) => {
|
|
|
2106
2233
|
);
|
|
2107
2234
|
}
|
|
2108
2235
|
}
|
|
2236
|
+
if (a.add.excludes) {
|
|
2237
|
+
for (const exclude of a.add.excludes) {
|
|
2238
|
+
inner.push(
|
|
2239
|
+
`${green(`+ add exclude`)} on (${exclude.columns.map((c) => "column" in c ? c.column : c.expression).join(", ")})`
|
|
2240
|
+
);
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2109
2243
|
if (a.add.constraints) {
|
|
2110
2244
|
for (const { references } of a.add.constraints) {
|
|
2111
2245
|
if (!references) continue;
|
|
@@ -2353,6 +2487,7 @@ const migrateAndPullStructures = async (options, config, afterPull) => {
|
|
|
2353
2487
|
tables: [],
|
|
2354
2488
|
views: [],
|
|
2355
2489
|
indexes: [],
|
|
2490
|
+
excludes: [],
|
|
2356
2491
|
constraints: [],
|
|
2357
2492
|
triggers: [],
|
|
2358
2493
|
extensions: [],
|
|
@@ -2591,7 +2726,7 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
2591
2726
|
props.push("noPrimaryKey = true;");
|
|
2592
2727
|
}
|
|
2593
2728
|
const hasTableData = Boolean(
|
|
2594
|
-
ast.primaryKey || ast.indexes?.length || ast.constraints?.length
|
|
2729
|
+
ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
|
|
2595
2730
|
);
|
|
2596
2731
|
const shapeCode = columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
|
|
2597
2732
|
props.push(
|