rake-db 2.4.37 → 2.4.38

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
@@ -175,6 +175,7 @@ declare namespace RakeDbAst {
175
175
  columns: string[];
176
176
  } & ForeignKeyOptions)[];
177
177
  indexes?: Omit<SingleColumnIndexOptions, 'column' | 'expression'>[];
178
+ identity?: TableData.Identity;
178
179
  };
179
180
  type RenameTable = {
180
181
  type: 'renameTable';
package/dist/index.js CHANGED
@@ -316,6 +316,9 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
316
316
  if (item.data.collate) {
317
317
  line.push(`COLLATE ${pqb.quote(item.data.collate)}`);
318
318
  }
319
+ if (item.data.identity) {
320
+ line.push(identityToSql(item.data.identity));
321
+ }
319
322
  if (item.data.isPrimaryKey && !hasMultiplePrimaryKeys) {
320
323
  line.push("PRIMARY KEY");
321
324
  } else if (!item.data.isNullable) {
@@ -352,6 +355,32 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
352
355
  }
353
356
  return line.join(" ");
354
357
  };
358
+ const identityToSql = (identity) => {
359
+ const options = sequenceOptionsToSql(identity);
360
+ return `GENERATED ${identity.always ? "ALWAYS" : "BY DEFAULT"} AS IDENTITY${options ? ` (${options})` : ""}`;
361
+ };
362
+ const sequenceOptionsToSql = (item) => {
363
+ const line = [];
364
+ if (item.dataType)
365
+ line.push(`AS ${item.dataType}`);
366
+ if (item.incrementBy !== void 0)
367
+ line.push(`INCREMENT BY ${item.incrementBy}`);
368
+ if (item.min !== void 0)
369
+ line.push(`MINVALUE ${item.min}`);
370
+ if (item.max !== void 0)
371
+ line.push(`MAXVALUE ${item.max}`);
372
+ if (item.startWith !== void 0)
373
+ line.push(`START WITH ${item.startWith}`);
374
+ if (item.cache !== void 0)
375
+ line.push(`CACHE ${item.cache}`);
376
+ if (item.cycle)
377
+ line.push(`CYCLE`);
378
+ if (item.ownedBy) {
379
+ const [schema, table] = getSchemaAndTableFromName(item.ownedBy);
380
+ line.push(`OWNED BY ${quoteWithSchema({ schema, name: table })}`);
381
+ }
382
+ return line.join(" ");
383
+ };
355
384
  const addColumnIndex = (indexes, name, item) => {
356
385
  if (item.data.indexes) {
357
386
  indexes.push(
@@ -1015,6 +1044,11 @@ const astToQueries = (ast, snakeCase) => {
1015
1044
  `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${pqb.quote(to.collate)}` : ""}${item.using ? ` USING ${pqb.getRaw(item.using, values)}` : ""}`
1016
1045
  );
1017
1046
  }
1047
+ if (typeof from.identity !== typeof to.identity || !orchidCore.deepCompare(from.identity, to.identity)) {
1048
+ alterTable.push(
1049
+ `ALTER COLUMN "${name}" ${to.identity ? `ADD ${identityToSql(to.identity)}` : `DROP IDENTITY`}`
1050
+ );
1051
+ }
1018
1052
  if (from.default !== to.default) {
1019
1053
  const value = typeof to.default === "object" && to.default && orchidCore.isRaw(to.default) ? pqb.getRaw(to.default, values) : pqb.quote(to.default);
1020
1054
  const expr = value === void 0 ? "DROP DEFAULT" : `SET DEFAULT ${value}`;
@@ -1863,41 +1897,83 @@ WHERE ${filterSchema("n.nspname")}`
1863
1897
  async getColumns() {
1864
1898
  const { rows } = await this.db.query(
1865
1899
  `SELECT
1866
- table_schema "schemaName",
1867
- table_name "tableName",
1868
- column_name "name",
1869
- typname "type",
1870
- n.nspname "typeSchema",
1871
- data_type = 'ARRAY' "isArray",
1872
- character_maximum_length AS "maxChars",
1873
- numeric_precision AS "numericPrecision",
1874
- numeric_scale AS "numericScale",
1875
- datetime_precision AS "dateTimePrecision",
1876
- column_default "default",
1877
- is_nullable::boolean "isNullable",
1878
- collation_name AS "collation",
1900
+ nc.nspname AS "schemaName",
1901
+ c.relname AS "tableName",
1902
+ a.attname AS "name",
1903
+ COALESCE(et.typname, t.typname) AS "type",
1904
+ tn.nspname AS "typeSchema",
1905
+ et.typname IS NOT NULL AS "isArray",
1906
+ information_schema._pg_char_max_length(
1907
+ information_schema._pg_truetypid(a, t),
1908
+ information_schema._pg_truetypmod(a, t)
1909
+ ) AS "maxChars",
1910
+ information_schema._pg_numeric_precision(
1911
+ information_schema._pg_truetypid(a, t),
1912
+ information_schema._pg_truetypmod(a, t)
1913
+ ) AS "numericPrecision",
1914
+ information_schema._pg_numeric_scale(
1915
+ information_schema._pg_truetypid(a, t),
1916
+ information_schema._pg_truetypmod(a, t)
1917
+ ) AS "numericScale",
1918
+ information_schema._pg_datetime_precision(
1919
+ information_schema._pg_truetypid(a, t),
1920
+ information_schema._pg_truetypmod(a, t)
1921
+ ) AS "datetimePrecision",
1922
+ CAST(
1923
+ CASE WHEN a.attgenerated = ''
1924
+ THEN pg_get_expr(ad.adbin, ad.adrelid)
1925
+ END AS information_schema.character_data
1926
+ ) AS "default",
1927
+ NOT (a.attnotnull OR (t.typtype = 'd' AND t.typnotnull)) AS "isNullable",
1928
+ co.collname AS "collation",
1879
1929
  NULLIF(a.attcompression, '') AS compression,
1880
- pgd.description AS "comment"
1881
- FROM information_schema.columns c
1882
- JOIN pg_catalog.pg_statio_all_tables st
1883
- ON c.table_schema = st.schemaname
1884
- AND c.table_name = st.relname
1930
+ pgd.description AS "comment",
1931
+ (
1932
+ CASE WHEN a.attidentity IN ('a', 'd') THEN (
1933
+ json_build_object(
1934
+ 'always',
1935
+ a.attidentity = 'a',
1936
+ 'start',
1937
+ seq.seqstart,
1938
+ 'increment',
1939
+ seq.seqincrement,
1940
+ 'min',
1941
+ nullif(seq.seqmin, 1),
1942
+ 'max',
1943
+ nullif(seq.seqmax, (
1944
+ CASE COALESCE(et.typname, t.typname)
1945
+ WHEN 'int2' THEN 32767
1946
+ WHEN 'int4' THEN 2147483647
1947
+ WHEN 'int8' THEN 9223372036854775807
1948
+ ELSE NULL
1949
+ END
1950
+ )),
1951
+ 'cache',
1952
+ seq.seqcache,
1953
+ 'cycle',
1954
+ seq.seqcycle
1955
+ )
1956
+ ) END
1957
+ ) "identity"
1958
+ FROM pg_attribute a
1959
+ LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
1960
+ JOIN pg_class c ON a.attrelid = c.oid
1961
+ JOIN pg_namespace nc ON c.relnamespace = nc.oid
1962
+ JOIN pg_type t ON a.atttypid = t.oid
1963
+ LEFT JOIN pg_type et ON t.typelem = et.oid
1964
+ JOIN pg_namespace tn ON tn.oid = t.typnamespace
1965
+ LEFT JOIN (pg_collation co JOIN pg_namespace nco ON (co.collnamespace = nco.oid))
1966
+ ON a.attcollation = co.oid AND (nco.nspname, co.collname) <> ('pg_catalog', 'default')
1885
1967
  LEFT JOIN pg_catalog.pg_description pgd
1886
- ON pgd.objoid = st.relid
1887
- AND pgd.objsubid = c.ordinal_position
1888
- JOIN pg_catalog.pg_attribute a
1889
- ON a.attrelid = st.relid
1890
- AND a.attnum = c.ordinal_position
1891
- JOIN pg_catalog.pg_type t
1892
- ON (
1893
- CASE WHEN data_type = 'ARRAY'
1894
- THEN typarray
1895
- ELSE oid
1896
- END
1897
- ) = atttypid
1898
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
1899
- WHERE ${filterSchema("table_schema")}
1900
- ORDER BY c.ordinal_position`
1968
+ ON pgd.objoid = a.attrelid
1969
+ AND pgd.objsubid = a.attnum
1970
+ LEFT JOIN (pg_depend dep JOIN pg_sequence seq ON (dep.classid = 'pg_class'::regclass AND dep.objid = seq.seqrelid AND dep.deptype = 'i'))
1971
+ ON (dep.refclassid = 'pg_class'::regclass AND dep.refobjid = c.oid AND dep.refobjsubid = a.attnum)
1972
+ WHERE a.attnum > 0
1973
+ AND NOT a.attisdropped
1974
+ AND c.relkind IN ('r', 'v', 'f', 'p')
1975
+ AND ${filterSchema("nc.nspname")}
1976
+ ORDER BY a.attnum`
1901
1977
  );
1902
1978
  return rows;
1903
1979
  }
@@ -2394,7 +2470,7 @@ const getColumnType = (type, isSerial) => {
2394
2470
  return type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial";
2395
2471
  };
2396
2472
  const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstraints = data.constraints) => {
2397
- var _a;
2473
+ var _a, _b;
2398
2474
  const { schemaName, name: tableName } = table;
2399
2475
  const key = `${schemaName}.${table.name}`;
2400
2476
  delete pendingTables[key];
@@ -2459,7 +2535,12 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
2459
2535
  isArray: item.isArray,
2460
2536
  isSerial
2461
2537
  }));
2462
- if (((_a = primaryKey == null ? void 0 : primaryKey.columns) == null ? void 0 : _a.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columns[0]) === item.name) {
2538
+ if (item.identity) {
2539
+ column.data.identity = item.identity;
2540
+ if (!item.identity.always)
2541
+ (_a = column.data.identity) == null ? true : delete _a.always;
2542
+ }
2543
+ if (((_b = primaryKey == null ? void 0 : primaryKey.columns) == null ? void 0 : _b.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columns[0]) === item.name) {
2463
2544
  column = column.primaryKey();
2464
2545
  }
2465
2546
  const indexes = tableIndexes.filter(
@@ -2775,7 +2856,11 @@ const pullDbStructure = async (options, config) => {
2775
2856
  return;
2776
2857
  const version = makeFileTimeStamp();
2777
2858
  await writeMigrationFile(config, version, "pull", result);
2778
- await saveMigratedVersion(adapter, version, config);
2859
+ const silentQueries = Object.assign(adapter, {
2860
+ silentQuery: adapter.query,
2861
+ silentArrays: adapter.arrays
2862
+ });
2863
+ await saveMigratedVersion(silentQueries, version, config);
2779
2864
  const cache = {};
2780
2865
  for (const item of ast) {
2781
2866
  await ((_a = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _a.call(config, {