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.
@@ -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 { colors, toSnakeCase, deepCompare, emptyArray, toArray, getFreeSetAlias, exhaustive, toCamelCase, addCode, pluralize, codeToString, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
2
+ import { noop, colors, toSnakeCase, deepCompare, emptyArray, toArray, getFreeSetAlias, exhaustive, toCamelCase, addCode, pluralize, codeToString, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
3
3
  import { EnumColumn, ArrayColumn, getColumnBaseType, RawSQL, VirtualColumn, DomainColumn, UnknownColumn, defaultSchemaConfig, columnsShapeToCode, pushTableDataCode } from 'pqb';
4
4
  import path from 'node:path';
5
5
  import { pathToFileURL } from 'url';
@@ -8,43 +8,33 @@ import typescript from 'typescript';
8
8
  export * from 'rake-db/postgres-js';
9
9
 
10
10
  const compareSqlExpressions = async (tableExpressions, adapter) => {
11
- if (tableExpressions.length) {
12
- let id = 1;
13
- await Promise.all(
14
- tableExpressions.map(async ({ source, compare, handle }) => {
15
- const viewName = `orchidTmpView${id++}`;
16
- const values = [];
17
- let result;
18
- try {
19
- const results = await adapter.query(
20
- // It is important to run `CREATE TEMPORARY VIEW` and `DROP VIEW` on the same db connection,
21
- // that's why SQLs are combined into a single query.
22
- [
23
- `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
24
- ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
25
- (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
26
- ).join(", ")}`
27
- ).join(", ")} FROM ${source})`,
28
- `SELECT pg_get_viewdef('${viewName}') v`,
29
- `DROP VIEW ${viewName}`
30
- ].join("; "),
31
- values
32
- );
33
- result = results[1];
34
- } catch {
35
- }
36
- if (!result) {
37
- handle();
38
- return;
39
- }
40
- const match = compareSqlExpressionResult(
41
- result.rows[0].v,
42
- compare[0].inCode
43
- );
44
- handle(match);
45
- })
46
- );
47
- }
11
+ if (!tableExpressions.length) return;
12
+ let id = 1;
13
+ await Promise.all(
14
+ tableExpressions.map(async ({ source, compare, handle }) => {
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).then((res) => res[1], noop);
27
+ if (!result) {
28
+ handle();
29
+ return;
30
+ }
31
+ const match = compareSqlExpressionResult(
32
+ result.rows[0].v,
33
+ compare[0].inCode
34
+ );
35
+ handle(match);
36
+ })
37
+ );
48
38
  };
49
39
  const compareSqlExpressionResult = (resultSql, inCode) => {
50
40
  let pos = 7;
@@ -626,6 +616,12 @@ const getColumnDbType = (column, currentSchema) => {
626
616
  return column.dataType;
627
617
  }
628
618
  };
619
+ const getColumnDbTypeQuoted = (column, currentSchema) => {
620
+ const [schema, type] = getSchemaAndTableFromName(
621
+ getColumnDbType(column, currentSchema)
622
+ );
623
+ return schema ? `"${schema}"."${type}"` : `"${type}"`;
624
+ };
629
625
  const renameColumn = (columns, from, to) => {
630
626
  for (let i = 0; i < columns.length; i++) {
631
627
  if (columns[i] === from) {
@@ -1975,7 +1971,7 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
1975
1971
  if (!column.dataType) continue;
1976
1972
  const name = column.data.name ?? key;
1977
1973
  names.push(name);
1978
- types.push(getColumnDbType(column, currentSchema));
1974
+ types.push(getColumnDbTypeQuoted(column, currentSchema));
1979
1975
  }
1980
1976
  const tableName = codeTable.table;
1981
1977
  const source = `(VALUES (${types.map((x) => `NULL::${x}`).join(", ")})) "${tableName}"(${names.map((x) => `"${x}"`).join(", ")})`;