rake-db 2.25.8 → 2.25.10

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.js CHANGED
@@ -162,7 +162,7 @@ const encodeColumnDefault = (def, values, column) => {
162
162
  return def.toSQL({ values });
163
163
  } else {
164
164
  return pqb.escapeForMigration(
165
- column?.data.encode ? column.data.encode(def) : def
165
+ column instanceof pqb.ArrayColumn && Array.isArray(def) ? "{" + (column.data.item.data.encode ? def.map((x) => column.data.item.data.encode(x)) : def).join(",") + "}" : column?.data.encode ? column.data.encode(def) : def
166
166
  );
167
167
  }
168
168
  }
@@ -1179,8 +1179,9 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
1179
1179
  if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
1180
1180
  changeType = true;
1181
1181
  const type = !to.column || to.column.data.isOfCustomType ? to.column && to.column instanceof pqb.DomainColumn ? quoteNameFromString(to.type) : quoteCustomType(to.type) : to.type;
1182
+ const using = item.using?.usingUp ? ` USING ${item.using.usingUp.toSQL({ values })}` : to.column instanceof pqb.EnumColumn ? ` USING "${name}"::text::${type}` : to.column instanceof pqb.ArrayColumn ? ` USING "${name}"::text[]::${type}` : "";
1182
1183
  alterTable.push(
1183
- `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${item.using?.usingUp ? ` USING ${item.using.usingUp.toSQL({ values })}` : to.column instanceof pqb.EnumColumn ? ` USING "${name}"::text::${type}` : ""}`
1184
+ `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${using}`
1184
1185
  );
1185
1186
  }
1186
1187
  if (typeof from.identity !== typeof to.identity || !orchidCore.deepCompare(from.identity, to.identity)) {
@@ -2479,22 +2480,30 @@ const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
2479
2480
  const recreateEnum = async (migration, { schema, name }, values, errorMessage) => {
2480
2481
  const defaultSchema = migration.adapter.schema;
2481
2482
  const quotedName = quoteTable(schema, name);
2483
+ const relKinds = ["r", "m"];
2482
2484
  const { rows: tables } = await migration.adapter.query(
2483
2485
  `SELECT n.nspname AS "schema",
2484
2486
  c.relname AS "table",
2485
- json_agg(a.attname ORDER BY a.attnum) AS "columns"
2487
+ json_agg(
2488
+ json_build_object('name', a.attname, 'arrayDims', a.attndims)
2489
+ ORDER BY a.attnum
2490
+ ) AS "columns"
2486
2491
  FROM pg_class c
2487
2492
  JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
2488
- JOIN pg_attribute a ON a.attrelid = c.oid
2489
- JOIN pg_type t ON a.atttypid = t.oid AND t.typname = ${orchidCore.singleQuote(name)}
2493
+ JOIN pg_type bt ON bt.typname = ${orchidCore.singleQuote(name)}
2494
+ JOIN pg_type t ON t.oid = bt.oid OR t.typelem = bt.oid
2495
+ JOIN pg_attribute a ON a.attrelid = c.oid AND a.atttypid = t.oid
2490
2496
  JOIN pg_namespace tn ON tn.oid = t.typnamespace AND tn.nspname = ${orchidCore.singleQuote(
2491
2497
  schema ?? defaultSchema
2492
2498
  )}
2499
+ WHERE c.relkind IN (${relKinds.map((c) => `'${c}'`).join(", ")})
2493
2500
  GROUP BY n.nspname, c.relname`
2494
2501
  );
2495
2502
  const sql = tables.map(
2496
2503
  (t) => `ALTER TABLE ${quoteTable(t.schema, t.table)}
2497
- ${t.columns.map((c) => ` ALTER COLUMN "${c}" TYPE text`).join(",\n")}`
2504
+ ${t.columns.map(
2505
+ (c) => ` ALTER COLUMN "${c.name}" TYPE text${"[]".repeat(c.arrayDims)}`
2506
+ ).join(",\n")}`
2498
2507
  );
2499
2508
  sql.push(
2500
2509
  `DROP TYPE ${quotedName}`,
@@ -2504,14 +2513,17 @@ GROUP BY n.nspname, c.relname`
2504
2513
  for (const t of tables) {
2505
2514
  const table = quoteTable(t.schema, t.table);
2506
2515
  for (const c of t.columns) {
2516
+ const type = quotedName + "[]".repeat(c.arrayDims);
2507
2517
  try {
2508
2518
  await migration.adapter.query(
2509
2519
  `ALTER TABLE ${table}
2510
- ALTER COLUMN "${c}" TYPE ${quotedName} USING "${c}"::${quotedName}`
2520
+ ALTER COLUMN "${c.name}" TYPE ${type} USING "${c.name}"::${type}`
2511
2521
  );
2512
2522
  } catch (err) {
2513
2523
  if (err.code === "22P02") {
2514
- throw new Error(errorMessage(quotedName, table, c), { cause: err });
2524
+ throw new Error(errorMessage(quotedName, table, c.name), {
2525
+ cause: err
2526
+ });
2515
2527
  }
2516
2528
  throw err;
2517
2529
  }
@@ -4238,7 +4250,8 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
4238
4250
  if (domainColumn) {
4239
4251
  column = new pqb.DomainColumn(
4240
4252
  ctx.columnSchemaConfig,
4241
- typeId,
4253
+ typeName,
4254
+ typeSchema,
4242
4255
  dbColumn.extension
4243
4256
  ).as(domainColumn);
4244
4257
  } else {
@@ -4255,7 +4268,8 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
4255
4268
  } else {
4256
4269
  column = new pqb.CustomTypeColumn(
4257
4270
  ctx.columnSchemaConfig,
4258
- typeId,
4271
+ typeName,
4272
+ typeSchema === "pg_catalog" ? void 0 : typeSchema,
4259
4273
  dbColumn.extension
4260
4274
  );
4261
4275
  ((_a = ctx.unsupportedTypes)[_b = dbColumn.type] ?? (_a[_b] = [])).push(
@@ -4852,14 +4866,16 @@ const astToMigration = (currentSchema, config, asts) => {
4852
4866
  const item = items[i];
4853
4867
  let satisfied = true;
4854
4868
  for (const dep of item.deps) {
4855
- if (toBeAdded.has(dep) && !added.has(dep)) {
4869
+ if (toBeAdded.has(dep) && !added.has(dep) && !item.add.has(dep)) {
4856
4870
  satisfied = false;
4857
4871
  break;
4858
4872
  }
4859
4873
  }
4860
4874
  if (satisfied) {
4861
- for (const key of item.drop) {
4862
- if (remainingDeps.has(key)) {
4875
+ for (const dep of item.drop) {
4876
+ const selfRef = item.deps.has(dep);
4877
+ const depsLeft = remainingDeps.get(dep);
4878
+ if (depsLeft && depsLeft > (selfRef ? 1 : 0)) {
4863
4879
  satisfied = false;
4864
4880
  break;
4865
4881
  }
@@ -4937,7 +4953,7 @@ ${group.map(
4937
4953
  return code;
4938
4954
  };
4939
4955
  const astEncoders = {
4940
- table(ast, config) {
4956
+ table(ast, config, currentSchema) {
4941
4957
  let code = [];
4942
4958
  const result = code;
4943
4959
  const hasOptions = Boolean(ast.comment || ast.noPrimaryKey === "ignore");
@@ -4971,6 +4987,7 @@ const astEncoders = {
4971
4987
  const toCodeCtx = {
4972
4988
  t: "t",
4973
4989
  table: ast.name,
4990
+ currentSchema,
4974
4991
  migration: true,
4975
4992
  snakeCase: config.snakeCase
4976
4993
  };
@@ -5026,6 +5043,7 @@ const astEncoders = {
5026
5043
  const toCodeCtx = {
5027
5044
  t: "t",
5028
5045
  table: ast.name,
5046
+ currentSchema,
5029
5047
  migration: true,
5030
5048
  snakeCase: config.snakeCase
5031
5049
  };
@@ -5058,6 +5076,7 @@ const astEncoders = {
5058
5076
  {
5059
5077
  t: "t",
5060
5078
  table: ast.name,
5079
+ currentSchema,
5061
5080
  migration: true,
5062
5081
  snakeCase: config.snakeCase
5063
5082
  },
@@ -5194,11 +5213,11 @@ const astEncoders = {
5194
5213
  ast
5195
5214
  )}, [${ast.fromValues.map(orchidCore.singleQuote).join(", ")}], [${ast.toValues.map(orchidCore.singleQuote).join(", ")}]);`;
5196
5215
  },
5197
- domain(ast) {
5216
+ domain(ast, _, currentSchema) {
5198
5217
  return `await db.${ast.action}Domain(${quoteSchemaTable(
5199
5218
  ast
5200
5219
  )}, (t) => ${ast.baseType.toCode(
5201
- { t: "t", table: ast.name },
5220
+ { t: "t", table: ast.name, currentSchema },
5202
5221
  ast.baseType.data.name ?? ""
5203
5222
  )});`;
5204
5223
  },