rake-db 2.25.9 → 2.25.11
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 +38 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
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)}` : ""}${
|
|
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)) {
|
|
@@ -2483,11 +2484,15 @@ const recreateEnum = async (migration, { schema, name }, values, errorMessage) =
|
|
|
2483
2484
|
const { rows: tables } = await migration.adapter.query(
|
|
2484
2485
|
`SELECT n.nspname AS "schema",
|
|
2485
2486
|
c.relname AS "table",
|
|
2486
|
-
json_agg(
|
|
2487
|
+
json_agg(
|
|
2488
|
+
json_build_object('name', a.attname, 'arrayDims', a.attndims)
|
|
2489
|
+
ORDER BY a.attnum
|
|
2490
|
+
) AS "columns"
|
|
2487
2491
|
FROM pg_class c
|
|
2488
2492
|
JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
|
|
2489
|
-
JOIN
|
|
2490
|
-
JOIN pg_type t ON
|
|
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
|
|
2491
2496
|
JOIN pg_namespace tn ON tn.oid = t.typnamespace AND tn.nspname = ${orchidCore.singleQuote(
|
|
2492
2497
|
schema ?? defaultSchema
|
|
2493
2498
|
)}
|
|
@@ -2496,7 +2501,9 @@ GROUP BY n.nspname, c.relname`
|
|
|
2496
2501
|
);
|
|
2497
2502
|
const sql = tables.map(
|
|
2498
2503
|
(t) => `ALTER TABLE ${quoteTable(t.schema, t.table)}
|
|
2499
|
-
${t.columns.map(
|
|
2504
|
+
${t.columns.map(
|
|
2505
|
+
(c) => ` ALTER COLUMN "${c.name}" TYPE text${"[]".repeat(c.arrayDims)}`
|
|
2506
|
+
).join(",\n")}`
|
|
2500
2507
|
);
|
|
2501
2508
|
sql.push(
|
|
2502
2509
|
`DROP TYPE ${quotedName}`,
|
|
@@ -2506,14 +2513,17 @@ GROUP BY n.nspname, c.relname`
|
|
|
2506
2513
|
for (const t of tables) {
|
|
2507
2514
|
const table = quoteTable(t.schema, t.table);
|
|
2508
2515
|
for (const c of t.columns) {
|
|
2516
|
+
const type = quotedName + "[]".repeat(c.arrayDims);
|
|
2509
2517
|
try {
|
|
2510
2518
|
await migration.adapter.query(
|
|
2511
2519
|
`ALTER TABLE ${table}
|
|
2512
|
-
ALTER COLUMN "${c}" TYPE ${
|
|
2520
|
+
ALTER COLUMN "${c.name}" TYPE ${type} USING "${c.name}"::${type}`
|
|
2513
2521
|
);
|
|
2514
2522
|
} catch (err) {
|
|
2515
2523
|
if (err.code === "22P02") {
|
|
2516
|
-
throw new Error(errorMessage(quotedName, table, c), {
|
|
2524
|
+
throw new Error(errorMessage(quotedName, table, c.name), {
|
|
2525
|
+
cause: err
|
|
2526
|
+
});
|
|
2517
2527
|
}
|
|
2518
2528
|
throw err;
|
|
2519
2529
|
}
|
|
@@ -4240,7 +4250,8 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
|
|
|
4240
4250
|
if (domainColumn) {
|
|
4241
4251
|
column = new pqb.DomainColumn(
|
|
4242
4252
|
ctx.columnSchemaConfig,
|
|
4243
|
-
|
|
4253
|
+
typeName,
|
|
4254
|
+
typeSchema,
|
|
4244
4255
|
dbColumn.extension
|
|
4245
4256
|
).as(domainColumn);
|
|
4246
4257
|
} else {
|
|
@@ -4257,7 +4268,8 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
|
|
|
4257
4268
|
} else {
|
|
4258
4269
|
column = new pqb.CustomTypeColumn(
|
|
4259
4270
|
ctx.columnSchemaConfig,
|
|
4260
|
-
|
|
4271
|
+
typeName,
|
|
4272
|
+
typeSchema === "pg_catalog" ? void 0 : typeSchema,
|
|
4261
4273
|
dbColumn.extension
|
|
4262
4274
|
);
|
|
4263
4275
|
((_a = ctx.unsupportedTypes)[_b = dbColumn.type] ?? (_a[_b] = [])).push(
|
|
@@ -4941,7 +4953,7 @@ ${group.map(
|
|
|
4941
4953
|
return code;
|
|
4942
4954
|
};
|
|
4943
4955
|
const astEncoders = {
|
|
4944
|
-
table(ast, config) {
|
|
4956
|
+
table(ast, config, currentSchema) {
|
|
4945
4957
|
let code = [];
|
|
4946
4958
|
const result = code;
|
|
4947
4959
|
const hasOptions = Boolean(ast.comment || ast.noPrimaryKey === "ignore");
|
|
@@ -4975,13 +4987,14 @@ const astEncoders = {
|
|
|
4975
4987
|
const toCodeCtx = {
|
|
4976
4988
|
t: "t",
|
|
4977
4989
|
table: ast.name,
|
|
4990
|
+
currentSchema,
|
|
4978
4991
|
migration: true,
|
|
4979
4992
|
snakeCase: config.snakeCase
|
|
4980
4993
|
};
|
|
4981
4994
|
for (const key in ast.shape) {
|
|
4982
4995
|
if (timestamps.hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
|
|
4983
4996
|
continue;
|
|
4984
|
-
const line = [`${orchidCore.quoteObjectKey(key)}: `];
|
|
4997
|
+
const line = [`${orchidCore.quoteObjectKey(key, config.snakeCase)}: `];
|
|
4985
4998
|
const columnCode = ast.shape[key].toCode(toCodeCtx, key);
|
|
4986
4999
|
for (const part of columnCode) {
|
|
4987
5000
|
orchidCore.addCode(line, part);
|
|
@@ -5030,6 +5043,7 @@ const astEncoders = {
|
|
|
5030
5043
|
const toCodeCtx = {
|
|
5031
5044
|
t: "t",
|
|
5032
5045
|
table: ast.name,
|
|
5046
|
+
currentSchema,
|
|
5033
5047
|
migration: true,
|
|
5034
5048
|
snakeCase: config.snakeCase
|
|
5035
5049
|
};
|
|
@@ -5043,7 +5057,7 @@ const astEncoders = {
|
|
|
5043
5057
|
const line = [
|
|
5044
5058
|
recreate ? `...t.${change.type}(t.name(${orchidCore.singleQuote(
|
|
5045
5059
|
change.item.data.name ?? key
|
|
5046
|
-
)})` : `${orchidCore.quoteObjectKey(key)}: t.${change.type}(`
|
|
5060
|
+
)})` : `${orchidCore.quoteObjectKey(key, config.snakeCase)}: t.${change.type}(`
|
|
5047
5061
|
];
|
|
5048
5062
|
const columnCode = change.item.toCode(toCodeCtx, key);
|
|
5049
5063
|
for (let i = 0; i < columnCode.length; i++) {
|
|
@@ -5056,12 +5070,13 @@ const astEncoders = {
|
|
|
5056
5070
|
} else if (change.type === "change") {
|
|
5057
5071
|
if (!change.from.column || !change.to.column) continue;
|
|
5058
5072
|
const line = [
|
|
5059
|
-
`${orchidCore.quoteObjectKey(key)}: t${change.name ? `.name(${orchidCore.singleQuote(change.name)})` : ""}.change(`
|
|
5073
|
+
`${orchidCore.quoteObjectKey(key, config.snakeCase)}: t${change.name ? `.name(${orchidCore.singleQuote(change.name)})` : ""}.change(`
|
|
5060
5074
|
];
|
|
5061
5075
|
const fromCode = change.from.column.toCode(
|
|
5062
5076
|
{
|
|
5063
5077
|
t: "t",
|
|
5064
5078
|
table: ast.name,
|
|
5079
|
+
currentSchema,
|
|
5065
5080
|
migration: true,
|
|
5066
5081
|
snakeCase: config.snakeCase
|
|
5067
5082
|
},
|
|
@@ -5091,7 +5106,9 @@ const astEncoders = {
|
|
|
5091
5106
|
code.push(line);
|
|
5092
5107
|
} else if (change.type === "rename") {
|
|
5093
5108
|
code.push([
|
|
5094
|
-
`${orchidCore.quoteObjectKey(key)}: t.rename(${orchidCore.singleQuote(
|
|
5109
|
+
`${orchidCore.quoteObjectKey(key, config.snakeCase)}: t.rename(${orchidCore.singleQuote(
|
|
5110
|
+
change.name
|
|
5111
|
+
)}),`
|
|
5095
5112
|
]);
|
|
5096
5113
|
} else {
|
|
5097
5114
|
exhaustive(change.type);
|
|
@@ -5188,21 +5205,23 @@ const astEncoders = {
|
|
|
5188
5205
|
ast
|
|
5189
5206
|
)}, [${ast.values.map(orchidCore.singleQuote).join(", ")}]);`;
|
|
5190
5207
|
},
|
|
5191
|
-
renameEnumValues(ast) {
|
|
5208
|
+
renameEnumValues(ast, config) {
|
|
5192
5209
|
return `await db.renameEnumValues(${quoteSchemaTable(
|
|
5193
5210
|
ast
|
|
5194
|
-
)}, { ${Object.entries(ast.values).map(
|
|
5211
|
+
)}, { ${Object.entries(ast.values).map(
|
|
5212
|
+
([from, to]) => `${orchidCore.quoteObjectKey(from, config.snakeCase)}: ${orchidCore.singleQuote(to)}`
|
|
5213
|
+
).join(", ")} });`;
|
|
5195
5214
|
},
|
|
5196
5215
|
changeEnumValues(ast) {
|
|
5197
5216
|
return `await db.changeEnumValues(${quoteSchemaTable(
|
|
5198
5217
|
ast
|
|
5199
5218
|
)}, [${ast.fromValues.map(orchidCore.singleQuote).join(", ")}], [${ast.toValues.map(orchidCore.singleQuote).join(", ")}]);`;
|
|
5200
5219
|
},
|
|
5201
|
-
domain(ast) {
|
|
5220
|
+
domain(ast, _, currentSchema) {
|
|
5202
5221
|
return `await db.${ast.action}Domain(${quoteSchemaTable(
|
|
5203
5222
|
ast
|
|
5204
5223
|
)}, (t) => ${ast.baseType.toCode(
|
|
5205
|
-
{ t: "t", table: ast.name },
|
|
5224
|
+
{ t: "t", table: ast.name, currentSchema },
|
|
5206
5225
|
ast.baseType.data.name ?? ""
|
|
5207
5226
|
)});`;
|
|
5208
5227
|
},
|