orchid-orm 1.57.6 → 1.57.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.
@@ -10,43 +10,33 @@ var typescript = require('typescript');
10
10
  var postgresJs = require('rake-db/postgres-js');
11
11
 
12
12
  const compareSqlExpressions = async (tableExpressions, adapter) => {
13
- if (tableExpressions.length) {
14
- let id = 1;
15
- await Promise.all(
16
- tableExpressions.map(async ({ source, compare, handle }) => {
17
- const viewName = `orchidTmpView${id++}`;
18
- const values = [];
19
- let result;
20
- try {
21
- const results = await adapter.query(
22
- // It is important to run `CREATE TEMPORARY VIEW` and `DROP VIEW` on the same db connection,
23
- // that's why SQLs are combined into a single query.
24
- [
25
- `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
26
- ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
27
- (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
28
- ).join(", ")}`
29
- ).join(", ")} FROM ${source})`,
30
- `SELECT pg_get_viewdef('${viewName}') v`,
31
- `DROP VIEW ${viewName}`
32
- ].join("; "),
33
- values
34
- );
35
- result = results[1];
36
- } catch {
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
- })
48
- );
49
- }
13
+ if (!tableExpressions.length) return;
14
+ let id = 1;
15
+ await Promise.all(
16
+ tableExpressions.map(async ({ source, compare, handle }) => {
17
+ const viewName = `orchidTmpView${id++}`;
18
+ const values = [];
19
+ const combinedQueries = [
20
+ `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
21
+ ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
22
+ (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
23
+ ).join(", ")}`
24
+ ).join(", ")} FROM ${source})`,
25
+ `SELECT pg_get_viewdef('${viewName}') v`,
26
+ `DROP VIEW ${viewName}`
27
+ ].join("; ");
28
+ const result = await adapter.query(combinedQueries, values).then((res) => res[1], orchidCore.noop);
29
+ if (!result) {
30
+ handle();
31
+ return;
32
+ }
33
+ const match = compareSqlExpressionResult(
34
+ result.rows[0].v,
35
+ compare[0].inCode
36
+ );
37
+ handle(match);
38
+ })
39
+ );
50
40
  };
51
41
  const compareSqlExpressionResult = (resultSql, inCode) => {
52
42
  let pos = 7;
@@ -628,6 +618,12 @@ const getColumnDbType = (column, currentSchema) => {
628
618
  return column.dataType;
629
619
  }
630
620
  };
621
+ const getColumnDbTypeQuoted = (column, currentSchema) => {
622
+ const [schema, type] = rakeDb.getSchemaAndTableFromName(
623
+ getColumnDbType(column, currentSchema)
624
+ );
625
+ return schema ? `"${schema}"."${type}"` : `"${type}"`;
626
+ };
631
627
  const renameColumn = (columns, from, to) => {
632
628
  for (let i = 0; i < columns.length; i++) {
633
629
  if (columns[i] === from) {
@@ -1977,7 +1973,7 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1977
1973
  if (!column.dataType) continue;
1978
1974
  const name = column.data.name ?? key;
1979
1975
  names.push(name);
1980
- types.push(getColumnDbType(column, currentSchema));
1976
+ types.push(getColumnDbTypeQuoted(column, currentSchema));
1981
1977
  }
1982
1978
  const tableName = codeTable.table;
1983
1979
  const source = `(VALUES (${types.map((x) => `NULL::${x}`).join(", ")})) "${tableName}"(${names.map((x) => `"${x}"`).join(", ")})`;