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