rake-db 2.33.10 → 2.33.12
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 +17 -2
- package/dist/index.js +51 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -521,6 +521,7 @@ const addOrDropChanges = [];
|
|
|
521
521
|
function add(item, options) {
|
|
522
522
|
consumeColumnName();
|
|
523
523
|
setName(this, item);
|
|
524
|
+
if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
|
|
524
525
|
if (item instanceof Column) {
|
|
525
526
|
const result = addOrDrop("add", item, options);
|
|
526
527
|
if (result.type === "change") return result;
|
|
@@ -544,6 +545,7 @@ function add(item, options) {
|
|
|
544
545
|
const drop = function(item, options) {
|
|
545
546
|
consumeColumnName();
|
|
546
547
|
setName(this, item);
|
|
548
|
+
if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
|
|
547
549
|
if (item instanceof Column) {
|
|
548
550
|
const result = addOrDrop("drop", item, options);
|
|
549
551
|
if (result.type === "change") return result;
|
|
@@ -587,6 +589,28 @@ const addOrDrop = (type, item, options) => {
|
|
|
587
589
|
dropMode: options?.dropMode
|
|
588
590
|
};
|
|
589
591
|
};
|
|
592
|
+
const isColumnForeignKeyChangeInput = (item) => {
|
|
593
|
+
return "columnForeignKey" in item;
|
|
594
|
+
};
|
|
595
|
+
const isNoForeignKeyChangeInput = (item) => {
|
|
596
|
+
return "noForeignKey" in item;
|
|
597
|
+
};
|
|
598
|
+
const isCheckConstraintItem = (item) => {
|
|
599
|
+
return !!item.constraint?.check;
|
|
600
|
+
};
|
|
601
|
+
const changeInputToColumnChange = (item, opposite) => {
|
|
602
|
+
if (item instanceof Column || "type" in item) return columnTypeToColumnChange(item);
|
|
603
|
+
if (isColumnForeignKeyChangeInput(item)) return { foreignKeys: [item.columnForeignKey] };
|
|
604
|
+
if (isNoForeignKeyChangeInput(item)) {
|
|
605
|
+
if (!isColumnForeignKeyChangeInput(opposite)) throw new Error("t.noForeignKey() in t.change(...) must be paired with t.foreignKey(...)");
|
|
606
|
+
return { foreignKeys: [] };
|
|
607
|
+
}
|
|
608
|
+
if (isCheckConstraintItem(item)) return { checks: [{
|
|
609
|
+
sql: item.constraint.check,
|
|
610
|
+
name: item.constraint.name
|
|
611
|
+
}] };
|
|
612
|
+
throw new Error("t.change(...) supports t.check(...) only for check constraints in this form");
|
|
613
|
+
};
|
|
590
614
|
const columnTypeToColumnChange = (item, name) => {
|
|
591
615
|
if (item instanceof Column) {
|
|
592
616
|
let column = item;
|
|
@@ -618,6 +642,18 @@ const setName = (self, item) => {
|
|
|
618
642
|
else if (item instanceof Column) item.data.name ??= name;
|
|
619
643
|
else item.name ??= name;
|
|
620
644
|
};
|
|
645
|
+
function foreignKey(...args) {
|
|
646
|
+
if (Array.isArray(args[0])) {
|
|
647
|
+
const [columns, fnOrTable, foreignColumns, options] = args;
|
|
648
|
+
return tableDataMethods.foreignKey(columns, fnOrTable, foreignColumns, options);
|
|
649
|
+
}
|
|
650
|
+
const [fnOrTable, foreignColumn, options] = args;
|
|
651
|
+
return { columnForeignKey: {
|
|
652
|
+
fnOrTable,
|
|
653
|
+
foreignColumns: [foreignColumn],
|
|
654
|
+
options
|
|
655
|
+
} };
|
|
656
|
+
}
|
|
621
657
|
const tableChangeMethods = {
|
|
622
658
|
...tableMethods,
|
|
623
659
|
...tableDataMethods,
|
|
@@ -629,10 +665,14 @@ const tableChangeMethods = {
|
|
|
629
665
|
},
|
|
630
666
|
add,
|
|
631
667
|
drop,
|
|
668
|
+
foreignKey,
|
|
669
|
+
noForeignKey() {
|
|
670
|
+
return { noForeignKey: true };
|
|
671
|
+
},
|
|
632
672
|
change(from, to, using) {
|
|
633
673
|
consumeColumnName();
|
|
634
|
-
const f =
|
|
635
|
-
const t =
|
|
674
|
+
const f = changeInputToColumnChange(from, to);
|
|
675
|
+
const t = changeInputToColumnChange(to, from);
|
|
636
676
|
setName(this, f);
|
|
637
677
|
setName(this, t);
|
|
638
678
|
return {
|
|
@@ -2095,7 +2135,7 @@ const wrapWithEnhancingError = async (text, values, promise) => {
|
|
|
2095
2135
|
try {
|
|
2096
2136
|
return await promise;
|
|
2097
2137
|
} catch (err) {
|
|
2098
|
-
if (err && typeof err === "object" && "message" in err && typeof err.message === "string" && err.constructor.name === "PostgresError") {
|
|
2138
|
+
if (err && typeof err === "object" && "message" in err && typeof err.message === "string" && (err.constructor.name === "PostgresError" || err.constructor.name === "DatabaseError")) {
|
|
2099
2139
|
err.message += `\nSQL: ${text}${values ? `\nVariables: ${JSON.stringify(values)}` : ""}`;
|
|
2100
2140
|
throw new err.constructor(err);
|
|
2101
2141
|
}
|
|
@@ -2569,13 +2609,11 @@ const getMaybeTransactionAdapter = (db) => "$getAdapter" in db ? db.$getAdapter(
|
|
|
2569
2609
|
const runSqlInSavePoint = async (db, sql, code) => {
|
|
2570
2610
|
const adapter = getMaybeTransactionAdapter(db);
|
|
2571
2611
|
try {
|
|
2572
|
-
|
|
2612
|
+
const query = () => adapter.query(sql);
|
|
2613
|
+
await (adapter.isInTransaction() ? adapter.savepoint("s", query) : query());
|
|
2573
2614
|
return "done";
|
|
2574
2615
|
} catch (err) {
|
|
2575
|
-
if (err.code === code)
|
|
2576
|
-
if (adapter.isInTransaction()) await adapter.query(`ROLLBACK TO SAVEPOINT s`);
|
|
2577
|
-
return "already";
|
|
2578
|
-
}
|
|
2616
|
+
if (err.code === code) return "already";
|
|
2579
2617
|
throw err;
|
|
2580
2618
|
}
|
|
2581
2619
|
};
|
|
@@ -2705,17 +2743,14 @@ const deleteMigratedVersion = async (adapter, version, name, config) => {
|
|
|
2705
2743
|
var NoMigrationsTableError = class extends Error {};
|
|
2706
2744
|
const getMigratedVersionsMap = async (ctx, adapter, config, renameTo) => {
|
|
2707
2745
|
const table = migrationsSchemaTableSql(adapter, config);
|
|
2708
|
-
const
|
|
2746
|
+
const queryVersion = () => adapter.arrays(`SELECT * FROM ${table} ORDER BY version`);
|
|
2709
2747
|
let result;
|
|
2710
2748
|
try {
|
|
2711
|
-
if (
|
|
2712
|
-
result = await
|
|
2713
|
-
if (inTransaction) await adapter.query(`RELEASE SAVEPOINT check_migrations_table`);
|
|
2749
|
+
if (adapter.isInTransaction()) result = await adapter.savepoint("check_migrations_table", queryVersion);
|
|
2750
|
+
else result = await queryVersion();
|
|
2714
2751
|
} catch (err) {
|
|
2715
|
-
if (err.code === "42P01")
|
|
2716
|
-
|
|
2717
|
-
throw new NoMigrationsTableError();
|
|
2718
|
-
} else throw err;
|
|
2752
|
+
if (err.code === "42P01") throw new NoMigrationsTableError();
|
|
2753
|
+
else throw err;
|
|
2719
2754
|
}
|
|
2720
2755
|
if (!result.fields[1]) {
|
|
2721
2756
|
const { migrations } = await getMigrations(ctx, config, true);
|