rake-db 2.27.16 → 2.27.17
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 +6 -8
- package/dist/index.js +128 -147
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -21
- package/dist/index.mjs.map +1 -1
- package/dist/node-postgres.d.ts +1 -1
- package/dist/node-postgres.js +2 -2
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs +1 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +1 -1
- package/dist/postgres-js.js +2 -2
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs +1 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +2 -10
package/dist/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var pqb = require('pqb');
|
|
4
|
-
var orchidCore = require('orchid-core');
|
|
5
4
|
var path = require('path');
|
|
6
5
|
var node_url = require('node:url');
|
|
7
6
|
var fs = require('fs/promises');
|
|
8
|
-
require('url');
|
|
9
|
-
require('node:path');
|
|
10
7
|
|
|
11
8
|
class RakeDbError extends Error {
|
|
12
9
|
}
|
|
@@ -49,7 +46,7 @@ const ensureMigrationsPath = (config) => {
|
|
|
49
46
|
};
|
|
50
47
|
const ensureBasePathAndDbScript = (config, intermediateCallers = 0) => {
|
|
51
48
|
if (config.basePath && config.dbScript) return config;
|
|
52
|
-
let filePath =
|
|
49
|
+
let filePath = pqb.getStackTrace()?.[3 + intermediateCallers].getFileName();
|
|
53
50
|
if (!filePath) {
|
|
54
51
|
throw new Error(
|
|
55
52
|
"Failed to determine path to db script. Please set basePath option of rakeDb"
|
|
@@ -146,7 +143,7 @@ const quoteCustomType = (s) => {
|
|
|
146
143
|
return schema ? '"' + schema + '".' + type : type;
|
|
147
144
|
};
|
|
148
145
|
const quoteSchemaTable = (arg) => {
|
|
149
|
-
return
|
|
146
|
+
return pqb.singleQuote(concatSchemaAndName(arg));
|
|
150
147
|
};
|
|
151
148
|
const concatSchemaAndName = ({
|
|
152
149
|
schema,
|
|
@@ -173,7 +170,7 @@ const columnTypeToSql = (item) => {
|
|
|
173
170
|
return item.data.isOfCustomType ? item instanceof pqb.DomainColumn ? quoteNameFromString(item.dataType) : quoteCustomType(item.toSQL()) : item.toSQL();
|
|
174
171
|
};
|
|
175
172
|
const getColumnName = (item, key, snakeCase) => {
|
|
176
|
-
return item.data.name || (snakeCase ?
|
|
173
|
+
return item.data.name || (snakeCase ? pqb.toSnakeCase(key) : key);
|
|
177
174
|
};
|
|
178
175
|
const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
179
176
|
const line = [`"${name}" ${columnTypeToSql(item)}`];
|
|
@@ -231,7 +228,7 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
|
231
228
|
};
|
|
232
229
|
const encodeColumnDefault = (def, values, column) => {
|
|
233
230
|
if (def !== void 0 && def !== null && typeof def !== "function") {
|
|
234
|
-
if (
|
|
231
|
+
if (pqb.isRawSQL(def)) {
|
|
235
232
|
return def.toSQL({ values });
|
|
236
233
|
} else {
|
|
237
234
|
return pqb.escapeForMigration(
|
|
@@ -296,7 +293,7 @@ const getConstraintName = (table, constraint, snakeCase) => {
|
|
|
296
293
|
if (constraint.references) {
|
|
297
294
|
let { columns } = constraint.references;
|
|
298
295
|
if (snakeCase) {
|
|
299
|
-
columns = columns.map(
|
|
296
|
+
columns = columns.map(pqb.toSnakeCase);
|
|
300
297
|
}
|
|
301
298
|
return makeConstraintName(table, columns, "fkey");
|
|
302
299
|
}
|
|
@@ -324,14 +321,14 @@ const checkToSql = (check, values) => {
|
|
|
324
321
|
};
|
|
325
322
|
const foreignKeyToSql = (item, snakeCase) => {
|
|
326
323
|
return `FOREIGN KEY (${joinColumns(
|
|
327
|
-
snakeCase ? item.columns.map(
|
|
324
|
+
snakeCase ? item.columns.map(pqb.toSnakeCase) : item.columns
|
|
328
325
|
)}) ${referencesToSql(item, snakeCase)}`;
|
|
329
326
|
};
|
|
330
327
|
const referencesToSql = (references, snakeCase) => {
|
|
331
328
|
const [schema, table] = getForeignKeyTable(references.fnOrTable);
|
|
332
329
|
const sql = [
|
|
333
330
|
`REFERENCES ${quoteTable(schema, table)}(${joinColumns(
|
|
334
|
-
snakeCase ? references.foreignColumns.map(
|
|
331
|
+
snakeCase ? references.foreignColumns.map(pqb.toSnakeCase) : references.foreignColumns
|
|
335
332
|
)})`
|
|
336
333
|
];
|
|
337
334
|
const { options } = references;
|
|
@@ -351,10 +348,10 @@ const makeConstraintName = (table, columns, suffix) => {
|
|
|
351
348
|
const long = `${table}_${columns.join("_")}_${suffix}`;
|
|
352
349
|
if (long.length <= MAX_CONSTRAINT_NAME_LEN) return long;
|
|
353
350
|
for (let partLen = 3; partLen > 0; partLen--) {
|
|
354
|
-
const shorter = `${
|
|
355
|
-
|
|
351
|
+
const shorter = `${pqb.toCamelCase(
|
|
352
|
+
pqb.toSnakeCase(table).split("_").map((p) => p.slice(0, partLen)).join("_")
|
|
356
353
|
)}_${columns.map(
|
|
357
|
-
(c) =>
|
|
354
|
+
(c) => pqb.toCamelCase(
|
|
358
355
|
c.split("_").map((p) => p.slice(0, partLen)).join("_")
|
|
359
356
|
)
|
|
360
357
|
).join("_")}_${suffix}`;
|
|
@@ -363,8 +360,8 @@ const makeConstraintName = (table, columns, suffix) => {
|
|
|
363
360
|
const short = `${table}_${columns.length}columns_${suffix}`;
|
|
364
361
|
if (short.length <= MAX_CONSTRAINT_NAME_LEN) return short;
|
|
365
362
|
for (let partLen = 3; partLen > 0; partLen--) {
|
|
366
|
-
const short2 = `${
|
|
367
|
-
|
|
363
|
+
const short2 = `${pqb.toCamelCase(
|
|
364
|
+
pqb.toSnakeCase(table).split("_").map((p) => p.slice(0, partLen)).join("_")
|
|
368
365
|
)}_${columns.length}columns_${suffix}`;
|
|
369
366
|
if (short2.length <= MAX_CONSTRAINT_NAME_LEN) return short2;
|
|
370
367
|
}
|
|
@@ -446,7 +443,7 @@ const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, lan
|
|
|
446
443
|
}
|
|
447
444
|
if (options.where) {
|
|
448
445
|
sql.push(
|
|
449
|
-
`WHERE ${
|
|
446
|
+
`WHERE ${pqb.isRawSQL(options.where) ? options.where.toSQL({ values }) : options.where}`
|
|
450
447
|
);
|
|
451
448
|
}
|
|
452
449
|
return { text: sql.join(" "), values };
|
|
@@ -489,19 +486,19 @@ const excludesToQuery = (up, { schema, name: tableName }, excludes, snakeCase) =
|
|
|
489
486
|
include?.length && `INCLUDE (${include.map((column) => `"${column}"`).join(", ")})`,
|
|
490
487
|
options.with && `WITH (${options.with})`,
|
|
491
488
|
options.tablespace && `USING INDEX TABLESPACE ${options.tablespace}`,
|
|
492
|
-
options.where && `WHERE ${
|
|
489
|
+
options.where && `WHERE ${pqb.isRawSQL(options.where) ? options.where.toSQL({ values }) : options.where}`
|
|
493
490
|
].filter((x) => !!x).join(" ");
|
|
494
491
|
return { text, values };
|
|
495
492
|
});
|
|
496
493
|
};
|
|
497
494
|
const getIndexOrExcludeMainOptions = (tableName, item, getName, snakeCase) => {
|
|
498
|
-
let include = item.options.include ?
|
|
495
|
+
let include = item.options.include ? pqb.toArray(item.options.include) : void 0;
|
|
499
496
|
let { columns } = item;
|
|
500
497
|
if (snakeCase) {
|
|
501
498
|
columns = columns.map(
|
|
502
|
-
(c) => "column" in c ? { ...c, column:
|
|
499
|
+
(c) => "column" in c ? { ...c, column: pqb.toSnakeCase(c.column) } : c
|
|
503
500
|
);
|
|
504
|
-
if (include) include = include.map(
|
|
501
|
+
if (include) include = include.map(pqb.toSnakeCase);
|
|
505
502
|
}
|
|
506
503
|
return {
|
|
507
504
|
columns,
|
|
@@ -560,7 +557,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
560
557
|
fn = second;
|
|
561
558
|
dataFn = third;
|
|
562
559
|
} else {
|
|
563
|
-
options =
|
|
560
|
+
options = pqb.emptyObject;
|
|
564
561
|
fn = first;
|
|
565
562
|
dataFn = second;
|
|
566
563
|
}
|
|
@@ -570,7 +567,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
570
567
|
Object.create(migration.columnTypes),
|
|
571
568
|
tableMethods
|
|
572
569
|
);
|
|
573
|
-
types[
|
|
570
|
+
types[pqb.snakeCaseKey] = snakeCase;
|
|
574
571
|
let shape;
|
|
575
572
|
let tableData;
|
|
576
573
|
if (fn) {
|
|
@@ -586,7 +583,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
586
583
|
x.name = `${tableName}_check${i === 0 ? "" : i}`;
|
|
587
584
|
});
|
|
588
585
|
} else {
|
|
589
|
-
shape = tableData =
|
|
586
|
+
shape = tableData = pqb.emptyObject;
|
|
590
587
|
}
|
|
591
588
|
const ast = makeAst$2(
|
|
592
589
|
up,
|
|
@@ -772,7 +769,7 @@ const resetChangeTableData = () => {
|
|
|
772
769
|
};
|
|
773
770
|
const addOrDropChanges = [];
|
|
774
771
|
function add(item, options) {
|
|
775
|
-
|
|
772
|
+
pqb.consumeColumnName();
|
|
776
773
|
setName(this, item);
|
|
777
774
|
if (item instanceof pqb.ColumnType) {
|
|
778
775
|
const result = addOrDrop("add", item, options);
|
|
@@ -781,7 +778,7 @@ function add(item, options) {
|
|
|
781
778
|
return addOrDropChanges.length - 1;
|
|
782
779
|
}
|
|
783
780
|
for (const key in item) {
|
|
784
|
-
if (item[key] instanceof
|
|
781
|
+
if (item[key] instanceof pqb.ColumnTypeBase) {
|
|
785
782
|
const result = {};
|
|
786
783
|
for (const key2 in item) {
|
|
787
784
|
result[key2] = {
|
|
@@ -798,7 +795,7 @@ function add(item, options) {
|
|
|
798
795
|
return void 0;
|
|
799
796
|
}
|
|
800
797
|
const drop = function(item, options) {
|
|
801
|
-
|
|
798
|
+
pqb.consumeColumnName();
|
|
802
799
|
setName(this, item);
|
|
803
800
|
if (item instanceof pqb.ColumnType) {
|
|
804
801
|
const result = addOrDrop("drop", item, options);
|
|
@@ -807,7 +804,7 @@ const drop = function(item, options) {
|
|
|
807
804
|
return addOrDropChanges.length - 1;
|
|
808
805
|
}
|
|
809
806
|
for (const key in item) {
|
|
810
|
-
if (item[key] instanceof
|
|
807
|
+
if (item[key] instanceof pqb.ColumnTypeBase) {
|
|
811
808
|
const result = {};
|
|
812
809
|
for (const key2 in item) {
|
|
813
810
|
result[key2] = {
|
|
@@ -883,7 +880,7 @@ const tableChangeMethods = {
|
|
|
883
880
|
...tableMethods,
|
|
884
881
|
...pqb.tableDataMethods,
|
|
885
882
|
name(name) {
|
|
886
|
-
|
|
883
|
+
pqb.setCurrentColumnName(name);
|
|
887
884
|
const types = Object.create(this);
|
|
888
885
|
types[nameKey] = name;
|
|
889
886
|
return types;
|
|
@@ -891,7 +888,7 @@ const tableChangeMethods = {
|
|
|
891
888
|
add,
|
|
892
889
|
drop,
|
|
893
890
|
change(from, to, using) {
|
|
894
|
-
|
|
891
|
+
pqb.consumeColumnName();
|
|
895
892
|
const f = columnTypeToColumnChange(from);
|
|
896
893
|
const t = columnTypeToColumnChange(to);
|
|
897
894
|
setName(this, f);
|
|
@@ -949,13 +946,13 @@ const tableChangeMethods = {
|
|
|
949
946
|
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
950
947
|
const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
|
|
951
948
|
const language = "language" in options ? options.language : migration.options.language;
|
|
952
|
-
|
|
949
|
+
pqb.setDefaultLanguage(language);
|
|
953
950
|
resetChangeTableData();
|
|
954
951
|
const tableChanger = Object.create(
|
|
955
952
|
migration.columnTypes
|
|
956
953
|
);
|
|
957
954
|
Object.assign(tableChanger, tableChangeMethods);
|
|
958
|
-
tableChanger[
|
|
955
|
+
tableChanger[pqb.snakeCaseKey] = snakeCase;
|
|
959
956
|
addOrDropChanges.length = 0;
|
|
960
957
|
const changeData = fn?.(tableChanger) || {};
|
|
961
958
|
const ast = makeAst$1(up, tableName, changeData, changeTableData, options);
|
|
@@ -1004,7 +1001,7 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
|
|
|
1004
1001
|
if (!name2) {
|
|
1005
1002
|
throw new Error(`Column in ...t.${change.type}() must have a name`);
|
|
1006
1003
|
}
|
|
1007
|
-
const arr = shape[name2] ?
|
|
1004
|
+
const arr = shape[name2] ? pqb.toArray(shape[name2]) : [];
|
|
1008
1005
|
arr[up ? "push" : "unshift"](
|
|
1009
1006
|
up ? change : { ...change, type: change.type === "add" ? "drop" : "add" }
|
|
1010
1007
|
);
|
|
@@ -1063,14 +1060,14 @@ const astToQueries = (ast, snakeCase, language) => {
|
|
|
1063
1060
|
addPrimaryKeys.name = ast.add.primaryKey.name;
|
|
1064
1061
|
const { columns } = ast.add.primaryKey;
|
|
1065
1062
|
addPrimaryKeys.columns.push(
|
|
1066
|
-
...snakeCase ? columns.map(
|
|
1063
|
+
...snakeCase ? columns.map(pqb.toSnakeCase) : columns
|
|
1067
1064
|
);
|
|
1068
1065
|
}
|
|
1069
1066
|
if (ast.drop.primaryKey) {
|
|
1070
1067
|
dropPrimaryKeys.name = ast.drop.primaryKey.name;
|
|
1071
1068
|
const { columns } = ast.drop.primaryKey;
|
|
1072
1069
|
dropPrimaryKeys.columns.push(
|
|
1073
|
-
...snakeCase ? columns.map(
|
|
1070
|
+
...snakeCase ? columns.map(pqb.toSnakeCase) : columns
|
|
1074
1071
|
);
|
|
1075
1072
|
}
|
|
1076
1073
|
const alterTable = [];
|
|
@@ -1143,7 +1140,7 @@ const astToQueries = (ast, snakeCase, language) => {
|
|
|
1143
1140
|
`ADD ${primaryKeyToSql(
|
|
1144
1141
|
snakeCase ? {
|
|
1145
1142
|
name: addPrimaryKeys.name,
|
|
1146
|
-
columns: addPrimaryKeys.columns.map(
|
|
1143
|
+
columns: addPrimaryKeys.columns.map(pqb.toSnakeCase)
|
|
1147
1144
|
} : addPrimaryKeys
|
|
1148
1145
|
)}`
|
|
1149
1146
|
);
|
|
@@ -1203,13 +1200,13 @@ const handlePrerequisitesForTableItem = (key, item, queries, addPrimaryKeys, dro
|
|
|
1203
1200
|
}
|
|
1204
1201
|
if (item.from.primaryKey) {
|
|
1205
1202
|
dropPrimaryKeys.columns.push(
|
|
1206
|
-
item.from.column ? getColumnName(item.from.column, key, snakeCase) : snakeCase ?
|
|
1203
|
+
item.from.column ? getColumnName(item.from.column, key, snakeCase) : snakeCase ? pqb.toSnakeCase(key) : key
|
|
1207
1204
|
);
|
|
1208
1205
|
dropPrimaryKeys.change = true;
|
|
1209
1206
|
}
|
|
1210
1207
|
if (item.to.primaryKey) {
|
|
1211
1208
|
addPrimaryKeys.columns.push(
|
|
1212
|
-
item.to.column ? getColumnName(item.to.column, key, snakeCase) : snakeCase ?
|
|
1209
|
+
item.to.column ? getColumnName(item.to.column, key, snakeCase) : snakeCase ? pqb.toSnakeCase(key) : key
|
|
1213
1210
|
);
|
|
1214
1211
|
addPrimaryKeys.change = true;
|
|
1215
1212
|
}
|
|
@@ -1252,7 +1249,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1252
1249
|
`ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${using}`
|
|
1253
1250
|
);
|
|
1254
1251
|
}
|
|
1255
|
-
if (typeof from.identity !== typeof to.identity || !
|
|
1252
|
+
if (typeof from.identity !== typeof to.identity || !pqb.deepCompare(from.identity, to.identity)) {
|
|
1256
1253
|
if (from.identity) {
|
|
1257
1254
|
alterTable.push(`ALTER COLUMN "${name}" DROP IDENTITY`);
|
|
1258
1255
|
}
|
|
@@ -1312,7 +1309,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1312
1309
|
references: {
|
|
1313
1310
|
columns: [name],
|
|
1314
1311
|
...fromFkey,
|
|
1315
|
-
foreignColumns: snakeCase ? fromFkey.foreignColumns.map(
|
|
1312
|
+
foreignColumns: snakeCase ? fromFkey.foreignColumns.map(pqb.toSnakeCase) : fromFkey.foreignColumns
|
|
1316
1313
|
}
|
|
1317
1314
|
});
|
|
1318
1315
|
}
|
|
@@ -1323,7 +1320,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1323
1320
|
references: {
|
|
1324
1321
|
columns: [name],
|
|
1325
1322
|
...toFkey,
|
|
1326
|
-
foreignColumns: snakeCase ? toFkey.foreignColumns.map(
|
|
1323
|
+
foreignColumns: snakeCase ? toFkey.foreignColumns.map(pqb.toSnakeCase) : toFkey.foreignColumns
|
|
1327
1324
|
}
|
|
1328
1325
|
});
|
|
1329
1326
|
}
|
|
@@ -1343,7 +1340,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1343
1340
|
}
|
|
1344
1341
|
} else if (item.type === "rename") {
|
|
1345
1342
|
renameItems.push(
|
|
1346
|
-
snakeCase ? renameColumnSql(
|
|
1343
|
+
snakeCase ? renameColumnSql(pqb.toSnakeCase(key), pqb.toSnakeCase(item.name)) : renameColumnSql(key, item.name)
|
|
1347
1344
|
);
|
|
1348
1345
|
}
|
|
1349
1346
|
};
|
|
@@ -1352,7 +1349,7 @@ const pushIndexesOrExcludes = (key, from, to, name, add2, drop2) => {
|
|
|
1352
1349
|
for (let i = 0; i < len; i++) {
|
|
1353
1350
|
const fromItem = from[key]?.[i];
|
|
1354
1351
|
const toItem = to[key]?.[i];
|
|
1355
|
-
if ((fromItem || toItem) && (!fromItem || !toItem || !
|
|
1352
|
+
if ((fromItem || toItem) && (!fromItem || !toItem || !pqb.deepCompare(fromItem, toItem))) {
|
|
1356
1353
|
if (fromItem) {
|
|
1357
1354
|
drop2.push({
|
|
1358
1355
|
...fromItem,
|
|
@@ -1384,7 +1381,7 @@ const getChangeColumnName = (what, change, key, snakeCase) => {
|
|
|
1384
1381
|
return change.name || (change[what].column ? (
|
|
1385
1382
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1386
1383
|
getColumnName(change[what].column, key, snakeCase)
|
|
1387
|
-
) : snakeCase ?
|
|
1384
|
+
) : snakeCase ? pqb.toSnakeCase(key) : key);
|
|
1388
1385
|
};
|
|
1389
1386
|
const renameColumnSql = (from, to) => {
|
|
1390
1387
|
return `RENAME COLUMN "${from}" TO "${to}"`;
|
|
@@ -1427,7 +1424,7 @@ const astToQuery = (ast) => {
|
|
|
1427
1424
|
if (options?.with) {
|
|
1428
1425
|
const list = [];
|
|
1429
1426
|
if (options.with.checkOption)
|
|
1430
|
-
list.push(`check_option = ${
|
|
1427
|
+
list.push(`check_option = ${pqb.singleQuote(options.with.checkOption)}`);
|
|
1431
1428
|
if (options.with.securityBarrier) list.push(`security_barrier = true`);
|
|
1432
1429
|
if (options.with.securityInvoker) list.push(`security_invoker = true`);
|
|
1433
1430
|
sql.push(`WITH ( ${list.join(", ")} )`);
|
|
@@ -2218,7 +2215,7 @@ class Migration {
|
|
|
2218
2215
|
return createCollation(this, !this.up, name, options);
|
|
2219
2216
|
}
|
|
2220
2217
|
createView(name, ...args) {
|
|
2221
|
-
const [options, sql] = args.length === 2 ? args : [
|
|
2218
|
+
const [options, sql] = args.length === 2 ? args : [pqb.emptyObject, args[0]];
|
|
2222
2219
|
return createView(
|
|
2223
2220
|
this,
|
|
2224
2221
|
this.up,
|
|
@@ -2228,7 +2225,7 @@ class Migration {
|
|
|
2228
2225
|
);
|
|
2229
2226
|
}
|
|
2230
2227
|
dropView(name, ...args) {
|
|
2231
|
-
const [options, sql] = args.length === 2 ? args : [
|
|
2228
|
+
const [options, sql] = args.length === 2 ? args : [pqb.emptyObject, args[0]];
|
|
2232
2229
|
return createView(
|
|
2233
2230
|
this,
|
|
2234
2231
|
!this.up,
|
|
@@ -2281,7 +2278,7 @@ class Migration {
|
|
|
2281
2278
|
text: `SELECT 1 FROM "information_schema"."columns" WHERE "table_name" = $1 AND "column_name" = $2`,
|
|
2282
2279
|
values: [
|
|
2283
2280
|
tableName,
|
|
2284
|
-
this.options.snakeCase ?
|
|
2281
|
+
this.options.snakeCase ? pqb.toSnakeCase(columnName) : columnName
|
|
2285
2282
|
]
|
|
2286
2283
|
});
|
|
2287
2284
|
}
|
|
@@ -2512,7 +2509,7 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
|
|
|
2512
2509
|
await Promise.all(
|
|
2513
2510
|
(ast.place === "after" ? [...ast.values].reverse() : ast.values).map(
|
|
2514
2511
|
(value) => migration.adapter.query(
|
|
2515
|
-
`ALTER TYPE ${quoteTable(ast.schema, ast.name)} ADD VALUE${ast.ifNotExists ? " IF NOT EXISTS" : ""} ${
|
|
2512
|
+
`ALTER TYPE ${quoteTable(ast.schema, ast.name)} ADD VALUE${ast.ifNotExists ? " IF NOT EXISTS" : ""} ${pqb.singleQuote(value)}${ast.place && ast.relativeTo ? ` ${ast.place.toUpperCase()} ${pqb.singleQuote(ast.relativeTo)}` : ""}`
|
|
2516
2513
|
)
|
|
2517
2514
|
)
|
|
2518
2515
|
);
|
|
@@ -2526,7 +2523,7 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
|
|
|
2526
2523
|
migration,
|
|
2527
2524
|
ast,
|
|
2528
2525
|
existingValues.filter((v) => !ast.values.includes(v)),
|
|
2529
|
-
(quotedName2, table, column) => `Cannot drop ${quotedName2} enum values [${ast.values.map(
|
|
2526
|
+
(quotedName2, table, column) => `Cannot drop ${quotedName2} enum values [${ast.values.map(pqb.singleQuote).join(
|
|
2530
2527
|
", "
|
|
2531
2528
|
)}]: table ${table} has a row with such value in the column "${column}"`
|
|
2532
2529
|
);
|
|
@@ -2549,7 +2546,7 @@ const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
|
|
|
2549
2546
|
migration,
|
|
2550
2547
|
ast,
|
|
2551
2548
|
ast.toValues,
|
|
2552
|
-
(quotedName, table, column) => `Cannot change ${quotedName} enum values from [${fromValues.map(
|
|
2549
|
+
(quotedName, table, column) => `Cannot change ${quotedName} enum values from [${fromValues.map(pqb.singleQuote).join(", ")}] to [${toValues.map(pqb.singleQuote).join(
|
|
2553
2550
|
", "
|
|
2554
2551
|
)}]: table ${table} has a row with removed value in the column "${column}"`
|
|
2555
2552
|
);
|
|
@@ -2567,10 +2564,10 @@ const recreateEnum = async (migration, { schema, name }, values, errorMessage) =
|
|
|
2567
2564
|
) AS "columns"
|
|
2568
2565
|
FROM pg_class c
|
|
2569
2566
|
JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
|
|
2570
|
-
JOIN pg_type bt ON bt.typname = ${
|
|
2567
|
+
JOIN pg_type bt ON bt.typname = ${pqb.singleQuote(name)}
|
|
2571
2568
|
JOIN pg_type t ON t.oid = bt.oid OR t.typelem = bt.oid
|
|
2572
2569
|
JOIN pg_attribute a ON a.attrelid = c.oid AND a.atttypid = t.oid
|
|
2573
|
-
JOIN pg_namespace tn ON tn.oid = t.typnamespace AND tn.nspname = ${
|
|
2570
|
+
JOIN pg_namespace tn ON tn.oid = t.typnamespace AND tn.nspname = ${pqb.singleQuote(
|
|
2574
2571
|
schema ?? defaultSchema
|
|
2575
2572
|
)}
|
|
2576
2573
|
WHERE c.relkind IN (${relKinds.map((c) => `'${c}'`).join(", ")})
|
|
@@ -2584,7 +2581,7 @@ GROUP BY n.nspname, c.relname`
|
|
|
2584
2581
|
);
|
|
2585
2582
|
sql.push(
|
|
2586
2583
|
`DROP TYPE ${quotedName}`,
|
|
2587
|
-
`CREATE TYPE ${quotedName} AS ENUM (${values.map(
|
|
2584
|
+
`CREATE TYPE ${quotedName} AS ENUM (${values.map(pqb.singleQuote).join(", ")})`
|
|
2588
2585
|
);
|
|
2589
2586
|
await migration.adapter.query(sql.join(";\n"));
|
|
2590
2587
|
for (const t of tables) {
|
|
@@ -2614,7 +2611,7 @@ const writeMigrationFile = async (config, version, name, migrationCode) => {
|
|
|
2614
2611
|
config.migrationsPath,
|
|
2615
2612
|
`${version}_${name.replaceAll(" ", "-")}.ts`
|
|
2616
2613
|
);
|
|
2617
|
-
const importPath =
|
|
2614
|
+
const importPath = pqb.getImportPath(
|
|
2618
2615
|
filePath,
|
|
2619
2616
|
path.join(config.basePath, config.dbScript)
|
|
2620
2617
|
);
|
|
@@ -2623,7 +2620,7 @@ const writeMigrationFile = async (config, version, name, migrationCode) => {
|
|
|
2623
2620
|
`import { change } from '${importPath}';
|
|
2624
2621
|
${migrationCode}`
|
|
2625
2622
|
);
|
|
2626
|
-
config.logger?.log(`Created ${
|
|
2623
|
+
config.logger?.log(`Created ${pqb.pathToLog(filePath)}`);
|
|
2627
2624
|
};
|
|
2628
2625
|
const newMigration = async (config, [name]) => {
|
|
2629
2626
|
if (!name) throw new Error("Migration name is missing");
|
|
@@ -3234,11 +3231,11 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3234
3231
|
sequence.pop();
|
|
3235
3232
|
}
|
|
3236
3233
|
config.logger?.log(
|
|
3237
|
-
`${up ? "Migrated" : "Rolled back"} ${
|
|
3234
|
+
`${up ? "Migrated" : "Rolled back"} ${pqb.pathToLog(file.path)}
|
|
3238
3235
|
`
|
|
3239
3236
|
);
|
|
3240
3237
|
}
|
|
3241
|
-
migrations ?? (migrations =
|
|
3238
|
+
migrations ?? (migrations = pqb.emptyArray);
|
|
3242
3239
|
const afterMigrate = config[up ? "afterMigrate" : "afterRollback"];
|
|
3243
3240
|
if (config.afterChange || afterMigrate) {
|
|
3244
3241
|
db ?? (db = getDb(trx));
|
|
@@ -3290,7 +3287,7 @@ const getChanges = async (file, config) => {
|
|
|
3290
3287
|
let changes = file.path ? changeCache[file.path] : void 0;
|
|
3291
3288
|
if (!changes) {
|
|
3292
3289
|
const module = await file.load();
|
|
3293
|
-
const exported = module?.default &&
|
|
3290
|
+
const exported = module?.default && pqb.toArray(module.default);
|
|
3294
3291
|
if (config?.forceDefaultExports && !exported) {
|
|
3295
3292
|
throw new RakeDbError(
|
|
3296
3293
|
`Missing a default export in ${file.path} migration`
|
|
@@ -3322,18 +3319,6 @@ const changeMigratedVersion = async (adapter, up, file, config) => {
|
|
|
3322
3319
|
);
|
|
3323
3320
|
};
|
|
3324
3321
|
|
|
3325
|
-
const colors = {
|
|
3326
|
-
yellow: (s) => `\x1B[33m${s}\x1B[0m`,
|
|
3327
|
-
green: (s) => `\x1B[32m${s}\x1B[0m`,
|
|
3328
|
-
red: (s) => `\x1B[31m${s}\x1B[0m`,
|
|
3329
|
-
blue: (s) => `\x1B[34m${s}\x1B[0m`,
|
|
3330
|
-
bright: (s) => `\x1B[1m${s}\x1B[0m`,
|
|
3331
|
-
blueBold: (s) => `\x1B[1m\x1B[34m${s}\x1B[0m`,
|
|
3332
|
-
yellowBold: (s) => `\x1B[1m\x1B[33m${s}\x1B[0m`,
|
|
3333
|
-
greenBold: (s) => `\x1B[1m\x1B[32m${s}\x1B[0m`,
|
|
3334
|
-
pale: (s) => `\x1B[2m${s}\x1B[0m`
|
|
3335
|
-
};
|
|
3336
|
-
|
|
3337
3322
|
const ESC = "\x1B";
|
|
3338
3323
|
const CSI = `${ESC}[`;
|
|
3339
3324
|
const cursorShow = `${CSI}?25h`;
|
|
@@ -3375,7 +3360,7 @@ const prompt = async ({
|
|
|
3375
3360
|
value,
|
|
3376
3361
|
submitted: false,
|
|
3377
3362
|
render() {
|
|
3378
|
-
let text = (ctx.submitted ? colors.greenBold("\u2714") : colors.yellowBold("?")) + " " + render(ctx);
|
|
3363
|
+
let text = (ctx.submitted ? pqb.colors.greenBold("\u2714") : pqb.colors.yellowBold("?")) + " " + render(ctx);
|
|
3379
3364
|
if (ctx.submitted) text += "\n";
|
|
3380
3365
|
stdout.write(prevText ? clear(prevText) + "\r" + text : text);
|
|
3381
3366
|
prevText = text;
|
|
@@ -3410,7 +3395,7 @@ const prompt = async ({
|
|
|
3410
3395
|
ctx.render();
|
|
3411
3396
|
});
|
|
3412
3397
|
};
|
|
3413
|
-
const defaultActive = (s) => `${colors.blueBold("\u276F")} ${s}`;
|
|
3398
|
+
const defaultActive = (s) => `${pqb.colors.blueBold("\u276F")} ${s}`;
|
|
3414
3399
|
const defaultInactive = (s) => ` ${s}`;
|
|
3415
3400
|
const promptSelect = ({
|
|
3416
3401
|
message,
|
|
@@ -3420,7 +3405,7 @@ const promptSelect = ({
|
|
|
3420
3405
|
}) => prompt({
|
|
3421
3406
|
value: 0,
|
|
3422
3407
|
render(ctx) {
|
|
3423
|
-
let text = `${message} ${colors.pale(
|
|
3408
|
+
let text = `${message} ${pqb.colors.pale(
|
|
3424
3409
|
"Use arrows or jk. Press enter to submit."
|
|
3425
3410
|
)}
|
|
3426
3411
|
`;
|
|
@@ -3439,8 +3424,8 @@ const promptConfirm = ({
|
|
|
3439
3424
|
}) => prompt({
|
|
3440
3425
|
value: true,
|
|
3441
3426
|
render(ctx) {
|
|
3442
|
-
return `${colors.bright(message)}
|
|
3443
|
-
${ctx.submitted ? `> ${ctx.value ? colors.greenBold("Yes") : colors.yellowBold("No")}` : colors.pale(`> (Y/n)`)}
|
|
3427
|
+
return `${pqb.colors.bright(message)}
|
|
3428
|
+
${ctx.submitted ? `> ${ctx.value ? pqb.colors.greenBold("Yes") : pqb.colors.yellowBold("No")}` : pqb.colors.pale(`> (Y/n)`)}
|
|
3444
3429
|
`;
|
|
3445
3430
|
},
|
|
3446
3431
|
onKeyPress(ctx, s) {
|
|
@@ -3466,8 +3451,8 @@ const promptText = ({
|
|
|
3466
3451
|
cursor: true,
|
|
3467
3452
|
validate: (ctx) => !min || ctx.value.length >= min,
|
|
3468
3453
|
render(ctx) {
|
|
3469
|
-
let text = `${colors.bright(message)}
|
|
3470
|
-
> ${ctx.submitted ? renderValue(ctx) : showDefault ? colors.pale(def) + "\b".repeat(def.length) : ctx.value}`;
|
|
3454
|
+
let text = `${pqb.colors.bright(message)}
|
|
3455
|
+
> ${ctx.submitted ? renderValue(ctx) : showDefault ? pqb.colors.pale(def) + "\b".repeat(def.length) : ctx.value}`;
|
|
3471
3456
|
if (ctx.submitted) text += "\n";
|
|
3472
3457
|
return text;
|
|
3473
3458
|
},
|
|
@@ -4271,7 +4256,7 @@ const getDbColumnIsSerial = (item) => {
|
|
|
4271
4256
|
if (item.type === "int2" || item.type === "int4" || item.type === "int8") {
|
|
4272
4257
|
const { default: def, schemaName, tableName, name } = item;
|
|
4273
4258
|
const seq = `${tableName}_${name}_seq`;
|
|
4274
|
-
if (def && (def === `nextval(${
|
|
4259
|
+
if (def && (def === `nextval(${pqb.singleQuote(`${seq}`)}::regclass)` || def === `nextval(${pqb.singleQuote(`"${seq}"`)}::regclass)` || def === `nextval(${pqb.singleQuote(`${schemaName}.${seq}`)}::regclass)` || def === `nextval(${pqb.singleQuote(`"${schemaName}".${seq}`)}::regclass)` || def === `nextval(${pqb.singleQuote(`${schemaName}."${seq}"`)}::regclass)` || def === `nextval(${pqb.singleQuote(`"${schemaName}"."${seq}"`)}::regclass)`)) {
|
|
4275
4260
|
return true;
|
|
4276
4261
|
}
|
|
4277
4262
|
}
|
|
@@ -4356,7 +4341,7 @@ const tableToAst = (ctx, data, table, action, domains) => {
|
|
|
4356
4341
|
name: tableName,
|
|
4357
4342
|
shape: makeDbStructureColumnsShape(ctx, data, domains, table, tableData),
|
|
4358
4343
|
noPrimaryKey: tableData.primaryKey ? "error" : "ignore",
|
|
4359
|
-
primaryKey: primaryKey && primaryKey.columns.length > 1 ? { ...primaryKey, columns: primaryKey.columns.map(
|
|
4344
|
+
primaryKey: primaryKey && primaryKey.columns.length > 1 ? { ...primaryKey, columns: primaryKey.columns.map(pqb.toCamelCase) } : void 0,
|
|
4360
4345
|
indexes: indexesOrExcludesToAst(
|
|
4361
4346
|
tableName,
|
|
4362
4347
|
tableData,
|
|
@@ -4382,14 +4367,14 @@ const indexesOrExcludesToAst = (tableName, tableData, key) => {
|
|
|
4382
4367
|
acc.push({
|
|
4383
4368
|
columns: item.columns.map((it, i) => ({
|
|
4384
4369
|
with: "exclude" in item && item.exclude ? item.exclude[i] : void 0,
|
|
4385
|
-
..."expression" in it ? { expression: it.expression } : { column:
|
|
4370
|
+
..."expression" in it ? { expression: it.expression } : { column: pqb.toCamelCase(it.column) },
|
|
4386
4371
|
collate: it.collate,
|
|
4387
4372
|
opclass: it.opclass,
|
|
4388
4373
|
order: it.order
|
|
4389
4374
|
})),
|
|
4390
4375
|
options: {
|
|
4391
4376
|
...options,
|
|
4392
|
-
include: item.include?.map(
|
|
4377
|
+
include: item.include?.map(pqb.toCamelCase)
|
|
4393
4378
|
}
|
|
4394
4379
|
});
|
|
4395
4380
|
}
|
|
@@ -4455,7 +4440,7 @@ const viewToAst = (ctx, data, domains, view) => {
|
|
|
4455
4440
|
options.with = withOptions;
|
|
4456
4441
|
for (const pair of view.with) {
|
|
4457
4442
|
const [key, value] = pair.split("=");
|
|
4458
|
-
withOptions[
|
|
4443
|
+
withOptions[pqb.toCamelCase(key)] = value === "true" ? true : value === "false" ? false : value;
|
|
4459
4444
|
}
|
|
4460
4445
|
}
|
|
4461
4446
|
return {
|
|
@@ -4531,9 +4516,9 @@ const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, ch
|
|
|
4531
4516
|
sql: new pqb.RawSQL([[check]])
|
|
4532
4517
|
}));
|
|
4533
4518
|
}
|
|
4534
|
-
const camelCaseName =
|
|
4519
|
+
const camelCaseName = pqb.toCamelCase(item.name);
|
|
4535
4520
|
if (ctx.snakeCase) {
|
|
4536
|
-
const snakeCaseName =
|
|
4521
|
+
const snakeCaseName = pqb.toSnakeCase(camelCaseName);
|
|
4537
4522
|
if (snakeCaseName !== item.name) column.data.name = item.name;
|
|
4538
4523
|
} else if (camelCaseName !== item.name) {
|
|
4539
4524
|
column.data.name = item.name;
|
|
@@ -4622,10 +4607,6 @@ const checkIfIsOuterRecursiveFkey = (data, table, references) => {
|
|
|
4622
4607
|
return false;
|
|
4623
4608
|
};
|
|
4624
4609
|
|
|
4625
|
-
const exhaustive = (_) => {
|
|
4626
|
-
throw new Error("Condition was not exhaustive");
|
|
4627
|
-
};
|
|
4628
|
-
|
|
4629
4610
|
const astToGenerateItems = (config, asts, currentSchema) => {
|
|
4630
4611
|
return asts.map((ast) => astToGenerateItem(config, ast, currentSchema));
|
|
4631
4612
|
};
|
|
@@ -4684,7 +4665,7 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4684
4665
|
deps.push(table);
|
|
4685
4666
|
const columns = [];
|
|
4686
4667
|
for (const name in ast.shape) {
|
|
4687
|
-
const arr =
|
|
4668
|
+
const arr = pqb.toArray(ast.shape[name]);
|
|
4688
4669
|
for (const item of arr) {
|
|
4689
4670
|
if (item.type === "add") {
|
|
4690
4671
|
columns.push([add, name, { column: item.item }]);
|
|
@@ -4780,7 +4761,7 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4780
4761
|
break;
|
|
4781
4762
|
}
|
|
4782
4763
|
default:
|
|
4783
|
-
exhaustive();
|
|
4764
|
+
pqb.exhaustive(ast);
|
|
4784
4765
|
}
|
|
4785
4766
|
return {
|
|
4786
4767
|
ast,
|
|
@@ -4986,7 +4967,7 @@ const astToMigration = (currentSchema, config, asts) => {
|
|
|
4986
4967
|
code += `
|
|
4987
4968
|
change(async (db) => {
|
|
4988
4969
|
${group.map(
|
|
4989
|
-
(ast) =>
|
|
4970
|
+
(ast) => pqb.codeToString(
|
|
4990
4971
|
astEncoders[ast.type](ast, config, currentSchema),
|
|
4991
4972
|
" ",
|
|
4992
4973
|
" "
|
|
@@ -5007,7 +4988,7 @@ const astEncoders = {
|
|
|
5007
4988
|
);
|
|
5008
4989
|
const isShifted = hasOptions || hasTableData;
|
|
5009
4990
|
if (isShifted) {
|
|
5010
|
-
|
|
4991
|
+
pqb.addCode(code, `await db.${ast.action}Table(`);
|
|
5011
4992
|
const inner = [`${quoteSchemaTable(ast)},`];
|
|
5012
4993
|
code.push(inner);
|
|
5013
4994
|
code = inner;
|
|
@@ -5020,7 +5001,7 @@ const astEncoders = {
|
|
|
5020
5001
|
}
|
|
5021
5002
|
code.push("(t) => ({");
|
|
5022
5003
|
} else {
|
|
5023
|
-
|
|
5004
|
+
pqb.addCode(
|
|
5024
5005
|
code,
|
|
5025
5006
|
`await db.${ast.action}Table(${quoteSchemaTable(ast)}, (t) => ({`
|
|
5026
5007
|
);
|
|
@@ -5039,23 +5020,23 @@ const astEncoders = {
|
|
|
5039
5020
|
for (const key in ast.shape) {
|
|
5040
5021
|
if (timestamps.hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
|
|
5041
5022
|
continue;
|
|
5042
|
-
const line = [`${
|
|
5023
|
+
const line = [`${pqb.quoteObjectKey(key, config.snakeCase)}: `];
|
|
5043
5024
|
const columnCode = ast.shape[key].toCode(toCodeCtx, key);
|
|
5044
5025
|
for (const part of columnCode) {
|
|
5045
|
-
|
|
5026
|
+
pqb.addCode(line, part);
|
|
5046
5027
|
}
|
|
5047
|
-
|
|
5028
|
+
pqb.addCode(line, ",");
|
|
5048
5029
|
code.push(line);
|
|
5049
5030
|
}
|
|
5050
5031
|
if (timestamps.hasAnyTimestamps) {
|
|
5051
5032
|
code.push([`...${timestampsToCode(timestamps)},`]);
|
|
5052
5033
|
}
|
|
5053
5034
|
if (isShifted) {
|
|
5054
|
-
|
|
5035
|
+
pqb.addCode(code, "}),");
|
|
5055
5036
|
if (hasTableData) pqb.pushTableDataCode(code, ast);
|
|
5056
|
-
|
|
5037
|
+
pqb.addCode(result, ");");
|
|
5057
5038
|
} else {
|
|
5058
|
-
|
|
5039
|
+
pqb.addCode(result, "}));");
|
|
5059
5040
|
}
|
|
5060
5041
|
return result;
|
|
5061
5042
|
},
|
|
@@ -5068,7 +5049,7 @@ const astEncoders = {
|
|
|
5068
5049
|
});
|
|
5069
5050
|
const { comment } = ast;
|
|
5070
5051
|
if (comment !== void 0) {
|
|
5071
|
-
|
|
5052
|
+
pqb.addCode(code, `await db.changeTable(`);
|
|
5072
5053
|
const inner = [
|
|
5073
5054
|
`${schemaTable},`,
|
|
5074
5055
|
`{ comment: ${JSON.stringify(ast.comment)} },`,
|
|
@@ -5077,7 +5058,7 @@ const astEncoders = {
|
|
|
5077
5058
|
code.push(inner);
|
|
5078
5059
|
code = inner;
|
|
5079
5060
|
} else {
|
|
5080
|
-
|
|
5061
|
+
pqb.addCode(code, `await db.changeTable(${schemaTable}, (t) => ({`);
|
|
5081
5062
|
}
|
|
5082
5063
|
const [addTimestamps, dropTimestamps] = ["add", "drop"].map(
|
|
5083
5064
|
(type) => getHasTimestamps(
|
|
@@ -5093,29 +5074,29 @@ const astEncoders = {
|
|
|
5093
5074
|
snakeCase: config.snakeCase
|
|
5094
5075
|
};
|
|
5095
5076
|
for (const key in ast.shape) {
|
|
5096
|
-
const changes =
|
|
5077
|
+
const changes = pqb.toArray(ast.shape[key]);
|
|
5097
5078
|
for (const change of changes) {
|
|
5098
5079
|
if (change.type === "add" || change.type === "drop") {
|
|
5099
5080
|
if ((addTimestamps.hasAnyTimestamps || dropTimestamps.hasAnyTimestamps) && (key === "createdAt" || key === "updatedAt"))
|
|
5100
5081
|
continue;
|
|
5101
5082
|
const recreate = changes.length > 1;
|
|
5102
5083
|
const line = [
|
|
5103
|
-
recreate ? `...t.${change.type}(t.name(${
|
|
5084
|
+
recreate ? `...t.${change.type}(t.name(${pqb.singleQuote(
|
|
5104
5085
|
change.item.data.name ?? key
|
|
5105
|
-
)})` : `${
|
|
5086
|
+
)})` : `${pqb.quoteObjectKey(key, config.snakeCase)}: t.${change.type}(`
|
|
5106
5087
|
];
|
|
5107
5088
|
const columnCode = change.item.toCode(toCodeCtx, key);
|
|
5108
5089
|
for (let i = 0; i < columnCode.length; i++) {
|
|
5109
5090
|
let part = columnCode[i];
|
|
5110
5091
|
if (recreate && !i) part = part.slice(1);
|
|
5111
|
-
|
|
5092
|
+
pqb.addCode(line, part);
|
|
5112
5093
|
}
|
|
5113
|
-
|
|
5094
|
+
pqb.addCode(line, "),");
|
|
5114
5095
|
code.push(line);
|
|
5115
5096
|
} else if (change.type === "change") {
|
|
5116
5097
|
if (!change.from.column || !change.to.column) continue;
|
|
5117
5098
|
const line = [
|
|
5118
|
-
`${
|
|
5099
|
+
`${pqb.quoteObjectKey(key, config.snakeCase)}: t${change.name ? `.name(${pqb.singleQuote(change.name)})` : ""}.change(`
|
|
5119
5100
|
];
|
|
5120
5101
|
const fromCode = change.from.column.toCode(
|
|
5121
5102
|
{
|
|
@@ -5128,15 +5109,15 @@ const astEncoders = {
|
|
|
5128
5109
|
key
|
|
5129
5110
|
);
|
|
5130
5111
|
for (const part of fromCode) {
|
|
5131
|
-
|
|
5112
|
+
pqb.addCode(line, part);
|
|
5132
5113
|
}
|
|
5133
|
-
|
|
5114
|
+
pqb.addCode(line, ", ");
|
|
5134
5115
|
const toCode = change.to.column.toCode(toCodeCtx, key);
|
|
5135
5116
|
for (const part of toCode) {
|
|
5136
|
-
|
|
5117
|
+
pqb.addCode(line, part);
|
|
5137
5118
|
}
|
|
5138
5119
|
if (change.using) {
|
|
5139
|
-
|
|
5120
|
+
pqb.addCode(line, ", {");
|
|
5140
5121
|
const u = [];
|
|
5141
5122
|
if (change.using.usingUp) {
|
|
5142
5123
|
u.push(`usingUp: ${change.using.usingUp.toCode("t")},`);
|
|
@@ -5144,46 +5125,46 @@ const astEncoders = {
|
|
|
5144
5125
|
if (change.using.usingDown) {
|
|
5145
5126
|
u.push(`usingDown: ${change.using.usingDown.toCode("t")},`);
|
|
5146
5127
|
}
|
|
5147
|
-
|
|
5148
|
-
|
|
5128
|
+
pqb.addCode(line, u);
|
|
5129
|
+
pqb.addCode(line, "}");
|
|
5149
5130
|
}
|
|
5150
|
-
|
|
5131
|
+
pqb.addCode(line, "),");
|
|
5151
5132
|
code.push(line);
|
|
5152
5133
|
} else if (change.type === "rename") {
|
|
5153
5134
|
code.push([
|
|
5154
|
-
`${
|
|
5135
|
+
`${pqb.quoteObjectKey(key, config.snakeCase)}: t.rename(${pqb.singleQuote(
|
|
5155
5136
|
change.name
|
|
5156
5137
|
)}),`
|
|
5157
5138
|
]);
|
|
5158
5139
|
} else {
|
|
5159
|
-
exhaustive(change.type);
|
|
5140
|
+
pqb.exhaustive(change.type);
|
|
5160
5141
|
}
|
|
5161
5142
|
}
|
|
5162
5143
|
}
|
|
5163
5144
|
for (const key of ["drop", "add"]) {
|
|
5164
5145
|
const timestamps = key === "add" ? addTimestamps : dropTimestamps;
|
|
5165
5146
|
if (timestamps.hasAnyTimestamps) {
|
|
5166
|
-
|
|
5147
|
+
pqb.addCode(code, [`...t.${key}(${timestampsToCode(timestamps)}),`]);
|
|
5167
5148
|
}
|
|
5168
5149
|
const { primaryKey, indexes, excludes, constraints } = ast[key];
|
|
5169
5150
|
if (primaryKey) {
|
|
5170
|
-
|
|
5151
|
+
pqb.addCode(code, [
|
|
5171
5152
|
`...t.${key}(${pqb.primaryKeyInnerToCode(primaryKey, "t")}),`
|
|
5172
5153
|
]);
|
|
5173
5154
|
}
|
|
5174
5155
|
if (indexes) {
|
|
5175
5156
|
for (const item of indexes) {
|
|
5176
|
-
|
|
5157
|
+
pqb.addCode(code, [`...t.${key}(`, pqb.indexInnerToCode(item, "t"), "),"]);
|
|
5177
5158
|
}
|
|
5178
5159
|
}
|
|
5179
5160
|
if (excludes) {
|
|
5180
5161
|
for (const item of excludes) {
|
|
5181
|
-
|
|
5162
|
+
pqb.addCode(code, [`...t.${key}(`, pqb.excludeInnerToCode(item, "t"), "),"]);
|
|
5182
5163
|
}
|
|
5183
5164
|
}
|
|
5184
5165
|
if (constraints) {
|
|
5185
5166
|
for (const item of constraints) {
|
|
5186
|
-
|
|
5167
|
+
pqb.addCode(code, [
|
|
5187
5168
|
`...t.${key}(`,
|
|
5188
5169
|
pqb.constraintInnerToCode(item, "t", true),
|
|
5189
5170
|
"),"
|
|
@@ -5192,10 +5173,10 @@ const astEncoders = {
|
|
|
5192
5173
|
}
|
|
5193
5174
|
}
|
|
5194
5175
|
if (ast.comment !== void 0) {
|
|
5195
|
-
|
|
5196
|
-
|
|
5176
|
+
pqb.addCode(code, "}),");
|
|
5177
|
+
pqb.addCode(result, ");");
|
|
5197
5178
|
} else {
|
|
5198
|
-
|
|
5179
|
+
pqb.addCode(result, "}));");
|
|
5199
5180
|
}
|
|
5200
5181
|
return result;
|
|
5201
5182
|
},
|
|
@@ -5203,14 +5184,14 @@ const astEncoders = {
|
|
|
5203
5184
|
const code = [];
|
|
5204
5185
|
const kind = ast.kind === "TABLE" ? "Table" : "Type";
|
|
5205
5186
|
if (ast.from === ast.to) {
|
|
5206
|
-
|
|
5187
|
+
pqb.addCode(
|
|
5207
5188
|
code,
|
|
5208
|
-
`await db.change${kind}Schema(${
|
|
5189
|
+
`await db.change${kind}Schema(${pqb.singleQuote(ast.to)}, ${pqb.singleQuote(
|
|
5209
5190
|
ast.fromSchema ?? currentSchema
|
|
5210
|
-
)}, ${
|
|
5191
|
+
)}, ${pqb.singleQuote(ast.toSchema ?? currentSchema)});`
|
|
5211
5192
|
);
|
|
5212
5193
|
} else {
|
|
5213
|
-
|
|
5194
|
+
pqb.addCode(
|
|
5214
5195
|
code,
|
|
5215
5196
|
`await db.rename${kind}(${quoteSchemaTable({
|
|
5216
5197
|
schema: ast.fromSchema === currentSchema ? void 0 : ast.fromSchema,
|
|
@@ -5224,10 +5205,10 @@ const astEncoders = {
|
|
|
5224
5205
|
return code;
|
|
5225
5206
|
},
|
|
5226
5207
|
schema(ast) {
|
|
5227
|
-
return `await db.${ast.action === "create" ? "createSchema" : "dropSchema"}(${
|
|
5208
|
+
return `await db.${ast.action === "create" ? "createSchema" : "dropSchema"}(${pqb.singleQuote(ast.name)});`;
|
|
5228
5209
|
},
|
|
5229
5210
|
renameSchema(ast) {
|
|
5230
|
-
return `await db.renameSchema(${
|
|
5211
|
+
return `await db.renameSchema(${pqb.singleQuote(ast.from)}, ${pqb.singleQuote(
|
|
5231
5212
|
ast.to
|
|
5232
5213
|
)});`;
|
|
5233
5214
|
},
|
|
@@ -5236,31 +5217,31 @@ const astEncoders = {
|
|
|
5236
5217
|
`await db.${ast.action}Extension(${quoteSchemaTable(ast)}`
|
|
5237
5218
|
];
|
|
5238
5219
|
if (ast.version) {
|
|
5239
|
-
|
|
5240
|
-
code.push([`version: ${
|
|
5220
|
+
pqb.addCode(code, ", {");
|
|
5221
|
+
code.push([`version: ${pqb.singleQuote(ast.version)},`], "}");
|
|
5241
5222
|
}
|
|
5242
|
-
|
|
5223
|
+
pqb.addCode(code, ");");
|
|
5243
5224
|
return code;
|
|
5244
5225
|
},
|
|
5245
5226
|
enum(ast) {
|
|
5246
|
-
return `await db.${ast.action === "create" ? "createEnum" : "dropEnum"}(${quoteSchemaTable(ast)}, [${ast.values.map(
|
|
5227
|
+
return `await db.${ast.action === "create" ? "createEnum" : "dropEnum"}(${quoteSchemaTable(ast)}, [${ast.values.map(pqb.singleQuote).join(", ")}]);`;
|
|
5247
5228
|
},
|
|
5248
5229
|
enumValues(ast) {
|
|
5249
5230
|
return `await db.${ast.action}EnumValues(${quoteSchemaTable(
|
|
5250
5231
|
ast
|
|
5251
|
-
)}, [${ast.values.map(
|
|
5232
|
+
)}, [${ast.values.map(pqb.singleQuote).join(", ")}]);`;
|
|
5252
5233
|
},
|
|
5253
5234
|
renameEnumValues(ast, config) {
|
|
5254
5235
|
return `await db.renameEnumValues(${quoteSchemaTable(
|
|
5255
5236
|
ast
|
|
5256
5237
|
)}, { ${Object.entries(ast.values).map(
|
|
5257
|
-
([from, to]) => `${
|
|
5238
|
+
([from, to]) => `${pqb.quoteObjectKey(from, config.snakeCase)}: ${pqb.singleQuote(to)}`
|
|
5258
5239
|
).join(", ")} });`;
|
|
5259
5240
|
},
|
|
5260
5241
|
changeEnumValues(ast) {
|
|
5261
5242
|
return `await db.changeEnumValues(${quoteSchemaTable(
|
|
5262
5243
|
ast
|
|
5263
|
-
)}, [${ast.fromValues.map(
|
|
5244
|
+
)}, [${ast.fromValues.map(pqb.singleQuote).join(", ")}], [${ast.toValues.map(pqb.singleQuote).join(", ")}]);`;
|
|
5264
5245
|
},
|
|
5265
5246
|
domain(ast, _, currentSchema) {
|
|
5266
5247
|
return `await db.${ast.action}Domain(${quoteSchemaTable(
|
|
@@ -5298,7 +5279,7 @@ const astEncoders = {
|
|
|
5298
5279
|
}
|
|
5299
5280
|
const check = ast.check;
|
|
5300
5281
|
return [
|
|
5301
|
-
`await db.addCheck(${table}, ${check.toCode("t")}${ast.name ? `, ${
|
|
5282
|
+
`await db.addCheck(${table}, ${check.toCode("t")}${ast.name ? `, ${pqb.singleQuote(ast.name)}` : ""});`
|
|
5302
5283
|
];
|
|
5303
5284
|
},
|
|
5304
5285
|
renameTableItem(ast) {
|
|
@@ -5306,7 +5287,7 @@ const astEncoders = {
|
|
|
5306
5287
|
`await db.rename${ast.kind === "INDEX" ? "Index" : "Constraint"}(${quoteSchemaTable({
|
|
5307
5288
|
schema: ast.tableSchema,
|
|
5308
5289
|
name: ast.tableName
|
|
5309
|
-
})}, ${
|
|
5290
|
+
})}, ${pqb.singleQuote(ast.from)}, ${pqb.singleQuote(ast.to)});`
|
|
5310
5291
|
];
|
|
5311
5292
|
},
|
|
5312
5293
|
view(ast) {
|
|
@@ -5320,10 +5301,10 @@ const astEncoders = {
|
|
|
5320
5301
|
if (w?.securityInvoker)
|
|
5321
5302
|
options.push(`securityInvoker: ${w.securityInvoker},`);
|
|
5322
5303
|
if (options.length) {
|
|
5323
|
-
|
|
5304
|
+
pqb.addCode(code, ", {");
|
|
5324
5305
|
code.push(options, "}");
|
|
5325
5306
|
}
|
|
5326
|
-
|
|
5307
|
+
pqb.addCode(code, ", ");
|
|
5327
5308
|
if (!ast.sql._values) {
|
|
5328
5309
|
const raw = ast.sql._sql;
|
|
5329
5310
|
let sql;
|
|
@@ -5338,11 +5319,11 @@ const astEncoders = {
|
|
|
5338
5319
|
}
|
|
5339
5320
|
sql += parts[last];
|
|
5340
5321
|
}
|
|
5341
|
-
|
|
5322
|
+
pqb.addCode(code, pqb.backtickQuote(sql));
|
|
5342
5323
|
} else {
|
|
5343
|
-
|
|
5324
|
+
pqb.addCode(code, ast.sql.toCode("db"));
|
|
5344
5325
|
}
|
|
5345
|
-
|
|
5326
|
+
pqb.addCode(code, ");");
|
|
5346
5327
|
return code;
|
|
5347
5328
|
}
|
|
5348
5329
|
};
|
|
@@ -5350,7 +5331,7 @@ const isTimestamp = (column, type) => {
|
|
|
5350
5331
|
if (!column) return false;
|
|
5351
5332
|
const { default: def } = column.data;
|
|
5352
5333
|
return Boolean(
|
|
5353
|
-
column instanceof type && !column.data.isNullable && def && typeof def === "object" &&
|
|
5334
|
+
column instanceof type && !column.data.isNullable && def && typeof def === "object" && pqb.isRawSQL(def) && (typeof def._sql === "object" ? def._sql[0][0] : def._sql) === "now()"
|
|
5354
5335
|
);
|
|
5355
5336
|
};
|
|
5356
5337
|
const getHasTimestamps = (createdAt, updatedAt) => {
|
|
@@ -5459,7 +5440,7 @@ const rebase = async (adapters, config) => {
|
|
|
5459
5440
|
const result = await promptSelect({
|
|
5460
5441
|
message: "Which should go first?",
|
|
5461
5442
|
options: [prev.name, file.name],
|
|
5462
|
-
active: (s) => `${colors.yellow("\u276F")} ${colors.yellow(s)}`
|
|
5443
|
+
active: (s) => `${pqb.colors.yellow("\u276F")} ${pqb.colors.yellow(s)}`
|
|
5463
5444
|
});
|
|
5464
5445
|
moveFile = result ? file : prev;
|
|
5465
5446
|
}
|
|
@@ -5605,7 +5586,7 @@ const listMigrationsStatuses = async (adapters, config, args) => {
|
|
|
5605
5586
|
green: asIs,
|
|
5606
5587
|
red: asIs,
|
|
5607
5588
|
blue: asIs
|
|
5608
|
-
} : colors;
|
|
5589
|
+
} : pqb.colors;
|
|
5609
5590
|
const log = Object.values(map).map(({ databases, migrations: migrations2 }) => {
|
|
5610
5591
|
let log2 = ` ${c.yellow("Database:")} ${databases.join(", ")}`;
|
|
5611
5592
|
if (migrations2.length === 0) {
|