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