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.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
- let other = from[key];
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
- const config = to.config;
1803
- if (config) {
1804
- const fromConfig = from.config ?? emptyObject;
1805
- for (const key in config) {
1806
- const value = config[key];
1807
- const other = fromConfig[key];
1808
- if (value !== other) {
1809
- queries.push(
1810
- `ALTER ROLE "${name}" ${value ? `SET ${key} = '${value}'` : `RESET ${key}`}`
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.action === "create" ? roleParams(ast) : void 0;
4689
+ const params = roleParams(ast.name, ast);
4673
4690
  const arr = [
4674
- `await db.${ast.action}Role(${singleQuote(ast.name)}${params?.length ? ", {" : ");"}`
4691
+ `await db.${ast.action}Role(${singleQuote(ast.name)}${params.length ? ", {" : ");"}`
4675
4692
  ];
4676
- if (params?.length) {
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 ?? `name != 'postgres' AND name !~ '^pg_'`}`;
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) role.validUntil = new Date(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(