orchid-orm 1.58.3 → 1.58.5

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.
@@ -1,5 +1,5 @@
1
1
  import { promptSelect, getSchemaAndTableFromName, getDbTableColumnsChecks, dbColumnToAst, instantiateDbColumn, concatSchemaAndName, encodeColumnDefault, getIndexName, getExcludeName, getConstraintName, tableToAst, getDbStructureTableData, makeDomainsMap, astToMigration, createMigrationInterface, introspectDbSchema, makeStructureToAstCtx, makeFileVersion, writeMigrationFile, migrateAndClose, migrate, structureToAst, saveMigratedVersion, rakeDbCommands } from 'rake-db';
2
- import { noop, colors, EnumColumn, ArrayColumn, toSnakeCase, getColumnBaseType, deepCompare, RawSQL, emptyArray, toArray, getFreeSetAlias, VirtualColumn, exhaustive, toCamelCase, addCode, pluralize, codeToString, DomainColumn, UnknownColumn, defaultSchemaConfig, toPascalCase, getImportPath, singleQuote, columnsShapeToCode, pushTableDataCode, quoteObjectKey, pathToLog } from 'pqb';
2
+ import { colors, EnumColumn, ArrayColumn, toSnakeCase, getColumnBaseType, deepCompare, RawSQL, emptyArray, toArray, getFreeSetAlias, VirtualColumn, exhaustive, toCamelCase, addCode, pluralize, codeToString, DomainColumn, UnknownColumn, defaultSchemaConfig, toPascalCase, getImportPath, singleQuote, columnsShapeToCode, pushTableDataCode, quoteObjectKey, pathToLog } from 'pqb';
3
3
  import path from 'node:path';
4
4
  import { pathToFileURL } from 'url';
5
5
  import fs from 'fs/promises';
@@ -22,7 +22,14 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
22
22
  `SELECT pg_get_viewdef('${viewName}') v`,
23
23
  `DROP VIEW ${viewName}`
24
24
  ].join("; ");
25
- const result = await adapter.query(combinedQueries, values).then((res) => res[1], noop);
25
+ const result = await adapter.query(combinedQueries, values).then(
26
+ (res) => res[1],
27
+ (err) => {
28
+ if (err.code !== "42704") {
29
+ throw err;
30
+ }
31
+ }
32
+ );
26
33
  if (!result) {
27
34
  handle();
28
35
  return;
@@ -604,15 +611,13 @@ const getColumnDbType = (column, currentSchema) => {
604
611
  let type = item instanceof EnumColumn ? item.enumName : item.dataType;
605
612
  type = type.startsWith(currentSchema + ".") ? type.slice(currentSchema.length + 1) : type;
606
613
  return type + "[]".repeat(column.data.arrayDims);
607
- } else if (column.data.isOfCustomType) {
614
+ } else {
608
615
  let type = column.dataType;
609
616
  const i = type.indexOf("(");
610
617
  if (i !== -1) {
611
618
  type = type.slice(0, i);
612
619
  }
613
- return type.includes(".") ? type : currentSchema + "." + type;
614
- } else {
615
- return column.dataType;
620
+ return column.data.isOfCustomType ? type.includes(".") ? type : currentSchema + "." + type : type;
616
621
  }
617
622
  };
618
623
  const getColumnDbTypeQuoted = (column, currentSchema) => {
@@ -634,7 +639,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
634
639
  structureToAstCtx,
635
640
  currentSchema,
636
641
  internal: { generatorIgnore }
637
- }) => {
642
+ }, pendingDbTypes) => {
638
643
  const codeDomains = [];
639
644
  if (domains) {
640
645
  for (const { schemaName, name, column } of domains) {
@@ -721,6 +726,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
721
726
  toSchema: first.schemaName,
722
727
  to: first.name
723
728
  });
729
+ pendingDbTypes.add(first.schemaName, first.name);
724
730
  }
725
731
  codeDomains.splice(i, 1);
726
732
  } else {
@@ -730,6 +736,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
730
736
  for (const codeDomain of codeDomains) {
731
737
  if (!holdCodeDomains.has(codeDomain)) {
732
738
  ast.push(createAst(codeDomain));
739
+ pendingDbTypes.add(codeDomain.schemaName, codeDomain.name);
733
740
  }
734
741
  }
735
742
  if (tableExpressions.length) {
@@ -737,6 +744,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
737
744
  if (holdCodeDomains.size) {
738
745
  for (const codeDomain of holdCodeDomains.keys()) {
739
746
  ast.push(createAst(codeDomain));
747
+ pendingDbTypes.add(codeDomain.schemaName, codeDomain.name);
740
748
  }
741
749
  }
742
750
  }
@@ -819,7 +827,7 @@ const processEnums = async (ast, dbStructure, {
819
827
  currentSchema,
820
828
  verifying,
821
829
  internal: { generatorIgnore }
822
- }) => {
830
+ }, pendingDbTypes) => {
823
831
  const createEnums = [];
824
832
  const dropEnums = [];
825
833
  for (const [, codeEnum] of enums) {
@@ -837,7 +845,7 @@ const processEnums = async (ast, dbStructure, {
837
845
  }
838
846
  const codeEnum = enums.get(`${dbEnum.schemaName}.${dbEnum.name}`);
839
847
  if (codeEnum) {
840
- changeEnum(ast, dbEnum, codeEnum);
848
+ changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
841
849
  continue;
842
850
  }
843
851
  const i = createEnums.findIndex((x) => x.name === dbEnum.name);
@@ -855,7 +863,8 @@ const processEnums = async (ast, dbStructure, {
855
863
  toSchema,
856
864
  to: dbEnum.name
857
865
  });
858
- changeEnum(ast, dbEnum, codeEnum2);
866
+ pendingDbTypes.add(toSchema, dbEnum.name);
867
+ changeEnum(ast, dbEnum, codeEnum2, pendingDbTypes);
859
868
  continue;
860
869
  }
861
870
  dropEnums.push(dbEnum);
@@ -893,7 +902,8 @@ const processEnums = async (ast, dbStructure, {
893
902
  toSchema,
894
903
  to
895
904
  });
896
- changeEnum(ast, dbEnum, codeEnum);
905
+ pendingDbTypes.add(toSchema, to);
906
+ changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
897
907
  continue;
898
908
  }
899
909
  }
@@ -902,6 +912,7 @@ const processEnums = async (ast, dbStructure, {
902
912
  action: "create",
903
913
  ...codeEnum
904
914
  });
915
+ pendingDbTypes.add(codeEnum.schema, codeEnum.name);
905
916
  }
906
917
  for (const dbEnum of dropEnums) {
907
918
  ast.push({
@@ -913,7 +924,7 @@ const processEnums = async (ast, dbStructure, {
913
924
  });
914
925
  }
915
926
  };
916
- const changeEnum = (ast, dbEnum, codeEnum) => {
927
+ const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
917
928
  const { values: dbValues } = dbEnum;
918
929
  const { values: codeValues, schema, name } = codeEnum;
919
930
  if (dbValues.length < codeValues.length) {
@@ -925,6 +936,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
925
936
  name,
926
937
  values: codeValues.filter((value) => !dbValues.includes(value))
927
938
  });
939
+ pendingDbTypes.add(schema, name);
928
940
  return;
929
941
  }
930
942
  } else if (dbValues.length > codeValues.length) {
@@ -936,6 +948,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
936
948
  name,
937
949
  values: dbValues.filter((value) => !codeValues.includes(value))
938
950
  });
951
+ pendingDbTypes.add(schema, name);
939
952
  return;
940
953
  }
941
954
  } else if (!dbValues.some((value) => !codeValues.includes(value))) {
@@ -948,6 +961,7 @@ const changeEnum = (ast, dbEnum, codeEnum) => {
948
961
  fromValues: dbValues,
949
962
  toValues: codeValues
950
963
  });
964
+ pendingDbTypes.add(schema, name);
951
965
  };
952
966
  const renameColumnsTypeSchema = (dbStructure, from, to) => {
953
967
  for (const table of dbStructure.tables) {
@@ -1826,7 +1840,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, {
1826
1840
  currentSchema,
1827
1841
  internal: { generatorIgnore },
1828
1842
  verifying
1829
- }) => {
1843
+ }, pendingDbTypes) => {
1830
1844
  const createTables = collectCreateTables(
1831
1845
  tables,
1832
1846
  dbStructure,
@@ -1864,7 +1878,8 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, {
1864
1878
  config,
1865
1879
  compareSql,
1866
1880
  tableExpressions,
1867
- verifying
1881
+ verifying,
1882
+ pendingDbTypes
1868
1883
  );
1869
1884
  processForeignKeys(config, ast, changeTables, currentSchema, tableShapes);
1870
1885
  await Promise.all([
@@ -1942,7 +1957,7 @@ const applyChangeTableSchemas = (changeTableSchemas, currentSchema, ast) => {
1942
1957
  });
1943
1958
  }
1944
1959
  };
1945
- const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying) => {
1960
+ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying, pendingDbTypes) => {
1946
1961
  const compareExpressions = [];
1947
1962
  const typeCastsCache = {};
1948
1963
  for (const changeTableData of changeTables) {
@@ -1969,8 +1984,11 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1969
1984
  const column = codeTable.shape[key];
1970
1985
  if (!column.dataType) continue;
1971
1986
  const name = column.data.name ?? key;
1972
- names.push(name);
1973
- types.push(getColumnDbTypeQuoted(column, currentSchema));
1987
+ const type = getColumnDbTypeQuoted(column, currentSchema);
1988
+ if (!pendingDbTypes.set.has(type)) {
1989
+ names.push(name);
1990
+ types.push(type);
1991
+ }
1974
1992
  }
1975
1993
  const tableName = codeTable.table;
1976
1994
  const source = `(VALUES (${types.map((x) => `NULL::${x}`).join(", ")})) "${tableName}"(${names.map((x) => `"${x}"`).join(", ")})`;
@@ -2115,14 +2133,38 @@ const processTableChange = async (adapter, structureToAstCtx, dbStructure, domai
2115
2133
  }
2116
2134
  };
2117
2135
 
2136
+ class PendingDbTypes {
2137
+ constructor() {
2138
+ this.set = /* @__PURE__ */ new Set();
2139
+ }
2140
+ add(schemaName = "public", name) {
2141
+ this.set.add(`"${schemaName}"."${name}"`);
2142
+ }
2143
+ }
2118
2144
  const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2119
2145
  const { structureToAstCtx, currentSchema } = params;
2120
2146
  const domainsMap = makeDomainsMap(structureToAstCtx, dbStructure);
2121
2147
  await processSchemas(ast, dbStructure, params);
2122
2148
  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);
2149
+ const pendingDbTypes = new PendingDbTypes();
2150
+ await processDomains(
2151
+ ast,
2152
+ adapter,
2153
+ domainsMap,
2154
+ dbStructure,
2155
+ params,
2156
+ pendingDbTypes
2157
+ );
2158
+ await processEnums(ast, dbStructure, params, pendingDbTypes);
2159
+ await processTables(
2160
+ ast,
2161
+ domainsMap,
2162
+ adapter,
2163
+ dbStructure,
2164
+ config,
2165
+ params,
2166
+ pendingDbTypes
2167
+ );
2126
2168
  return astToMigration(currentSchema, config, ast);
2127
2169
  };
2128
2170