orchid-orm 1.72.8 → 1.72.9

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,10 +1,18 @@
1
1
  import { astToMigration, concatSchemaAndName, createMigrationInterface, dbColumnToAst, encodeColumnDefault, getConstraintName, getDbStructureTableData, getDbTableColumnsChecks, getDbVersion, getExcludeName, getIndexName, getMigrationsSchemaAndTable, getSchemaAndTableFromName, instantiateDbColumn, introspectDbSchema, makeDomainsMap, makeFileVersion, makeStructureToAstCtx, migrate, migrateAndClose, promptSelect, rakeDbCommands, saveMigratedVersion, structureToAst, tableToAst, writeMigrationFile } from "rake-db";
2
- import { ArrayColumn, DomainColumn, EnumColumn, RawSql, UnknownColumn, VirtualColumn, addCode, codeToString, colors, columnsShapeToCode, deepCompare, emptyArray, emptyObject, exhaustive, getColumnBaseType, getDriverErrorCode, getFreeSetAlias, getImportPath, getQuerySchema, getSupportedDefaultPrivileges, pathToLog, pluralize, pushTableDataCode, quoteObjectKey, raw, singleQuote, toArray, toCamelCase, toPascalCase, toSnakeCase } from "pqb/internal";
2
+ import { ArrayColumn, DomainColumn, EnumColumn, RawSql, UnknownColumn, VirtualColumn, addCode, codeToString, colors, columnsShapeToCode, deepCompare, emptyArray, emptyObject, exhaustive, getColumnBaseType, getDriverErrorCode, getFreeSetAlias, getImportPath, getQuerySchema, getSupportedDefaultPrivileges, pathToLog, pluralize, pushTableDataCode, queryToSql, quoteObjectKey, raw, rawSqlToSql, singleQuote, sqlToRawSql, toArray, toCamelCase, toPascalCase, toSnakeCase } from "pqb/internal";
3
3
  import path from "node:path";
4
4
  import { pathToFileURL } from "node:url";
5
5
  import fs from "node:fs/promises";
6
6
  import typescript from "typescript";
7
7
  export * from "rake-db";
8
+ const viewDataToSql = (viewData, viewName) => {
9
+ return sqlToRawSql(viewDataToQuerySql(viewData, viewName));
10
+ };
11
+ const viewDataToQuerySql = (viewData, viewName) => {
12
+ if (viewData.query) return queryToSql(viewData.query);
13
+ if (viewData.sql !== void 0) return rawSqlToSql(viewData.sql);
14
+ throw new Error(`Either sql or query is required for view ${viewName}`);
15
+ };
8
16
  const compareSqlExpressions = async (expressions, adapter) => {
9
17
  if (!expressions.length) return;
10
18
  let id = 1;
@@ -1725,6 +1733,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, { st
1725
1733
  };
1726
1734
  const collectCreateTables = (tables, dbStructure, currentSchema) => {
1727
1735
  return tables.reduce((acc, codeTable) => {
1736
+ if (codeTable.internal.generatorIgnored) return acc;
1728
1737
  const tableSchema = codeTable.q.schema ?? currentSchema;
1729
1738
  if (!dbStructure.tables.some((t) => t.name === codeTable.table && t.schemaName === tableSchema)) acc.push(codeTable);
1730
1739
  return acc;
@@ -1744,7 +1753,7 @@ const collectChangeAndDropTables = (adapter, config, tables, dbStructure, curren
1744
1753
  });
1745
1754
  const { schema: migrationsSchema = "public", table: migrationsTable } = getMigrationsSchemaAndTable(adapter, config);
1746
1755
  for (const dbTable of dbStructure.tables) {
1747
- if (dbTable.name === migrationsTable && dbTable.schemaName === migrationsSchema || generatorIgnore?.schemas?.includes(dbTable.schemaName) || ignoreTables?.some(({ schema, table }) => table === dbTable.name && schema === dbTable.schemaName)) continue;
1756
+ if (dbTable.name === migrationsTable && dbTable.schemaName === migrationsSchema || generatorIgnore?.schemas?.includes(dbTable.schemaName) || ignoreTables?.some(({ schema, table }) => table === dbTable.name && schema === dbTable.schemaName) || isDefinitionIgnoredDbTable(tables, dbTable, currentSchema)) continue;
1748
1757
  const codeTable = tables.find((t) => t.table === dbTable.name && (t.q.schema ?? currentSchema) === dbTable.schemaName);
1749
1758
  if (codeTable) {
1750
1759
  addChangeTable(dbStructure, changeTables, tableShapes, currentSchema, dbTable, codeTable);
@@ -1769,6 +1778,15 @@ const collectChangeAndDropTables = (adapter, config, tables, dbStructure, curren
1769
1778
  tableShapes
1770
1779
  };
1771
1780
  };
1781
+ const isDefinitionIgnoredDbTable = (tables, dbTable, currentSchema) => {
1782
+ let hasIgnoredSameName = false;
1783
+ for (const codeTable of tables) {
1784
+ if (codeTable.table !== dbTable.name) continue;
1785
+ if ((codeTable.q.schema ?? currentSchema) === dbTable.schemaName) return !!codeTable.internal.generatorIgnored;
1786
+ if (codeTable.internal.generatorIgnored) hasIgnoredSameName = true;
1787
+ }
1788
+ return hasIgnoredSameName;
1789
+ };
1772
1790
  const applyChangeTableSchemas = (changeTableSchemas, currentSchema, ast) => {
1773
1791
  for (const { codeTable, dbTable } of changeTableSchemas) {
1774
1792
  const fromSchema = dbTable.schemaName;
@@ -2553,7 +2571,7 @@ const pushRecreateView$1 = (ast, from, to) => {
2553
2571
  };
2554
2572
  const codeViewToAst$1 = (view, currentSchema, action) => {
2555
2573
  const schema = view.q.schema ?? currentSchema;
2556
- const sql = typeof view.viewData.sql === "string" ? raw({ raw: view.viewData.sql }) : view.viewData.sql ?? raw({ raw: "" });
2574
+ const sql = viewDataToSql(view.viewData, view.name);
2557
2575
  return {
2558
2576
  type: "view",
2559
2577
  action,
@@ -2691,7 +2709,7 @@ const pushRecreateView = (ast, from, to) => {
2691
2709
  };
2692
2710
  const codeViewToAst = (view, currentSchema, action) => {
2693
2711
  const schema = view.q.schema ?? currentSchema;
2694
- const sql = typeof view.viewData.sql === "string" ? raw({ raw: view.viewData.sql }) : view.viewData.sql ?? raw({ raw: "" });
2712
+ const sql = viewDataToSql(view.viewData, view.name);
2695
2713
  return {
2696
2714
  type: "materializedView",
2697
2715
  action,
@@ -3136,6 +3154,7 @@ const generate = async (adapters, config, args, afterPull) => {
3136
3154
  const adapterSchema = adapter.getSchema();
3137
3155
  const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
3138
3156
  const codeItems = await getActualItems(db, currentSchema, internal, columnTypes);
3157
+ const generatorIgnore = getGeneratorIgnoreWithDefinitionIgnoredTableLikes(internal.generatorIgnore, codeItems.tables, codeItems.views);
3139
3158
  const effectiveGrants = getEffectiveGrants(internal.grants, codeItems);
3140
3159
  const generateMigrationParams = {
3141
3160
  structureToAstCtx: makeStructureToAstCtx(config, currentSchema),
@@ -3143,6 +3162,7 @@ const generate = async (adapters, config, args, afterPull) => {
3143
3162
  currentSchema,
3144
3163
  internal: {
3145
3164
  ...internal,
3165
+ generatorIgnore,
3146
3166
  grants: effectiveGrants
3147
3167
  }
3148
3168
  };
@@ -3265,6 +3285,26 @@ const getEffectiveGrants = (grants, codeItems) => {
3265
3285
  }
3266
3286
  return effectiveGrants.length ? effectiveGrants : void 0;
3267
3287
  };
3288
+ const getGeneratorIgnoreWithDefinitionIgnoredTableLikes = (generatorIgnore, tables, views) => {
3289
+ const ignoredTables = new Set(generatorIgnore?.tables);
3290
+ const ignoredViews = new Set(generatorIgnore?.views);
3291
+ let hasDefinitionIgnoredTableLikes = false;
3292
+ for (const table of tables) {
3293
+ if (!table.internal.generatorIgnored) continue;
3294
+ hasDefinitionIgnoredTableLikes = true;
3295
+ ignoredTables.add(table.q.schema ? `${table.q.schema}.${table.table}` : table.table);
3296
+ }
3297
+ for (const view of views) {
3298
+ if (!view.internal.generatorIgnored) continue;
3299
+ hasDefinitionIgnoredTableLikes = true;
3300
+ ignoredViews.add(view.q.schema ? `${view.q.schema}.${view.name}` : view.name);
3301
+ }
3302
+ return hasDefinitionIgnoredTableLikes ? {
3303
+ ...generatorIgnore,
3304
+ tables: [...ignoredTables],
3305
+ views: [...ignoredViews]
3306
+ } : generatorIgnore;
3307
+ };
3268
3308
  const compareDbStructures = (a, b, i, path) => {
3269
3309
  let err;
3270
3310
  if (typeof a !== typeof b) err = true;
@@ -3307,7 +3347,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
3307
3347
  },
3308
3348
  q: { schema: getQuerySchema(table) }
3309
3349
  });
3310
- for (const key in table.relations) {
3350
+ if (!table.internal.generatorIgnored) for (const key in table.relations) {
3311
3351
  const column = table.shape[key];
3312
3352
  if (column && "joinTable" in column) processHasAndBelongsToManyColumn(column, habtmTables, codeItems);
3313
3353
  }