rake-db 2.22.7 → 2.22.9

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, EnumColumn, defaultSchemaConfig, getColumnTypes, parseTableData, tableDataMethods, ColumnType, parseTableDataInput, UnknownColumn, raw, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, makeColumnTypes, makeColumnsByType, RawSQL, DomainColumn, CustomTypeColumn, ArrayColumn, instantiateColumn, pushTableDataCode, primaryKeyInnerToCode, indexInnerToCode, constraintInnerToCode, referencesArgsToCode, TimestampTZColumn, TimestampColumn } from 'pqb';
2
- import { singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, setCurrentColumnName, consumeColumnName, ColumnTypeBase, setDefaultLanguage, deepCompare, getImportPath, pathToLog, emptyArray, getStackTrace, toCamelCase, codeToString, addCode, quoteObjectKey, columnToCode, backtickQuote } from 'orchid-core';
2
+ import { singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, setCurrentColumnName, consumeColumnName, ColumnTypeBase, setDefaultLanguage, deepCompare, getImportPath, pathToLog, emptyArray, getStackTrace, toCamelCase, codeToString, addCode, quoteObjectKey, backtickQuote } from 'orchid-core';
3
3
  import path, { join } from 'path';
4
4
  import { pathToFileURL, fileURLToPath } from 'node:url';
5
5
  import fs, { mkdir, writeFile, readdir, stat, readFile } from 'fs/promises';
@@ -1210,7 +1210,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
1210
1210
  for (let i = 0; i < indexesLen; i++) {
1211
1211
  const fromIndex = (_v = from.indexes) == null ? void 0 : _v[i];
1212
1212
  const toIndex = (_w = to.indexes) == null ? void 0 : _w[i];
1213
- if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.options.collate !== toIndex.options.collate || fromIndex.options.opclass !== toIndex.options.opclass || fromIndex.options.order !== toIndex.options.order || fromIndex.name !== toIndex.name || fromIndex.options.unique !== toIndex.options.unique || fromIndex.options.using !== toIndex.options.using || fromIndex.options.include !== toIndex.options.include || Array.isArray(fromIndex.options.include) && Array.isArray(toIndex.options.include) && fromIndex.options.include.join(",") !== toIndex.options.include.join(",") || fromIndex.options.with !== toIndex.options.with || fromIndex.options.tablespace !== toIndex.options.tablespace || fromIndex.options.where !== toIndex.options.where || fromIndex.options.dropMode !== toIndex.options.dropMode)) {
1213
+ if ((fromIndex || toIndex) && (!fromIndex || !toIndex || !deepCompare(fromIndex, toIndex))) {
1214
1214
  if (fromIndex) {
1215
1215
  dropIndexes.push(__spreadProps$5(__spreadValues$7({}, fromIndex), {
1216
1216
  columns: [
@@ -3661,25 +3661,13 @@ const columnsSql = ({
3661
3661
  ${schema}.nspname AS "schemaName",
3662
3662
  ${table}.relname AS "tableName",
3663
3663
  a.attname AS "name",
3664
- COALESCE(et.typname, t.typname) AS "type",
3664
+ t.typname AS "type",
3665
3665
  tn.nspname AS "typeSchema",
3666
- et.typname IS NOT NULL AS "isArray",
3667
- information_schema._pg_char_max_length(
3668
- information_schema._pg_truetypid(a, t),
3669
- information_schema._pg_truetypmod(a, t)
3670
- ) AS "maxChars",
3671
- information_schema._pg_numeric_precision(
3672
- information_schema._pg_truetypid(a, t),
3673
- information_schema._pg_truetypmod(a, t)
3674
- ) AS "numericPrecision",
3675
- information_schema._pg_numeric_scale(
3676
- information_schema._pg_truetypid(a, t),
3677
- information_schema._pg_truetypmod(a, t)
3678
- ) AS "numericScale",
3679
- information_schema._pg_datetime_precision(
3680
- information_schema._pg_truetypid(a, t),
3681
- information_schema._pg_truetypmod(a, t)
3682
- ) AS "dateTimePrecision",
3666
+ a.attndims AS "arrayDims",
3667
+ information_schema._pg_char_max_length(tt.id, tt.mod) "maxChars",
3668
+ information_schema._pg_numeric_precision(tt.id, tt.mod) "numericPrecision",
3669
+ information_schema._pg_numeric_scale(tt.id,tt.mod) "numericScale",
3670
+ information_schema._pg_datetime_precision(tt.id,tt.mod) "dateTimePrecision",
3683
3671
  CAST(
3684
3672
  CASE WHEN a.attgenerated = ''
3685
3673
  THEN pg_get_expr(ad.adbin, ad.adrelid)
@@ -3702,7 +3690,7 @@ const columnsSql = ({
3702
3690
  nullif(seq.seqmin, 1),
3703
3691
  'max',
3704
3692
  nullif(seq.seqmax, (
3705
- CASE COALESCE(et.typname, t.typname)
3693
+ CASE t.typname
3706
3694
  WHEN 'int2' THEN 32767
3707
3695
  WHEN 'int4' THEN 2147483647
3708
3696
  WHEN 'int8' THEN 9223372036854775807
@@ -3719,8 +3707,18 @@ const columnsSql = ({
3719
3707
  FROM pg_attribute a
3720
3708
  ${join}
3721
3709
  LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
3722
- JOIN pg_type t ON a.atttypid = t.oid
3723
- LEFT JOIN pg_type et ON t.typelem = et.oid
3710
+ JOIN pg_type t
3711
+ ON t.oid = (
3712
+ CASE WHEN a.attndims = 0
3713
+ THEN a.atttypid
3714
+ ELSE (SELECT t.typelem FROM pg_type t WHERE t.oid = a.atttypid)
3715
+ END
3716
+ )
3717
+ JOIN LATERAL (
3718
+ SELECT
3719
+ CASE WHEN t.typtype = 'd' THEN t.typbasetype ELSE t.oid END id,
3720
+ CASE WHEN t.typtype = 'd' THEN t.typtypmod ELSE a.atttypmod END mod
3721
+ ) tt ON true
3724
3722
  JOIN pg_namespace tn ON tn.oid = t.typnamespace
3725
3723
  LEFT JOIN (pg_collation co JOIN pg_namespace nco ON (co.collnamespace = nco.oid))
3726
3724
  ON a.attcollation = co.oid AND (nco.nspname, co.collname) <> ('pg_catalog', 'default')
@@ -3979,7 +3977,7 @@ const domainsSql = `SELECT
3979
3977
  t.typname AS "type",
3980
3978
  s.nspname AS "typeSchema",
3981
3979
  NOT d.typnotnull AS "isNullable",
3982
- d.typcategory = 'A' AS "isArray",
3980
+ d.typndims AS "arrayDims",
3983
3981
  character_maximum_length AS "maxChars",
3984
3982
  numeric_precision AS "numericPrecision",
3985
3983
  numeric_scale AS "numericScale",
@@ -4245,7 +4243,7 @@ const makeDomainsMap = (ctx, data) => {
4245
4243
  name: it.name,
4246
4244
  type: it.type,
4247
4245
  typeSchema: it.typeSchema,
4248
- isArray: it.isArray,
4246
+ arrayDims: it.arrayDims,
4249
4247
  tableName: "",
4250
4248
  isNullable: it.isNullable,
4251
4249
  collate: it.collate,
@@ -4308,11 +4306,19 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
4308
4306
  }
4309
4307
  }
4310
4308
  column.data.name = void 0;
4311
- return dbColumn.isArray ? new ArrayColumn(
4312
- ctx.columnSchemaConfig,
4313
- column,
4314
- ctx.columnSchemaConfig.type
4315
- ) : column;
4309
+ if (!column.data.isNullable)
4310
+ column.data.isNullable = void 0;
4311
+ if (dbColumn.arrayDims) {
4312
+ const arr = new ArrayColumn(
4313
+ ctx.columnSchemaConfig,
4314
+ column,
4315
+ ctx.columnSchemaConfig.type
4316
+ );
4317
+ arr.data.isNullable = dbColumn.isNullable;
4318
+ arr.data.arrayDims = dbColumn.arrayDims;
4319
+ column = arr;
4320
+ }
4321
+ return column;
4316
4322
  };
4317
4323
  const instantiateColumnByDbType = (ctx, type, isSerial, params) => {
4318
4324
  const columnFn = ctx.columnsByType[!isSerial ? type : type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial"];
@@ -4976,11 +4982,17 @@ const astEncoders = {
4976
4982
  ast.shape.createdAt,
4977
4983
  ast.shape.updatedAt
4978
4984
  );
4985
+ const toCodeCtx = {
4986
+ t: "t",
4987
+ table: ast.name,
4988
+ migration: true,
4989
+ snakeCase: config.snakeCase
4990
+ };
4979
4991
  for (const key in ast.shape) {
4980
4992
  if (timestamps.hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
4981
4993
  continue;
4982
4994
  const line = [`${quoteObjectKey(key)}: `];
4983
- const columnCode = columnToCode(key, ast.shape[key], config.snakeCase);
4995
+ const columnCode = ast.shape[key].toCode(toCodeCtx, key);
4984
4996
  for (const part of columnCode) {
4985
4997
  addCode(line, part);
4986
4998
  }
@@ -5030,6 +5042,12 @@ const astEncoders = {
5030
5042
  );
5031
5043
  }
5032
5044
  );
5045
+ const toCodeCtx = {
5046
+ t: "t",
5047
+ table: ast.name,
5048
+ migration: true,
5049
+ snakeCase: config.snakeCase
5050
+ };
5033
5051
  for (const key in ast.shape) {
5034
5052
  const changes = toArray(ast.shape[key]);
5035
5053
  for (const change of changes) {
@@ -5042,7 +5060,7 @@ const astEncoders = {
5042
5060
  (_a = change.item.data.name) != null ? _a : key
5043
5061
  )})` : `${quoteObjectKey(key)}: t.${change.type}(`
5044
5062
  ];
5045
- const columnCode = columnToCode(key, change.item, config.snakeCase);
5063
+ const columnCode = change.item.toCode(toCodeCtx, key);
5046
5064
  for (let i = 0; i < columnCode.length; i++) {
5047
5065
  let part = columnCode[i];
5048
5066
  if (recreate && !i)
@@ -5057,16 +5075,20 @@ const astEncoders = {
5057
5075
  const line = [
5058
5076
  `${quoteObjectKey(key)}: t${change.name ? `.name(${singleQuote(change.name)})` : ""}.change(`
5059
5077
  ];
5060
- const fromCode = columnToCode(
5061
- key,
5062
- change.from.column,
5063
- config.snakeCase
5078
+ const fromCode = change.from.column.toCode(
5079
+ {
5080
+ t: "t",
5081
+ table: ast.name,
5082
+ migration: true,
5083
+ snakeCase: config.snakeCase
5084
+ },
5085
+ key
5064
5086
  );
5065
5087
  for (const part of fromCode) {
5066
5088
  addCode(line, part);
5067
5089
  }
5068
5090
  addCode(line, ", ");
5069
- const toCode = columnToCode(key, change.to.column, config.snakeCase);
5091
+ const toCode = change.to.column.toCode(toCodeCtx, key);
5070
5092
  for (const part of toCode) {
5071
5093
  addCode(line, part);
5072
5094
  }
@@ -5190,9 +5212,13 @@ const astEncoders = {
5190
5212
  )}, [${ast.fromValues.map(singleQuote).join(", ")}], [${ast.toValues.map(singleQuote).join(", ")}]);`;
5191
5213
  },
5192
5214
  domain(ast) {
5215
+ var _a;
5193
5216
  return `await db.${ast.action}Domain(${quoteSchemaTable(
5194
5217
  ast
5195
- )}, (t) => ${ast.baseType.toCode("t")});`;
5218
+ )}, (t) => ${ast.baseType.toCode(
5219
+ { t: "t", table: ast.name },
5220
+ (_a = ast.baseType.data.name) != null ? _a : ""
5221
+ )});`;
5196
5222
  },
5197
5223
  collation(ast) {
5198
5224
  const params = [];