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