rake-db 2.4.43 → 2.4.44

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.mjs CHANGED
@@ -1728,7 +1728,7 @@ const execute = async (options, sql) => {
1728
1728
  }
1729
1729
  };
1730
1730
  const createOrDrop = async (options, adminOptions, config, args) => {
1731
- var _a, _b, _c, _d;
1731
+ var _a, _b, _c, _d, _e, _f;
1732
1732
  const params = getDatabaseAndUserFromOptions(options);
1733
1733
  const result = await execute(
1734
1734
  setAdapterOptions(adminOptions, { database: "postgres" }),
@@ -1766,6 +1766,21 @@ Don't use this command for database service providers, only for a local db.`;
1766
1766
  if (!args.create)
1767
1767
  return;
1768
1768
  const db = new Adapter(options);
1769
+ const { schema } = db;
1770
+ if (schema) {
1771
+ db.schema = void 0;
1772
+ try {
1773
+ await db.query(`CREATE SCHEMA "${schema}"`);
1774
+ (_e = config.logger) == null ? void 0 : _e.log(`Created schema ${schema}`);
1775
+ } catch (err) {
1776
+ if (err.code === "42P06") {
1777
+ (_f = config.logger) == null ? void 0 : _f.log(`Schema ${schema} already exists`);
1778
+ } else {
1779
+ throw err;
1780
+ }
1781
+ }
1782
+ db.schema = schema;
1783
+ }
1769
1784
  await createSchemaMigrations(db, config);
1770
1785
  await db.close();
1771
1786
  };
@@ -1979,7 +1994,12 @@ ORDER BY "name"`
1979
1994
  `SELECT
1980
1995
  nspname AS "schemaName",
1981
1996
  relname AS "name",
1982
- obj_description(c.oid) AS comment
1997
+ obj_description(c.oid) AS comment,
1998
+ (SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
1999
+ schema: "n",
2000
+ table: "c",
2001
+ where: "a.attrelid = c.oid"
2002
+ })}) t) AS "columns"
1983
2003
  FROM pg_class c
1984
2004
  JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
1985
2005
  WHERE relkind = 'r'
@@ -2041,17 +2061,6 @@ WHERE ${filterSchema("n.nspname")}`
2041
2061
  );
2042
2062
  return rows;
2043
2063
  }
2044
- async getColumns() {
2045
- const { rows } = await this.db.query(
2046
- columnsSql({
2047
- schema: "nc",
2048
- table: "c",
2049
- join: `JOIN pg_class c ON a.attrelid = c.oid AND c.relkind = 'r' JOIN pg_namespace nc ON nc.oid = c.relnamespace`,
2050
- where: filterSchema("nc.nspname")
2051
- })
2052
- );
2053
- return rows;
2054
- }
2055
2064
  async getIndexes() {
2056
2065
  const { rows } = await this.db.query(
2057
2066
  `SELECT
@@ -2396,7 +2405,7 @@ const structureToAst = async (ctx, db) => {
2396
2405
  type: "extension",
2397
2406
  action: "create",
2398
2407
  name: it.name,
2399
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2408
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2400
2409
  version: it.version
2401
2410
  });
2402
2411
  }
@@ -2405,7 +2414,7 @@ const structureToAst = async (ctx, db) => {
2405
2414
  type: "enum",
2406
2415
  action: "create",
2407
2416
  name: it.name,
2408
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2417
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2409
2418
  values: it.values
2410
2419
  });
2411
2420
  }
@@ -2413,7 +2422,7 @@ const structureToAst = async (ctx, db) => {
2413
2422
  ast.push({
2414
2423
  type: "domain",
2415
2424
  action: "create",
2416
- schema: it.schemaName === "public" ? void 0 : it.schemaName,
2425
+ schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName,
2417
2426
  name: it.name,
2418
2427
  baseType: domains[`${it.schemaName}.${it.name}`],
2419
2428
  notNull: it.notNull,
@@ -2446,10 +2455,10 @@ const structureToAst = async (ctx, db) => {
2446
2455
  );
2447
2456
  }
2448
2457
  for (const [fkey, table] of outerConstraints) {
2449
- ast.push(__spreadProps(__spreadValues({}, constraintToAst(fkey)), {
2458
+ ast.push(__spreadProps(__spreadValues({}, constraintToAst(ctx, fkey)), {
2450
2459
  type: "constraint",
2451
2460
  action: "create",
2452
- tableSchema: table.schemaName === "public" ? void 0 : table.schemaName,
2461
+ tableSchema: table.schemaName === ctx.currentSchema ? void 0 : table.schemaName,
2453
2462
  tableName: fkey.tableName
2454
2463
  }));
2455
2464
  }
@@ -2463,7 +2472,6 @@ const getData = async (db) => {
2463
2472
  schemas,
2464
2473
  tables,
2465
2474
  views,
2466
- columns,
2467
2475
  constraints,
2468
2476
  indexes,
2469
2477
  extensions,
@@ -2473,7 +2481,6 @@ const getData = async (db) => {
2473
2481
  db.getSchemas(),
2474
2482
  db.getTables(),
2475
2483
  db.getViews(),
2476
- db.getColumns(),
2477
2484
  db.getConstraints(),
2478
2485
  db.getIndexes(),
2479
2486
  db.getExtensions(),
@@ -2484,7 +2491,6 @@ const getData = async (db) => {
2484
2491
  schemas,
2485
2492
  tables,
2486
2493
  views,
2487
- columns,
2488
2494
  constraints,
2489
2495
  indexes,
2490
2496
  extensions,
@@ -2552,13 +2558,12 @@ const getColumnType = (type, isSerial) => {
2552
2558
  return type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial";
2553
2559
  };
2554
2560
  const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstraints = data.constraints) => {
2555
- const { schemaName, name: tableName } = table;
2561
+ const { schemaName, name: tableName, columns } = table;
2556
2562
  const key = `${schemaName}.${table.name}`;
2557
2563
  delete pendingTables[key];
2558
2564
  if (tableName === "schemaMigrations")
2559
2565
  return;
2560
2566
  const belongsToTable = makeBelongsToTable(schemaName, tableName);
2561
- const columns = data.columns.filter(belongsToTable);
2562
2567
  let primaryKey;
2563
2568
  for (const item of data.constraints) {
2564
2569
  if (belongsToTable(item) && item.primaryKey)
@@ -2573,7 +2578,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2573
2578
  const constraint = {
2574
2579
  references: references ? {
2575
2580
  columns: references.columns,
2576
- fnOrTable: getReferencesTable(references),
2581
+ fnOrTable: getReferencesTable(ctx, references),
2577
2582
  foreignColumns: references.foreignColumns,
2578
2583
  options: {
2579
2584
  match: matchMap[references.match],
@@ -2619,7 +2624,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2619
2624
  ast.push({
2620
2625
  type: "table",
2621
2626
  action: "create",
2622
- schema: schemaName === "public" ? void 0 : schemaName,
2627
+ schema: schemaName === ctx.currentSchema ? void 0 : schemaName,
2623
2628
  comment: table.comment,
2624
2629
  name: tableName,
2625
2630
  shape,
@@ -2658,7 +2663,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2658
2663
  }
2659
2664
  }
2660
2665
  };
2661
- const constraintToAst = (item) => {
2666
+ const constraintToAst = (ctx, item) => {
2662
2667
  var _a;
2663
2668
  const result = {};
2664
2669
  const { references, check } = item;
@@ -2666,7 +2671,7 @@ const constraintToAst = (item) => {
2666
2671
  const options = {};
2667
2672
  result.references = {
2668
2673
  columns: references.columns,
2669
- fnOrTable: getReferencesTable(references),
2674
+ fnOrTable: getReferencesTable(ctx, references),
2670
2675
  foreignColumns: references.foreignColumns,
2671
2676
  options
2672
2677
  };
@@ -2691,8 +2696,8 @@ const constraintToAst = (item) => {
2691
2696
  }
2692
2697
  return result;
2693
2698
  };
2694
- const getReferencesTable = (references) => {
2695
- return references.foreignSchema !== "public" ? `${references.foreignSchema}.${references.foreignTable}` : references.foreignTable;
2699
+ const getReferencesTable = (ctx, references) => {
2700
+ return references.foreignSchema !== ctx.currentSchema ? `${references.foreignSchema}.${references.foreignTable}` : references.foreignTable;
2696
2701
  };
2697
2702
  const isColumnCheck = (it) => {
2698
2703
  var _a, _b;
@@ -2718,7 +2723,7 @@ const viewToAst = (ctx, data, domains, view) => {
2718
2723
  return {
2719
2724
  type: "view",
2720
2725
  action: "create",
2721
- schema: view.schemaName === "public" ? void 0 : view.schemaName,
2726
+ schema: view.schemaName === ctx.currentSchema ? void 0 : view.schemaName,
2722
2727
  name: view.name,
2723
2728
  shape,
2724
2729
  sql: raw(view.sql),
@@ -2994,10 +2999,12 @@ const createView = (ast) => {
2994
2999
  const pullDbStructure = async (options, config) => {
2995
3000
  var _a, _b, _c;
2996
3001
  const adapter = new Adapter(options);
3002
+ const currentSchema = adapter.schema || "public";
2997
3003
  const db = new DbStructure(adapter);
2998
3004
  const ctx = {
2999
3005
  unsupportedTypes: {},
3000
- snakeCase: config.snakeCase
3006
+ snakeCase: config.snakeCase,
3007
+ currentSchema
3001
3008
  };
3002
3009
  const ast = await structureToAst(ctx, db);
3003
3010
  await adapter.close();