rake-db 2.5.0 → 2.6.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.d.ts CHANGED
@@ -287,7 +287,7 @@ declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
287
287
  declare const resetDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) => Promise<void>;
288
288
 
289
289
  declare const writeMigrationFile: (config: RakeDbConfig, version: string, name: string, content: string) => Promise<void>;
290
- declare const generate: (config: RakeDbConfig, args: string[]) => Promise<void>;
290
+ declare const generate: (config: RakeDbConfig, [name]: string[]) => Promise<void>;
291
291
  declare const makeFileTimeStamp: () => string;
292
292
 
293
293
  type ChangeCallback = (db: Migration, up: boolean) => Promise<void>;
package/dist/index.js CHANGED
@@ -1853,17 +1853,11 @@ const writeMigrationFile = async (config, version, name, content) => {
1853
1853
  await promises.writeFile(filePath, content);
1854
1854
  (_a = config.logger) == null ? void 0 : _a.log(`Created ${orchidCore.pathToLog(filePath)}`);
1855
1855
  };
1856
- const generate = async (config, args) => {
1857
- const name = args[0];
1856
+ const generate = async (config, [name]) => {
1858
1857
  if (!name)
1859
1858
  throw new Error("Migration name is missing");
1860
1859
  const version = makeFileTimeStamp();
1861
- await writeMigrationFile(
1862
- config,
1863
- version,
1864
- name,
1865
- makeContent(name, args.slice(1))
1866
- );
1860
+ await writeMigrationFile(config, version, name, makeContent(name));
1867
1861
  };
1868
1862
  const makeFileTimeStamp = () => {
1869
1863
  const now = /* @__PURE__ */ new Date();
@@ -1876,7 +1870,7 @@ const makeFileTimeStamp = () => {
1876
1870
  now.getUTCSeconds()
1877
1871
  ].map((value) => value < 10 ? `0${value}` : value).join("");
1878
1872
  };
1879
- const makeContent = (name, args) => {
1873
+ const makeContent = (name) => {
1880
1874
  let content = `import { change } from 'rake-db';
1881
1875
 
1882
1876
  change(async (db) => {`;
@@ -1884,42 +1878,27 @@ change(async (db) => {`;
1884
1878
  if (rest) {
1885
1879
  if (first === "create" || first === "drop") {
1886
1880
  content += `
1887
- await db.${first === "create" ? "createTable" : "dropTable"}('${rest}', (t) => ({`;
1888
- content += makeColumnsContent(args);
1889
- content += "\n }));";
1881
+ await db.${first === "create" ? "createTable" : "dropTable"}('${rest}', (t) => ({
1882
+
1883
+ }));`;
1890
1884
  } else if (first === "change") {
1891
1885
  content += `
1892
- await db.changeTable('${rest}', (t) => ({`;
1893
- content += "\n }));";
1886
+ await db.changeTable('${rest}', (t) => ({
1887
+
1888
+ }));`;
1894
1889
  } else if (first === "add" || first === "remove") {
1895
1890
  const table = first === "add" ? getTextAfterTo(rest) : getTextAfterFrom(rest);
1896
1891
  content += `
1897
- await db.changeTable(${table ? `'${table}'` : "tableName"}, (t) => ({`;
1898
- content += makeColumnsContent(args, first);
1899
- content += "\n }));";
1892
+ await db.changeTable(${table ? `'${table}'` : "tableName"}, (t) => ({
1893
+
1894
+ }));`;
1900
1895
  }
1901
1896
  }
1902
1897
  return content + "\n});\n";
1903
1898
  };
1904
- const makeColumnsContent = (args, method) => {
1905
- let content = "";
1906
- const prepend = method ? `t.${method}(` : "";
1907
- const append = method ? ")" : "";
1908
- for (const arg of args) {
1909
- const [name, def] = arg.split(":");
1910
- if (!def) {
1911
- throw new Error(
1912
- `Column argument should be similar to name:type, name:type.method1.method2, name:type(arg).method(arg). Example: name:varchar(20).nullable. Received: ${arg}`
1913
- );
1914
- }
1915
- const methods = def.split(".").map((method2) => method2.endsWith(")") ? `.${method2}` : `.${method2}()`);
1916
- content += `
1917
- ${name}: ${prepend}t${methods.join("")}${append},`;
1918
- }
1919
- return content;
1920
- };
1921
1899
 
1922
1900
  const filterSchema = (table) => `${table} !~ '^pg_' AND ${table} != 'information_schema'`;
1901
+ const jsonAgg = (sql2, as) => `(SELECT coalesce(json_agg(t.*), '[]') FROM (${sql2}) t) AS "${as}"`;
1923
1902
  const columnsSql = ({
1924
1903
  schema,
1925
1904
  table,
@@ -2001,50 +1980,33 @@ WHERE a.attnum > 0
2001
1980
  AND NOT a.attisdropped
2002
1981
  AND ${where}
2003
1982
  ORDER BY a.attnum`;
2004
- class DbStructure {
2005
- constructor(db) {
2006
- this.db = db;
2007
- }
2008
- async getSchemas() {
2009
- const { rows } = await this.db.arrays(
2010
- `SELECT n.nspname "name"
1983
+ const schemasSql = `SELECT coalesce(json_agg(nspname ORDER BY nspname), '[]')
2011
1984
  FROM pg_catalog.pg_namespace n
2012
- WHERE ${filterSchema("n.nspname")}
2013
- ORDER BY "name"`
2014
- );
2015
- return rows.flat();
2016
- }
2017
- async getTables() {
2018
- const { rows } = await this.db.query(
2019
- `SELECT
1985
+ WHERE ${filterSchema("nspname")}`;
1986
+ const tablesSql = `SELECT
2020
1987
  nspname AS "schemaName",
2021
1988
  relname AS "name",
2022
1989
  obj_description(c.oid) AS comment,
2023
1990
  (SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
2024
- schema: "n",
2025
- table: "c",
2026
- where: "a.attrelid = c.oid"
2027
- })}) t) AS "columns"
1991
+ schema: "n",
1992
+ table: "c",
1993
+ where: "a.attrelid = c.oid"
1994
+ })}) t) AS "columns"
2028
1995
  FROM pg_class c
2029
1996
  JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
2030
1997
  WHERE relkind = 'r'
2031
1998
  AND ${filterSchema("nspname")}
2032
- ORDER BY relname`
2033
- );
2034
- return rows;
2035
- }
2036
- async getViews() {
2037
- const { rows } = await this.db.query(
2038
- `SELECT
1999
+ ORDER BY relname`;
2000
+ const viewsSql = `SELECT
2039
2001
  nc.nspname AS "schemaName",
2040
2002
  c.relname AS "name",
2041
2003
  right(substring(r.ev_action from ':hasRecursive w'), 1)::bool AS "isRecursive",
2042
2004
  array_to_json(c.reloptions) AS "with",
2043
2005
  (SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
2044
- schema: "nc",
2045
- table: "c",
2046
- where: "a.attrelid = c.oid"
2047
- })}) t) AS "columns",
2006
+ schema: "nc",
2007
+ table: "c",
2008
+ where: "a.attrelid = c.oid"
2009
+ })}) t) AS "columns",
2048
2010
  pg_get_viewdef(c.oid) AS "sql"
2049
2011
  FROM pg_namespace nc
2050
2012
  JOIN pg_class c
@@ -2053,38 +2015,20 @@ JOIN pg_class c
2053
2015
  AND c.relpersistence != 't'
2054
2016
  JOIN pg_rewrite r ON r.ev_class = c.oid
2055
2017
  WHERE ${filterSchema("nc.nspname")}
2056
- ORDER BY c.relname`
2057
- );
2058
- return rows;
2018
+ ORDER BY c.relname`;
2019
+ const sql = `SELECT (${schemasSql}) AS "schemas", ${jsonAgg(
2020
+ tablesSql,
2021
+ "tables"
2022
+ )}, ${jsonAgg(viewsSql, "views")}`;
2023
+ class DbStructure {
2024
+ constructor(db) {
2025
+ this.db = db;
2059
2026
  }
2060
- async getProcedures() {
2061
- const { rows } = await this.db.query(
2062
- `SELECT
2063
- n.nspname AS "schemaName",
2064
- proname AS name,
2065
- proretset AS "returnSet",
2066
- (
2067
- SELECT typname FROM pg_type WHERE oid = prorettype
2068
- ) AS "returnType",
2069
- prokind AS "kind",
2070
- coalesce((
2071
- SELECT true FROM information_schema.triggers
2072
- WHERE n.nspname = trigger_schema AND trigger_name = proname
2073
- LIMIT 1
2074
- ), false) AS "isTrigger",
2075
- coalesce((
2076
- SELECT json_agg(pg_type.typname)
2077
- FROM unnest(coalesce(proallargtypes, proargtypes)) typeId
2078
- JOIN pg_type ON pg_type.oid = typeId
2079
- ), '[]') AS "types",
2080
- coalesce(to_json(proallargtypes::int[]), to_json(proargtypes::int[])) AS "argTypes",
2081
- coalesce(to_json(proargmodes), '[]') AS "argModes",
2082
- to_json(proargnames) AS "argNames"
2083
- FROM pg_proc p
2084
- JOIN pg_namespace n ON p.pronamespace = n.oid
2085
- WHERE ${filterSchema("n.nspname")}`
2086
- );
2087
- return rows;
2027
+ async getStructure() {
2028
+ const {
2029
+ rows: [structure]
2030
+ } = await this.db.query(sql);
2031
+ return structure;
2088
2032
  }
2089
2033
  async getIndexes() {
2090
2034
  const { rows } = await this.db.query(
@@ -2494,18 +2438,14 @@ const structureToAst = async (ctx, db) => {
2494
2438
  };
2495
2439
  const getData = async (db) => {
2496
2440
  const [
2497
- schemas,
2498
- tables,
2499
- views,
2441
+ { schemas, tables, views },
2500
2442
  constraints,
2501
2443
  indexes,
2502
2444
  extensions,
2503
2445
  enums,
2504
2446
  domains
2505
2447
  ] = await Promise.all([
2506
- db.getSchemas(),
2507
- db.getTables(),
2508
- db.getViews(),
2448
+ db.getStructure(),
2509
2449
  db.getConstraints(),
2510
2450
  db.getIndexes(),
2511
2451
  db.getExtensions(),
@@ -3106,6 +3046,9 @@ const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2))
3106
3046
  };
3107
3047
  const help = `Usage: rake-db [command] [arguments]
3108
3048
 
3049
+ See documentation at:
3050
+ https://orchid-orm.netlify.app/guide/migration-commands.html
3051
+
3109
3052
  Commands:
3110
3053
  create create databases
3111
3054
  drop drop databases
@@ -3128,17 +3071,6 @@ Rollback arguments:
3128
3071
  Migrate and rollback common arguments:
3129
3072
  --code run code updater, overrides \`useCodeUpdater\` option
3130
3073
  --code false do not run code updater
3131
-
3132
- New migration file arguments:
3133
- - (required) first argument is migration name
3134
- * create* template for create table
3135
- * change* template for change table
3136
- * add*To* template for add columns
3137
- * remove*From* template for remove columns
3138
- * drop* template for drop table
3139
-
3140
- - other arguments considered as columns with types and optional methods:
3141
- rake-db new createTable id:serial.primaryKey name:text.nullable
3142
3074
  `;
3143
3075
 
3144
3076
  exports.MigrationBase = MigrationBase;