orchid-orm 1.58.6 → 1.58.8

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