orchid-orm 1.58.6 → 1.58.7

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.
@@ -9,38 +9,42 @@ export * from 'rake-db/postgres-js';
9
9
  const compareSqlExpressions = async (tableExpressions, adapter) => {
10
10
  if (!tableExpressions.length) return;
11
11
  let id = 1;
12
- await Promise.all(
13
- tableExpressions.map(async ({ source, compare, handle }) => {
14
- const viewName = `orchidTmpView${id++}`;
15
- const values = [];
16
- const combinedQueries = [
17
- `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
18
- ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
19
- (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
20
- ).join(", ")}`
21
- ).join(", ")} FROM ${source})`,
22
- `SELECT pg_get_viewdef('${viewName}') v`,
23
- `DROP VIEW ${viewName}`
24
- ].join("; ");
25
- const result = await adapter.query(combinedQueries, values).then(
26
- (res) => res[1],
27
- (err) => {
28
- if (err.code !== "42704") {
29
- throw err;
30
- }
12
+ for (const { source, compare, handle } of tableExpressions) {
13
+ const viewName = `orchidTmpView${id++}`;
14
+ const values = [];
15
+ const combinedQueries = [
16
+ `SAVEPOINT "${viewName}"`,
17
+ `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
18
+ ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
19
+ (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
20
+ ).join(", ")}`
21
+ ).join(", ")} FROM ${source})`,
22
+ `SELECT pg_get_viewdef('${viewName}') v`,
23
+ `DROP VIEW ${viewName}`,
24
+ `RELEASE SAVEPOINT "${viewName}"`
25
+ ].join("; ");
26
+ const result = await adapter.query(combinedQueries, values).then(
27
+ (res) => {
28
+ const results = res;
29
+ return results.length === 2 ? results[1] : results[2];
30
+ },
31
+ async (err) => {
32
+ await adapter.query(`ROLLBACK TO SAVEPOINT "${viewName}"`);
33
+ if (err.code !== "42704") {
34
+ throw err;
31
35
  }
32
- );
33
- if (!result) {
34
- handle();
35
- return;
36
36
  }
37
- const match = compareSqlExpressionResult(
38
- result.rows[0].v,
39
- compare[0].inCode
40
- );
41
- handle(match);
42
- })
43
- );
37
+ );
38
+ if (!result) {
39
+ handle();
40
+ return;
41
+ }
42
+ const match = compareSqlExpressionResult(
43
+ result.rows[0].v,
44
+ compare[0].inCode
45
+ );
46
+ handle(match);
47
+ }
44
48
  };
45
49
  const compareSqlExpressionResult = (resultSql, inCode) => {
46
50
  let pos = 7;
@@ -605,27 +609,23 @@ const getColumnDbType = (column, currentSchema) => {
605
609
  const [schema = currentSchema, name] = getSchemaAndTableFromName(
606
610
  column.enumName
607
611
  );
608
- return column.enumName = `${schema}.${name}`;
612
+ return `${schema}.${name}`;
609
613
  } else if (column instanceof ArrayColumn) {
610
614
  const { item } = column.data;
611
615
  let type = item instanceof EnumColumn ? item.enumName : item.dataType;
612
616
  type = type.startsWith(currentSchema + ".") ? type.slice(currentSchema.length + 1) : type;
613
617
  return type + "[]".repeat(column.data.arrayDims);
614
- } else {
618
+ } else if (column.data.isOfCustomType) {
615
619
  let type = column.dataType;
616
620
  const i = type.indexOf("(");
617
621
  if (i !== -1) {
618
622
  type = type.slice(0, i);
619
623
  }
620
- return column.data.isOfCustomType ? type.includes(".") ? type : currentSchema + "." + type : type;
624
+ return type.includes(".") ? type : currentSchema + "." + type;
625
+ } else {
626
+ return column.dataType;
621
627
  }
622
628
  };
623
- const getColumnDbTypeQuoted = (column, currentSchema) => {
624
- const [schema, type] = getSchemaAndTableFromName(
625
- getColumnDbType(column, currentSchema)
626
- );
627
- return schema ? `"${schema}"."${type}"` : `"${type}"`;
628
- };
629
629
  const renameColumn = (columns, from, to) => {
630
630
  for (let i = 0; i < columns.length; i++) {
631
631
  if (columns[i] === from) {
@@ -1984,7 +1984,7 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1984
1984
  const column = codeTable.shape[key];
1985
1985
  if (!column.dataType) continue;
1986
1986
  const name = column.data.name ?? key;
1987
- const type = getColumnDbTypeQuoted(column, currentSchema);
1987
+ const type = getColumnDbTypeForComparison(column, currentSchema);
1988
1988
  if (!pendingDbTypes.set.has(type)) {
1989
1989
  names.push(name);
1990
1990
  types.push(type);
@@ -1998,6 +1998,29 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1998
1998
  }
1999
1999
  }
2000
2000
  };
2001
+ const getColumnDbTypeForComparison = (column, currentSchema) => {
2002
+ if (column instanceof EnumColumn) {
2003
+ return "text";
2004
+ }
2005
+ if (column instanceof ArrayColumn) {
2006
+ return getColumnDbTypeForComparison(column.data.item, currentSchema) + "[]".repeat(column.data.arrayDims);
2007
+ }
2008
+ let type = column.dataType;
2009
+ const i = type.indexOf("(");
2010
+ let append = "";
2011
+ if (i !== -1) {
2012
+ type = type.slice(0, i);
2013
+ append = type.slice(i);
2014
+ }
2015
+ const j = type.indexOf(".");
2016
+ if (j === -1) {
2017
+ let result = `"${type}"${append}`;
2018
+ if (column.data.isOfCustomType) result = `"${currentSchema}".${result}`;
2019
+ return result;
2020
+ } else {
2021
+ return `"${type.slice(j)}"."${type.slice(0, j)}"${append}`;
2022
+ }
2023
+ };
2001
2024
  const applyCompareSql = async (compareSql, adapter) => {
2002
2025
  if (compareSql.expressions.length) {
2003
2026
  const {