rake-db 2.4.37 → 2.4.39

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
@@ -1,5 +1,5 @@
1
1
  import { quote, getRaw, EnumColumn, columnTypes, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, TimestampColumn, referencesArgsToCode, constraintPropsToCode } from 'pqb';
2
- import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, pathToLog, raw, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey } from 'orchid-core';
2
+ import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, deepCompare, pathToLog, raw, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey } from 'orchid-core';
3
3
  import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
5
5
  import prompts from 'prompts';
@@ -288,6 +288,9 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
288
288
  if (item.data.collate) {
289
289
  line.push(`COLLATE ${quote(item.data.collate)}`);
290
290
  }
291
+ if (item.data.identity) {
292
+ line.push(identityToSql(item.data.identity));
293
+ }
291
294
  if (item.data.isPrimaryKey && !hasMultiplePrimaryKeys) {
292
295
  line.push("PRIMARY KEY");
293
296
  } else if (!item.data.isNullable) {
@@ -324,6 +327,32 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
324
327
  }
325
328
  return line.join(" ");
326
329
  };
330
+ const identityToSql = (identity) => {
331
+ const options = sequenceOptionsToSql(identity);
332
+ return `GENERATED ${identity.always ? "ALWAYS" : "BY DEFAULT"} AS IDENTITY${options ? ` (${options})` : ""}`;
333
+ };
334
+ const sequenceOptionsToSql = (item) => {
335
+ const line = [];
336
+ if (item.dataType)
337
+ line.push(`AS ${item.dataType}`);
338
+ if (item.incrementBy !== void 0)
339
+ line.push(`INCREMENT BY ${item.incrementBy}`);
340
+ if (item.min !== void 0)
341
+ line.push(`MINVALUE ${item.min}`);
342
+ if (item.max !== void 0)
343
+ line.push(`MAXVALUE ${item.max}`);
344
+ if (item.startWith !== void 0)
345
+ line.push(`START WITH ${item.startWith}`);
346
+ if (item.cache !== void 0)
347
+ line.push(`CACHE ${item.cache}`);
348
+ if (item.cycle)
349
+ line.push(`CYCLE`);
350
+ if (item.ownedBy) {
351
+ const [schema, table] = getSchemaAndTableFromName(item.ownedBy);
352
+ line.push(`OWNED BY ${quoteWithSchema({ schema, name: table })}`);
353
+ }
354
+ return line.join(" ");
355
+ };
327
356
  const addColumnIndex = (indexes, name, item) => {
328
357
  if (item.data.indexes) {
329
358
  indexes.push(
@@ -987,6 +1016,11 @@ const astToQueries = (ast, snakeCase) => {
987
1016
  `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quote(to.collate)}` : ""}${item.using ? ` USING ${getRaw(item.using, values)}` : ""}`
988
1017
  );
989
1018
  }
1019
+ if (typeof from.identity !== typeof to.identity || !deepCompare(from.identity, to.identity)) {
1020
+ alterTable.push(
1021
+ `ALTER COLUMN "${name}" ${to.identity ? `ADD ${identityToSql(to.identity)}` : `DROP IDENTITY`}`
1022
+ );
1023
+ }
990
1024
  if (from.default !== to.default) {
991
1025
  const value = typeof to.default === "object" && to.default && isRaw(to.default) ? getRaw(to.default, values) : quote(to.default);
992
1026
  const expr = value === void 0 ? "DROP DEFAULT" : `SET DEFAULT ${value}`;
@@ -1835,41 +1869,83 @@ WHERE ${filterSchema("n.nspname")}`
1835
1869
  async getColumns() {
1836
1870
  const { rows } = await this.db.query(
1837
1871
  `SELECT
1838
- table_schema "schemaName",
1839
- table_name "tableName",
1840
- column_name "name",
1841
- typname "type",
1842
- n.nspname "typeSchema",
1843
- data_type = 'ARRAY' "isArray",
1844
- character_maximum_length AS "maxChars",
1845
- numeric_precision AS "numericPrecision",
1846
- numeric_scale AS "numericScale",
1847
- datetime_precision AS "dateTimePrecision",
1848
- column_default "default",
1849
- is_nullable::boolean "isNullable",
1850
- collation_name AS "collation",
1872
+ nc.nspname AS "schemaName",
1873
+ c.relname AS "tableName",
1874
+ a.attname AS "name",
1875
+ COALESCE(et.typname, t.typname) AS "type",
1876
+ tn.nspname AS "typeSchema",
1877
+ et.typname IS NOT NULL AS "isArray",
1878
+ information_schema._pg_char_max_length(
1879
+ information_schema._pg_truetypid(a, t),
1880
+ information_schema._pg_truetypmod(a, t)
1881
+ ) AS "maxChars",
1882
+ information_schema._pg_numeric_precision(
1883
+ information_schema._pg_truetypid(a, t),
1884
+ information_schema._pg_truetypmod(a, t)
1885
+ ) AS "numericPrecision",
1886
+ information_schema._pg_numeric_scale(
1887
+ information_schema._pg_truetypid(a, t),
1888
+ information_schema._pg_truetypmod(a, t)
1889
+ ) AS "numericScale",
1890
+ information_schema._pg_datetime_precision(
1891
+ information_schema._pg_truetypid(a, t),
1892
+ information_schema._pg_truetypmod(a, t)
1893
+ ) AS "datetimePrecision",
1894
+ CAST(
1895
+ CASE WHEN a.attgenerated = ''
1896
+ THEN pg_get_expr(ad.adbin, ad.adrelid)
1897
+ END AS information_schema.character_data
1898
+ ) AS "default",
1899
+ NOT (a.attnotnull OR (t.typtype = 'd' AND t.typnotnull)) AS "isNullable",
1900
+ co.collname AS "collation",
1851
1901
  NULLIF(a.attcompression, '') AS compression,
1852
- pgd.description AS "comment"
1853
- FROM information_schema.columns c
1854
- JOIN pg_catalog.pg_statio_all_tables st
1855
- ON c.table_schema = st.schemaname
1856
- AND c.table_name = st.relname
1902
+ pgd.description AS "comment",
1903
+ (
1904
+ CASE WHEN a.attidentity IN ('a', 'd') THEN (
1905
+ json_build_object(
1906
+ 'always',
1907
+ a.attidentity = 'a',
1908
+ 'start',
1909
+ seq.seqstart,
1910
+ 'increment',
1911
+ seq.seqincrement,
1912
+ 'min',
1913
+ nullif(seq.seqmin, 1),
1914
+ 'max',
1915
+ nullif(seq.seqmax, (
1916
+ CASE COALESCE(et.typname, t.typname)
1917
+ WHEN 'int2' THEN 32767
1918
+ WHEN 'int4' THEN 2147483647
1919
+ WHEN 'int8' THEN 9223372036854775807
1920
+ ELSE NULL
1921
+ END
1922
+ )),
1923
+ 'cache',
1924
+ seq.seqcache,
1925
+ 'cycle',
1926
+ seq.seqcycle
1927
+ )
1928
+ ) END
1929
+ ) "identity"
1930
+ FROM pg_attribute a
1931
+ LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
1932
+ JOIN pg_class c ON a.attrelid = c.oid
1933
+ JOIN pg_namespace nc ON c.relnamespace = nc.oid
1934
+ JOIN pg_type t ON a.atttypid = t.oid
1935
+ LEFT JOIN pg_type et ON t.typelem = et.oid
1936
+ JOIN pg_namespace tn ON tn.oid = t.typnamespace
1937
+ LEFT JOIN (pg_collation co JOIN pg_namespace nco ON (co.collnamespace = nco.oid))
1938
+ ON a.attcollation = co.oid AND (nco.nspname, co.collname) <> ('pg_catalog', 'default')
1857
1939
  LEFT JOIN pg_catalog.pg_description pgd
1858
- ON pgd.objoid = st.relid
1859
- AND pgd.objsubid = c.ordinal_position
1860
- JOIN pg_catalog.pg_attribute a
1861
- ON a.attrelid = st.relid
1862
- AND a.attnum = c.ordinal_position
1863
- JOIN pg_catalog.pg_type t
1864
- ON (
1865
- CASE WHEN data_type = 'ARRAY'
1866
- THEN typarray
1867
- ELSE oid
1868
- END
1869
- ) = atttypid
1870
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
1871
- WHERE ${filterSchema("table_schema")}
1872
- ORDER BY c.ordinal_position`
1940
+ ON pgd.objoid = a.attrelid
1941
+ AND pgd.objsubid = a.attnum
1942
+ LEFT JOIN (pg_depend dep JOIN pg_sequence seq ON (dep.classid = 'pg_class'::regclass AND dep.objid = seq.seqrelid AND dep.deptype = 'i'))
1943
+ ON (dep.refclassid = 'pg_class'::regclass AND dep.refobjid = c.oid AND dep.refobjsubid = a.attnum)
1944
+ WHERE a.attnum > 0
1945
+ AND NOT a.attisdropped
1946
+ AND c.relkind IN ('r', 'v', 'f', 'p')
1947
+ AND ${filterSchema("nc.nspname")}
1948
+ ORDER BY a.attnum`
1873
1949
  );
1874
1950
  return rows;
1875
1951
  }
@@ -2366,7 +2442,7 @@ const getColumnType = (type, isSerial) => {
2366
2442
  return type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial";
2367
2443
  };
2368
2444
  const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstraints = data.constraints) => {
2369
- var _a;
2445
+ var _a, _b;
2370
2446
  const { schemaName, name: tableName } = table;
2371
2447
  const key = `${schemaName}.${table.name}`;
2372
2448
  delete pendingTables[key];
@@ -2431,7 +2507,12 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2431
2507
  isArray: item.isArray,
2432
2508
  isSerial
2433
2509
  }));
2434
- if (((_a = primaryKey == null ? void 0 : primaryKey.columns) == null ? void 0 : _a.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columns[0]) === item.name) {
2510
+ if (item.identity) {
2511
+ column.data.identity = item.identity;
2512
+ if (!item.identity.always)
2513
+ (_a = column.data.identity) == null ? true : delete _a.always;
2514
+ }
2515
+ if (((_b = primaryKey == null ? void 0 : primaryKey.columns) == null ? void 0 : _b.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columns[0]) === item.name) {
2435
2516
  column = column.primaryKey();
2436
2517
  }
2437
2518
  const indexes = tableIndexes.filter(
@@ -2747,7 +2828,11 @@ const pullDbStructure = async (options, config) => {
2747
2828
  return;
2748
2829
  const version = makeFileTimeStamp();
2749
2830
  await writeMigrationFile(config, version, "pull", result);
2750
- await saveMigratedVersion(adapter, version, config);
2831
+ const silentQueries = Object.assign(adapter, {
2832
+ silentQuery: adapter.query,
2833
+ silentArrays: adapter.arrays
2834
+ });
2835
+ await saveMigratedVersion(silentQueries, version, config);
2751
2836
  const cache = {};
2752
2837
  for (const item of ast) {
2753
2838
  await ((_a = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _a.call(config, {