rake-db 2.30.3 → 2.30.6
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 +9 -2
- package/dist/index.js +53 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/index.mjs
CHANGED
|
@@ -1787,33 +1787,46 @@ const changeAstToQuery = ({ name, from, to }) => {
|
|
|
1787
1787
|
const w = [];
|
|
1788
1788
|
for (const key in to) {
|
|
1789
1789
|
let value = to[key];
|
|
1790
|
-
if (key !== "config") {
|
|
1790
|
+
if (key !== "config" && key !== "name") {
|
|
1791
1791
|
if (value instanceof Date) value = value.toISOString();
|
|
1792
|
-
|
|
1793
|
-
if (other instanceof Date) other = other.toISOString();
|
|
1794
|
-
if (value !== other && key in serializers) {
|
|
1795
|
-
w.push(serializers[key](value));
|
|
1796
|
-
}
|
|
1792
|
+
w.push(serializers[key](value));
|
|
1797
1793
|
}
|
|
1798
1794
|
}
|
|
1799
1795
|
if (w.length) {
|
|
1800
1796
|
queries.push(`ALTER ROLE "${name}" WITH ${w.join(" ")}`);
|
|
1801
1797
|
}
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
}
|
|
1798
|
+
if (to.config) {
|
|
1799
|
+
for (const key in to.config) {
|
|
1800
|
+
const value = to.config[key];
|
|
1801
|
+
queries.push(
|
|
1802
|
+
`ALTER ROLE "${name}" ${value ? `SET ${key} = '${value}'` : `RESET ${key}`}`
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
} else if (from.config) {
|
|
1806
|
+
for (const key in from.config) {
|
|
1807
|
+
queries.push(`ALTER ROLE "${name}" RESET ${key}`);
|
|
1813
1808
|
}
|
|
1814
1809
|
}
|
|
1815
1810
|
return queries.join(";\n");
|
|
1816
1811
|
};
|
|
1812
|
+
const renameRole = async (migration, up, from, to) => {
|
|
1813
|
+
if (!up) {
|
|
1814
|
+
const f = from;
|
|
1815
|
+
from = to;
|
|
1816
|
+
to = f;
|
|
1817
|
+
}
|
|
1818
|
+
const ast = makeRenameAst(from, to);
|
|
1819
|
+
const sql = renameAstToQuery(ast);
|
|
1820
|
+
if (sql) await migration.adapter.arrays(sql);
|
|
1821
|
+
};
|
|
1822
|
+
const makeRenameAst = (from, to) => ({
|
|
1823
|
+
type: "renameRole",
|
|
1824
|
+
from,
|
|
1825
|
+
to
|
|
1826
|
+
});
|
|
1827
|
+
const renameAstToQuery = (ast) => {
|
|
1828
|
+
return `ALTER ROLE "${ast.from}" RENAME TO "${ast.to}"`;
|
|
1829
|
+
};
|
|
1817
1830
|
|
|
1818
1831
|
const createMigrationInterface = (tx, up, config) => {
|
|
1819
1832
|
const adapter = Object.create(tx);
|
|
@@ -2704,6 +2717,9 @@ class Migration {
|
|
|
2704
2717
|
dropRole(name, params) {
|
|
2705
2718
|
return createOrDropRole(this, !this.up, name, params);
|
|
2706
2719
|
}
|
|
2720
|
+
renameRole(from, to) {
|
|
2721
|
+
return renameRole(this, this.up, from, to);
|
|
2722
|
+
}
|
|
2707
2723
|
changeRole(name, params) {
|
|
2708
2724
|
return changeRole(
|
|
2709
2725
|
this,
|
|
@@ -4084,7 +4100,8 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4084
4100
|
break;
|
|
4085
4101
|
}
|
|
4086
4102
|
case "role":
|
|
4087
|
-
case "changeRole":
|
|
4103
|
+
case "changeRole":
|
|
4104
|
+
case "renameRole": {
|
|
4088
4105
|
break;
|
|
4089
4106
|
}
|
|
4090
4107
|
default:
|
|
@@ -4669,19 +4686,24 @@ const astEncoders = {
|
|
|
4669
4686
|
return code;
|
|
4670
4687
|
},
|
|
4671
4688
|
role(ast) {
|
|
4672
|
-
const params = ast.
|
|
4689
|
+
const params = roleParams(ast.name, ast);
|
|
4673
4690
|
const arr = [
|
|
4674
|
-
`await db.${ast.action}Role(${singleQuote(ast.name)}${params
|
|
4691
|
+
`await db.${ast.action}Role(${singleQuote(ast.name)}${params.length ? ", {" : ");"}`
|
|
4675
4692
|
];
|
|
4676
|
-
if (params
|
|
4693
|
+
if (params.length) {
|
|
4677
4694
|
arr.push(params);
|
|
4678
4695
|
arr.push("});");
|
|
4679
4696
|
}
|
|
4680
4697
|
return arr;
|
|
4681
4698
|
},
|
|
4699
|
+
renameRole(ast) {
|
|
4700
|
+
return `await db.renameRole(${singleQuote(ast.from)}, ${singleQuote(
|
|
4701
|
+
ast.to
|
|
4702
|
+
)});`;
|
|
4703
|
+
},
|
|
4682
4704
|
changeRole(ast) {
|
|
4683
|
-
const from = roleParams(ast.from, ast.to);
|
|
4684
|
-
const to = roleParams(ast.to, ast.from, true);
|
|
4705
|
+
const from = roleParams(ast.name, ast.from, ast.to);
|
|
4706
|
+
const to = roleParams(ast.name, ast.to, ast.from, true);
|
|
4685
4707
|
return [
|
|
4686
4708
|
`await db.changeRole(${singleQuote(ast.name)}, {`,
|
|
4687
4709
|
[...from.length ? ["from: {", from, "},"] : [], "to: {", to, "},"],
|
|
@@ -4689,7 +4711,7 @@ const astEncoders = {
|
|
|
4689
4711
|
];
|
|
4690
4712
|
}
|
|
4691
4713
|
};
|
|
4692
|
-
const roleParams = (ast, compare, to) => {
|
|
4714
|
+
const roleParams = (name, ast, compare, to) => {
|
|
4693
4715
|
const params = [];
|
|
4694
4716
|
for (const key of [
|
|
4695
4717
|
"name",
|
|
@@ -4704,7 +4726,7 @@ const roleParams = (ast, compare, to) => {
|
|
|
4704
4726
|
"bypassRls",
|
|
4705
4727
|
"config"
|
|
4706
4728
|
]) {
|
|
4707
|
-
if (key === "name" && !to) continue;
|
|
4729
|
+
if (key === "name" && (!to || ast.name === name)) continue;
|
|
4708
4730
|
let value = ast[key];
|
|
4709
4731
|
if (!compare && (!value || key === "connLimit" && value === -1)) {
|
|
4710
4732
|
continue;
|
|
@@ -5128,18 +5150,19 @@ const collationsSql = (version) => `SELECT
|
|
|
5128
5150
|
FROM pg_collation
|
|
5129
5151
|
JOIN pg_namespace n on pg_collation.collnamespace = n.oid
|
|
5130
5152
|
WHERE ${filterSchema("n.nspname")}`;
|
|
5131
|
-
const roleSql = (params) => `SELECT json_agg(json_build_object(
|
|
5153
|
+
const roleSql = (params) => `SELECT COALESCE(json_agg(json_build_object(
|
|
5132
5154
|
'name', rolname,
|
|
5133
5155
|
'super', rolsuper,
|
|
5134
5156
|
'inherit', rolinherit,
|
|
5135
5157
|
'createRole', rolcreaterole,
|
|
5158
|
+
'createDb', rolcreatedb,
|
|
5136
5159
|
'canLogin', rolcanlogin,
|
|
5137
5160
|
'replication', rolreplication,
|
|
5138
5161
|
'connLimit', rolconnlimit,
|
|
5139
5162
|
'validUntil', rolvaliduntil,
|
|
5140
5163
|
'bypassRls', rolbypassrls,
|
|
5141
5164
|
'config', rolconfig
|
|
5142
|
-
)) FROM pg_roles WHERE ${params.whereSql ?? `
|
|
5165
|
+
)), '[]') FROM pg_roles WHERE ${params.whereSql ?? `rolname != 'postgres' AND rolname !~ '^pg_'`}`;
|
|
5143
5166
|
const sql = (version, params) => `SELECT (${schemasSql}) AS "schemas", ${jsonAgg(
|
|
5144
5167
|
tablesSql,
|
|
5145
5168
|
"tables"
|
|
@@ -5243,7 +5266,9 @@ async function introspectDbSchema(db, params) {
|
|
|
5243
5266
|
if (result.roles) {
|
|
5244
5267
|
for (const role of result.roles) {
|
|
5245
5268
|
nullsToUndefined(role);
|
|
5246
|
-
if (role.validUntil)
|
|
5269
|
+
if (role.validUntil) {
|
|
5270
|
+
role.validUntil = role.validUntil === "infinity" ? void 0 : new Date(role.validUntil);
|
|
5271
|
+
}
|
|
5247
5272
|
if (role.config) {
|
|
5248
5273
|
const arr = role.config;
|
|
5249
5274
|
role.config = Object.fromEntries(
|