orchid-orm 1.58.3 → 1.58.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -634,7 +634,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
634
634
  structureToAstCtx,
635
635
  currentSchema,
636
636
  internal: { generatorIgnore }
637
- }) => {
637
+ }, pendingDbTypes) => {
638
638
  const codeDomains = [];
639
639
  if (domains) {
640
640
  for (const { schemaName, name, column } of domains) {
@@ -721,6 +721,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
721
721
  toSchema: first.schemaName,
722
722
  to: first.name
723
723
  });
724
+ pendingDbTypes.add(first.schemaName, first.name);
724
725
  }
725
726
  codeDomains.splice(i, 1);
726
727
  } else {
@@ -730,6 +731,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
730
731
  for (const codeDomain of codeDomains) {
731
732
  if (!holdCodeDomains.has(codeDomain)) {
732
733
  ast.push(createAst(codeDomain));
734
+ pendingDbTypes.add(codeDomain.schemaName, codeDomain.name);
733
735
  }
734
736
  }
735
737
  if (tableExpressions.length) {
@@ -737,6 +739,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
737
739
  if (holdCodeDomains.size) {
738
740
  for (const codeDomain of holdCodeDomains.keys()) {
739
741
  ast.push(createAst(codeDomain));
742
+ pendingDbTypes.add(codeDomain.schemaName, codeDomain.name);
740
743
  }
741
744
  }
742
745
  }
@@ -819,7 +822,7 @@ const processEnums = async (ast, dbStructure, {
819
822
  currentSchema,
820
823
  verifying,
821
824
  internal: { generatorIgnore }
822
- }) => {
825
+ }, pendingDbTypes) => {
823
826
  const createEnums = [];
824
827
  const dropEnums = [];
825
828
  for (const [, codeEnum] of enums) {
@@ -837,7 +840,7 @@ const processEnums = async (ast, dbStructure, {
837
840
  }
838
841
  const codeEnum = enums.get(`${dbEnum.schemaName}.${dbEnum.name}`);
839
842
  if (codeEnum) {
840
- changeEnum(ast, dbEnum, codeEnum);
843
+ changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
841
844
  continue;
842
845
  }
843
846
  const i = createEnums.findIndex((x) => x.name === dbEnum.name);
@@ -855,7 +858,8 @@ const processEnums = async (ast, dbStructure, {
855
858
  toSchema,
856
859
  to: dbEnum.name
857
860
  });
858
- changeEnum(ast, dbEnum, codeEnum2);
861
+ pendingDbTypes.add(toSchema, dbEnum.name);
862
+ changeEnum(ast, dbEnum, codeEnum2, pendingDbTypes);
859
863
  continue;
860
864
  }
861
865
  dropEnums.push(dbEnum);
@@ -893,7 +897,8 @@ const processEnums = async (ast, dbStructure, {
893
897
  toSchema,
894
898
  to
895
899
  });
896
- changeEnum(ast, dbEnum, codeEnum);
900
+ pendingDbTypes.add(toSchema, to);
901
+ changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
897
902
  continue;
898
903
  }
899
904
  }
@@ -902,6 +907,7 @@ const processEnums = async (ast, dbStructure, {
902
907
  action: "create",
903
908
  ...codeEnum
904
909
  });
910
+ pendingDbTypes.add(codeEnum.schema, codeEnum.name);
905
911
  }
906
912
  for (const dbEnum of dropEnums) {
907
913
  ast.push({
@@ -913,7 +919,7 @@ const processEnums = async (ast, dbStructure, {
913
919
  });
914
920
  }
915
921
  };
916
- const changeEnum = (ast, dbEnum, codeEnum) => {
922
+ const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
917
923
  const { values: dbValues } = dbEnum;
918
924
  const { values: codeValues, schema, name } = codeEnum;
919
925
  if (dbValues.length < codeValues.length) {
@@ -925,6 +931,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
925
931
  name,
926
932
  values: codeValues.filter((value) => !dbValues.includes(value))
927
933
  });
934
+ pendingDbTypes.add(schema, name);
928
935
  return;
929
936
  }
930
937
  } else if (dbValues.length > codeValues.length) {
@@ -936,6 +943,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
936
943
  name,
937
944
  values: dbValues.filter((value) => !codeValues.includes(value))
938
945
  });
946
+ pendingDbTypes.add(schema, name);
939
947
  return;
940
948
  }
941
949
  } else if (!dbValues.some((value) => !codeValues.includes(value))) {
@@ -948,6 +956,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
948
956
  fromValues: dbValues,
949
957
  toValues: codeValues
950
958
  });
959
+ pendingDbTypes.add(schema, name);
951
960
  };
952
961
  const renameColumnsTypeSchema = (dbStructure, from, to) => {
953
962
  for (const table of dbStructure.tables) {
@@ -1826,7 +1835,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, {
1826
1835
  currentSchema,
1827
1836
  internal: { generatorIgnore },
1828
1837
  verifying
1829
- }) => {
1838
+ }, pendingDbTypes) => {
1830
1839
  const createTables = collectCreateTables(
1831
1840
  tables,
1832
1841
  dbStructure,
@@ -1864,7 +1873,8 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, {
1864
1873
  config,
1865
1874
  compareSql,
1866
1875
  tableExpressions,
1867
- verifying
1876
+ verifying,
1877
+ pendingDbTypes
1868
1878
  );
1869
1879
  processForeignKeys(config, ast, changeTables, currentSchema, tableShapes);
1870
1880
  await Promise.all([
@@ -1942,7 +1952,7 @@ const applyChangeTableSchemas = (changeTableSchemas, currentSchema, ast) => {
1942
1952
  });
1943
1953
  }
1944
1954
  };
1945
- const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying) => {
1955
+ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying, pendingDbTypes) => {
1946
1956
  const compareExpressions = [];
1947
1957
  const typeCastsCache = {};
1948
1958
  for (const changeTableData of changeTables) {
@@ -1969,8 +1979,11 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1969
1979
  const column = codeTable.shape[key];
1970
1980
  if (!column.dataType) continue;
1971
1981
  const name = column.data.name ?? key;
1972
- names.push(name);
1973
- types.push(getColumnDbTypeQuoted(column, currentSchema));
1982
+ const type = getColumnDbTypeQuoted(column, currentSchema);
1983
+ if (!pendingDbTypes.set.has(type)) {
1984
+ names.push(name);
1985
+ types.push(type);
1986
+ }
1974
1987
  }
1975
1988
  const tableName = codeTable.table;
1976
1989
  const source = `(VALUES (${types.map((x) => `NULL::${x}`).join(", ")})) "${tableName}"(${names.map((x) => `"${x}"`).join(", ")})`;
@@ -2115,14 +2128,38 @@ const processTableChange = async (adapter, structureToAstCtx, dbStructure, domai
2115
2128
  }
2116
2129
  };
2117
2130
 
2131
+ class PendingDbTypes {
2132
+ constructor() {
2133
+ this.set = /* @__PURE__ */ new Set();
2134
+ }
2135
+ add(schemaName = "public", name) {
2136
+ this.set.add(`"${schemaName}"."${name}"`);
2137
+ }
2138
+ }
2118
2139
  const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2119
2140
  const { structureToAstCtx, currentSchema } = params;
2120
2141
  const domainsMap = makeDomainsMap(structureToAstCtx, dbStructure);
2121
2142
  await processSchemas(ast, dbStructure, params);
2122
2143
  processExtensions(ast, dbStructure, params);
2123
- await processDomains(ast, adapter, domainsMap, dbStructure, params);
2124
- await processEnums(ast, dbStructure, params);
2125
- await processTables(ast, domainsMap, adapter, dbStructure, config, params);
2144
+ const pendingDbTypes = new PendingDbTypes();
2145
+ await processDomains(
2146
+ ast,
2147
+ adapter,
2148
+ domainsMap,
2149
+ dbStructure,
2150
+ params,
2151
+ pendingDbTypes
2152
+ );
2153
+ await processEnums(ast, dbStructure, params, pendingDbTypes);
2154
+ await processTables(
2155
+ ast,
2156
+ domainsMap,
2157
+ adapter,
2158
+ dbStructure,
2159
+ config,
2160
+ params,
2161
+ pendingDbTypes
2162
+ );
2126
2163
  return astToMigration(currentSchema, config, ast);
2127
2164
  };
2128
2165