rake-db 2.8.50 → 2.9.0
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 +36 -36
- package/dist/index.js +23 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { columnTypes,
|
|
2
|
-
import { getCallerFilePath, singleQuote, toSnakeCase,
|
|
1
|
+
import { columnTypes, quote, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, raw, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, referencesArgsToCode, constraintPropsToCode, TimestampTZColumn } from 'pqb';
|
|
2
|
+
import { getCallerFilePath, singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, consumeColumnName, deepCompare, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, quoteObjectKey, backtickQuote } from 'orchid-core';
|
|
3
3
|
import path, { join } from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile, stat, readFile } from 'fs/promises';
|
|
5
5
|
import prompts from 'prompts';
|
|
@@ -308,8 +308,8 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
|
308
308
|
line.push(checkToSql(item.data.check, values));
|
|
309
309
|
}
|
|
310
310
|
if (item.data.default !== void 0) {
|
|
311
|
-
if (typeof item.data.default === "object" && item.data.default &&
|
|
312
|
-
line.push(`DEFAULT ${
|
|
311
|
+
if (typeof item.data.default === "object" && item.data.default && isRawSQL(item.data.default)) {
|
|
312
|
+
line.push(`DEFAULT ${item.data.default.toSQL(values)}`);
|
|
313
313
|
} else {
|
|
314
314
|
line.push(`DEFAULT ${quote(item.data.default)}`);
|
|
315
315
|
}
|
|
@@ -406,7 +406,7 @@ const constraintToSql = ({ name }, up, constraint, values, snakeCase) => {
|
|
|
406
406
|
return sql.join(" ");
|
|
407
407
|
};
|
|
408
408
|
const checkToSql = (check, values) => {
|
|
409
|
-
return `CHECK (${
|
|
409
|
+
return `CHECK (${check.toSQL(values)})`;
|
|
410
410
|
};
|
|
411
411
|
const foreignKeyToSql = (item, snakeCase) => {
|
|
412
412
|
return `FOREIGN KEY (${joinColumns(item.columns)}) ${referencesToSql(
|
|
@@ -492,7 +492,7 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
|
|
|
492
492
|
}
|
|
493
493
|
if (options.where) {
|
|
494
494
|
sql.push(
|
|
495
|
-
`WHERE ${
|
|
495
|
+
`WHERE ${isRawSQL(options.where) ? options.where.toSQL(values) : options.where}`
|
|
496
496
|
);
|
|
497
497
|
}
|
|
498
498
|
return { text: sql.join(" "), values };
|
|
@@ -1036,7 +1036,7 @@ const astToQueries = (ast, snakeCase) => {
|
|
|
1036
1036
|
if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
|
|
1037
1037
|
const type = !to.column || to.column.data.isOfCustomType ? `"${to.type}"` : to.type;
|
|
1038
1038
|
alterTable.push(
|
|
1039
|
-
`ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${item.using ? ` USING ${
|
|
1039
|
+
`ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${item.using ? ` USING ${item.using.toSQL(values)}` : ""}`
|
|
1040
1040
|
);
|
|
1041
1041
|
}
|
|
1042
1042
|
if (typeof from.identity !== typeof to.identity || !deepCompare(from.identity, to.identity)) {
|
|
@@ -1045,7 +1045,7 @@ const astToQueries = (ast, snakeCase) => {
|
|
|
1045
1045
|
);
|
|
1046
1046
|
}
|
|
1047
1047
|
if (from.default !== to.default) {
|
|
1048
|
-
const value = typeof to.default === "object" && to.default &&
|
|
1048
|
+
const value = typeof to.default === "object" && to.default && isRawSQL(to.default) ? to.default.toSQL(values) : quote(to.default);
|
|
1049
1049
|
const expr = value === void 0 ? "DROP DEFAULT" : `SET DEFAULT ${value}`;
|
|
1050
1050
|
alterTable.push(`ALTER COLUMN "${name}" ${expr}`);
|
|
1051
1051
|
}
|
|
@@ -1067,8 +1067,7 @@ const astToQueries = (ast, snakeCase) => {
|
|
|
1067
1067
|
if (to.check) {
|
|
1068
1068
|
alterTable.push(
|
|
1069
1069
|
`ADD CONSTRAINT "${checkName}"
|
|
1070
|
-
CHECK (${
|
|
1071
|
-
to.check,
|
|
1070
|
+
CHECK (${to.check.toSQL(
|
|
1072
1071
|
values
|
|
1073
1072
|
)})`
|
|
1074
1073
|
);
|
|
@@ -1214,7 +1213,7 @@ const createView$1 = async (migration, up, name, options, sql) => {
|
|
|
1214
1213
|
};
|
|
1215
1214
|
const makeAst = (up, name, options, sql) => {
|
|
1216
1215
|
if (typeof sql === "string") {
|
|
1217
|
-
sql = raw(sql);
|
|
1216
|
+
sql = raw({ raw: sql });
|
|
1218
1217
|
}
|
|
1219
1218
|
return {
|
|
1220
1219
|
type: "view",
|
|
@@ -1253,7 +1252,7 @@ const astToQuery = (ast) => {
|
|
|
1253
1252
|
list.push(`security_invoker = true`);
|
|
1254
1253
|
sql.push(`WITH ( ${list.join(", ")} )`);
|
|
1255
1254
|
}
|
|
1256
|
-
sql.push(`AS (${
|
|
1255
|
+
sql.push(`AS (${ast.sql.toSQL(values)})`);
|
|
1257
1256
|
} else {
|
|
1258
1257
|
sql.push("DROP VIEW");
|
|
1259
1258
|
if (options == null ? void 0 : options.dropIfExists)
|
|
@@ -2017,9 +2016,9 @@ const createDomain$1 = async (migration, up, name, fn, options) => {
|
|
|
2017
2016
|
if (ast.action === "create") {
|
|
2018
2017
|
query = `CREATE DOMAIN ${quotedName} AS ${columnTypeToSql(ast.baseType)}${ast.collation ? `
|
|
2019
2018
|
COLLATION ${singleQuote(ast.collation)}` : ""}${ast.default ? `
|
|
2020
|
-
DEFAULT ${
|
|
2019
|
+
DEFAULT ${ast.default.toSQL(values)}` : ""}${ast.notNull || ast.check ? "\n" : ""}${[
|
|
2021
2020
|
ast.notNull && "NOT NULL",
|
|
2022
|
-
ast.check && `CHECK ${
|
|
2021
|
+
ast.check && `CHECK ${ast.check.toSQL(values)}`
|
|
2023
2022
|
].filter(Boolean).join(" ")}`;
|
|
2024
2023
|
} else {
|
|
2025
2024
|
query = `DROP DOMAIN ${quotedName}${ast.cascade ? " CASCADE" : ""}`;
|
|
@@ -2933,7 +2932,7 @@ const structureToAst = async (ctx, db) => {
|
|
|
2933
2932
|
notNull: it.notNull,
|
|
2934
2933
|
collation: it.collation,
|
|
2935
2934
|
default: simplifyColumnDefault(it.default),
|
|
2936
|
-
check: it.check ? raw(it.check) : void 0
|
|
2935
|
+
check: it.check ? raw({ raw: it.check }) : void 0
|
|
2937
2936
|
});
|
|
2938
2937
|
}
|
|
2939
2938
|
for (const key in pendingTables) {
|
|
@@ -3090,7 +3089,7 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
|
|
|
3090
3089
|
onDelete: fkeyActionMap[references.onDelete]
|
|
3091
3090
|
}
|
|
3092
3091
|
} : void 0,
|
|
3093
|
-
check: check ? raw(check.expression) : void 0
|
|
3092
|
+
check: check ? raw({ raw: check.expression }) : void 0
|
|
3094
3093
|
};
|
|
3095
3094
|
const name = item.name && item.name !== getConstraintName(tableName, constraint) ? item.name : void 0;
|
|
3096
3095
|
if (name) {
|
|
@@ -3190,7 +3189,7 @@ const constraintToAst = (ctx, item) => {
|
|
|
3190
3189
|
options.onDelete = onDelete;
|
|
3191
3190
|
}
|
|
3192
3191
|
if (check) {
|
|
3193
|
-
result.check = raw(check.expression);
|
|
3192
|
+
result.check = raw({ raw: check.expression });
|
|
3194
3193
|
}
|
|
3195
3194
|
if (item.name && item.name !== getConstraintName(item.tableName, result)) {
|
|
3196
3195
|
result.name = item.name;
|
|
@@ -3230,7 +3229,7 @@ const viewToAst = (ctx, data, domains, view) => {
|
|
|
3230
3229
|
schema: view.schemaName === ctx.currentSchema ? void 0 : view.schemaName,
|
|
3231
3230
|
name: view.name,
|
|
3232
3231
|
shape,
|
|
3233
|
-
sql: raw(view.sql),
|
|
3232
|
+
sql: raw({ raw: view.sql }),
|
|
3234
3233
|
options
|
|
3235
3234
|
};
|
|
3236
3235
|
};
|
|
@@ -3289,7 +3288,7 @@ const makeColumnsShape = (ctx, data, domains, tableName, columns, primaryKey, in
|
|
|
3289
3288
|
}
|
|
3290
3289
|
const check = checks == null ? void 0 : checks[item.name];
|
|
3291
3290
|
if (check) {
|
|
3292
|
-
column.data.check = raw(check);
|
|
3291
|
+
column.data.check = raw({ raw: check });
|
|
3293
3292
|
}
|
|
3294
3293
|
const camelCaseName = toCamelCase(item.name);
|
|
3295
3294
|
if (ctx.snakeCase) {
|
|
@@ -3400,9 +3399,9 @@ const createDomain = (ast) => {
|
|
|
3400
3399
|
if (ast.collation)
|
|
3401
3400
|
props.push(`collation: ${singleQuote(ast.collation)},`);
|
|
3402
3401
|
if (ast.default)
|
|
3403
|
-
props.push(`default: ${
|
|
3402
|
+
props.push(`default: ${ast.default.toCode("db")},`);
|
|
3404
3403
|
if (ast.check)
|
|
3405
|
-
props.push(`check: ${
|
|
3404
|
+
props.push(`check: ${ast.check.toCode("db")},`);
|
|
3406
3405
|
addCode(code, ", {");
|
|
3407
3406
|
code.push(props);
|
|
3408
3407
|
addCode(code, "}");
|
|
@@ -3473,7 +3472,7 @@ const isTimestamp = (column, type) => {
|
|
|
3473
3472
|
if (!column)
|
|
3474
3473
|
return false;
|
|
3475
3474
|
const { default: def } = column.data;
|
|
3476
|
-
return column instanceof type && !column.data.isNullable && def && typeof def === "object" &&
|
|
3475
|
+
return column instanceof type && !column.data.isNullable && def && typeof def === "object" && isRawSQL(def) && def._sql === "now()";
|
|
3477
3476
|
};
|
|
3478
3477
|
const getTimestampsInfo = (config, ast, type) => {
|
|
3479
3478
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -3503,7 +3502,7 @@ const createConstraint = (item) => {
|
|
|
3503
3502
|
];
|
|
3504
3503
|
}
|
|
3505
3504
|
if (kind === "check" && item.check) {
|
|
3506
|
-
return [`await db.addCheck(${table}, ${
|
|
3505
|
+
return [`await db.addCheck(${table}, ${item.check.toCode("t")});`];
|
|
3507
3506
|
}
|
|
3508
3507
|
return [
|
|
3509
3508
|
`await db.addConstraint(${table}, {`,
|
|
@@ -3528,8 +3527,8 @@ const createView = (ast) => {
|
|
|
3528
3527
|
code.push(options, "}");
|
|
3529
3528
|
}
|
|
3530
3529
|
addCode(code, ", ");
|
|
3531
|
-
if (!ast.sql.
|
|
3532
|
-
const raw = ast.sql.
|
|
3530
|
+
if (!ast.sql._values) {
|
|
3531
|
+
const raw = ast.sql._sql;
|
|
3533
3532
|
let sql;
|
|
3534
3533
|
if (typeof raw === "string") {
|
|
3535
3534
|
sql = raw;
|
|
@@ -3544,7 +3543,7 @@ const createView = (ast) => {
|
|
|
3544
3543
|
}
|
|
3545
3544
|
addCode(code, backtickQuote(sql));
|
|
3546
3545
|
} else {
|
|
3547
|
-
addCode(code,
|
|
3546
|
+
addCode(code, ast.sql.toCode("db"));
|
|
3548
3547
|
}
|
|
3549
3548
|
addCode(code, ");");
|
|
3550
3549
|
return code;
|