rake-db 2.4.43 → 2.5.0

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.
package/dist/index.js CHANGED
@@ -1683,9 +1683,13 @@ const migrateOrRollback = async (options, config, args, up) => {
1683
1683
  }
1684
1684
  };
1685
1685
  const changeCache = {};
1686
+ const begin = {
1687
+ text: "BEGIN",
1688
+ values: orchidCore.emptyArray
1689
+ };
1686
1690
  const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
1687
1691
  var _a;
1688
- const asts = await db.transaction(async (tx) => {
1692
+ const asts = await db.transaction(begin, async (tx) => {
1689
1693
  const db2 = createMigrationInterface(tx, up, config);
1690
1694
  clearChanges();
1691
1695
  let changes = changeCache[file.path];
@@ -1749,7 +1753,7 @@ const execute = async (options, sql) => {
1749
1753
  }
1750
1754
  };
1751
1755
  const createOrDrop = async (options, adminOptions, config, args) => {
1752
- var _a, _b, _c, _d;
1756
+ var _a, _b, _c, _d, _e, _f;
1753
1757
  const params = getDatabaseAndUserFromOptions(options);
1754
1758
  const result = await execute(
1755
1759
  setAdapterOptions(adminOptions, { database: "postgres" }),
@@ -1787,6 +1791,21 @@ Don't use this command for database service providers, only for a local db.`;
1787
1791
  if (!args.create)
1788
1792
  return;
1789
1793
  const db = new pqb.Adapter(options);
1794
+ const { schema } = db;
1795
+ if (schema) {
1796
+ db.schema = void 0;
1797
+ try {
1798
+ await db.query(`CREATE SCHEMA "${schema}"`);
1799
+ (_e = config.logger) == null ? void 0 : _e.log(`Created schema ${schema}`);
1800
+ } catch (err) {
1801
+ if (err.code === "42P06") {
1802
+ (_f = config.logger) == null ? void 0 : _f.log(`Schema ${schema} already exists`);
1803
+ } else {
1804
+ throw err;
1805
+ }
1806
+ }
1807
+ db.schema = schema;
1808
+ }
1790
1809
  await createSchemaMigrations(db, config);
1791
1810
  await db.close();
1792
1811
  };
@@ -2000,7 +2019,12 @@ ORDER BY "name"`
2000
2019
  `SELECT
2001
2020
  nspname AS "schemaName",
2002
2021
  relname AS "name",
2003
- obj_description(c.oid) AS comment
2022
+ obj_description(c.oid) AS comment,
2023
+ (SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
2024
+ schema: "n",
2025
+ table: "c",
2026
+ where: "a.attrelid = c.oid"
2027
+ })}) t) AS "columns"
2004
2028
  FROM pg_class c
2005
2029
  JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
2006
2030
  WHERE relkind = 'r'
@@ -2062,17 +2086,6 @@ WHERE ${filterSchema("n.nspname")}`
2062
2086
  );
2063
2087
  return rows;
2064
2088
  }
2065
- async getColumns() {
2066
- const { rows } = await this.db.query(
2067
- columnsSql({
2068
- schema: "nc",
2069
- table: "c",
2070
- join: `JOIN pg_class c ON a.attrelid = c.oid AND c.relkind = 'r' JOIN pg_namespace nc ON nc.oid = c.relnamespace`,
2071
- where: filterSchema("nc.nspname")
2072
- })
2073
- );
2074
- return rows;
2075
- }
2076
2089
  async getIndexes() {
2077
2090
  const { rows } = await this.db.query(
2078
2091
  `SELECT
@@ -2417,7 +2430,7 @@ const structureToAst = async (ctx, db) => {
2417
2430
  type: "extension",
2418
2431
  action: "create",
2419
2432
  name: it.name,
2420
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2433
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2421
2434
  version: it.version
2422
2435
  });
2423
2436
  }
@@ -2426,7 +2439,7 @@ const structureToAst = async (ctx, db) => {
2426
2439
  type: "enum",
2427
2440
  action: "create",
2428
2441
  name: it.name,
2429
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2442
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2430
2443
  values: it.values
2431
2444
  });
2432
2445
  }
@@ -2434,7 +2447,7 @@ const structureToAst = async (ctx, db) => {
2434
2447
  ast.push({
2435
2448
  type: "domain",
2436
2449
  action: "create",
2437
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2450
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2438
2451
  name: it.name,
2439
2452
  baseType: domains[`${it.schemaName}.${it.name}`],
2440
2453
  notNull: it.notNull,
@@ -2467,10 +2480,10 @@ const structureToAst = async (ctx, db) => {
2467
2480
  );
2468
2481
  }
2469
2482
  for (const [fkey, table] of outerConstraints) {
2470
- ast.push(__spreadProps(__spreadValues({}, constraintToAst(fkey)), {
2483
+ ast.push(__spreadProps(__spreadValues({}, constraintToAst(ctx, fkey)), {
2471
2484
  type: "constraint",
2472
2485
  action: "create",
2473
- tableSchema: table.schemaName === "public" ? void 0 : table.schemaName,
2486
+ tableSchema: table.schemaName === ctx.currentSchema ? void 0 : table.schemaName,
2474
2487
  tableName: fkey.tableName
2475
2488
  }));
2476
2489
  }
@@ -2484,7 +2497,6 @@ const getData = async (db) => {
2484
2497
  schemas,
2485
2498
  tables,
2486
2499
  views,
2487
- columns,
2488
2500
  constraints,
2489
2501
  indexes,
2490
2502
  extensions,
@@ -2494,7 +2506,6 @@ const getData = async (db) => {
2494
2506
  db.getSchemas(),
2495
2507
  db.getTables(),
2496
2508
  db.getViews(),
2497
- db.getColumns(),
2498
2509
  db.getConstraints(),
2499
2510
  db.getIndexes(),
2500
2511
  db.getExtensions(),
@@ -2505,7 +2516,6 @@ const getData = async (db) => {
2505
2516
  schemas,
2506
2517
  tables,
2507
2518
  views,
2508
- columns,
2509
2519
  constraints,
2510
2520
  indexes,
2511
2521
  extensions,
@@ -2573,13 +2583,12 @@ const getColumnType = (type, isSerial) => {
2573
2583
  return type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial";
2574
2584
  };
2575
2585
  const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstraints = data.constraints) => {
2576
- const { schemaName, name: tableName } = table;
2586
+ const { schemaName, name: tableName, columns } = table;
2577
2587
  const key = `${schemaName}.${table.name}`;
2578
2588
  delete pendingTables[key];
2579
2589
  if (tableName === "schemaMigrations")
2580
2590
  return;
2581
2591
  const belongsToTable = makeBelongsToTable(schemaName, tableName);
2582
- const columns = data.columns.filter(belongsToTable);
2583
2592
  let primaryKey;
2584
2593
  for (const item of data.constraints) {
2585
2594
  if (belongsToTable(item) && item.primaryKey)
@@ -2594,7 +2603,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2594
2603
  const constraint = {
2595
2604
  references: references ? {
2596
2605
  columns: references.columns,
2597
- fnOrTable: getReferencesTable(references),
2606
+ fnOrTable: getReferencesTable(ctx, references),
2598
2607
  foreignColumns: references.foreignColumns,
2599
2608
  options: {
2600
2609
  match: matchMap[references.match],
@@ -2640,7 +2649,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2640
2649
  ast.push({
2641
2650
  type: "table",
2642
2651
  action: "create",
2643
- schema: schemaName === "public" ? void 0 : schemaName,
2652
+ schema: schemaName === ctx.currentSchema ? void 0 : schemaName,
2644
2653
  comment: table.comment,
2645
2654
  name: tableName,
2646
2655
  shape,
@@ -2679,7 +2688,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2679
2688
  }
2680
2689
  }
2681
2690
  };
2682
- const constraintToAst = (item) => {
2691
+ const constraintToAst = (ctx, item) => {
2683
2692
  var _a;
2684
2693
  const result = {};
2685
2694
  const { references, check } = item;
@@ -2687,7 +2696,7 @@ const constraintToAst = (item) => {
2687
2696
  const options = {};
2688
2697
  result.references = {
2689
2698
  columns: references.columns,
2690
- fnOrTable: getReferencesTable(references),
2699
+ fnOrTable: getReferencesTable(ctx, references),
2691
2700
  foreignColumns: references.foreignColumns,
2692
2701
  options
2693
2702
  };
@@ -2712,8 +2721,8 @@ const constraintToAst = (item) => {
2712
2721
  }
2713
2722
  return result;
2714
2723
  };
2715
- const getReferencesTable = (references) => {
2716
- return references.foreignSchema !== "public" ? `${references.foreignSchema}.${references.foreignTable}` : references.foreignTable;
2724
+ const getReferencesTable = (ctx, references) => {
2725
+ return references.foreignSchema !== ctx.currentSchema ? `${references.foreignSchema}.${references.foreignTable}` : references.foreignTable;
2717
2726
  };
2718
2727
  const isColumnCheck = (it) => {
2719
2728
  var _a, _b;
@@ -2739,7 +2748,7 @@ const viewToAst = (ctx, data, domains, view) => {
2739
2748
  return {
2740
2749
  type: "view",
2741
2750
  action: "create",
2742
- schema: view.schemaName === "public" ? void 0 : view.schemaName,
2751
+ schema: view.schemaName === ctx.currentSchema ? void 0 : view.schemaName,
2743
2752
  name: view.name,
2744
2753
  shape,
2745
2754
  sql: orchidCore.raw(view.sql),
@@ -3015,10 +3024,12 @@ const createView = (ast) => {
3015
3024
  const pullDbStructure = async (options, config) => {
3016
3025
  var _a, _b, _c;
3017
3026
  const adapter = new pqb.Adapter(options);
3027
+ const currentSchema = adapter.schema || "public";
3018
3028
  const db = new DbStructure(adapter);
3019
3029
  const ctx = {
3020
3030
  unsupportedTypes: {},
3021
- snakeCase: config.snakeCase
3031
+ snakeCase: config.snakeCase,
3032
+ currentSchema
3022
3033
  };
3023
3034
  const ast = await structureToAst(ctx, db);
3024
3035
  await adapter.close();