rake-db 2.30.5 → 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 CHANGED
@@ -176,6 +176,7 @@ interface IntrospectedStructure {
176
176
  domains: DbStructure.Domain[];
177
177
  collations: DbStructure.Collation[];
178
178
  roles?: DbStructure.Role[];
179
+ managedRolesSql?: string;
179
180
  }
180
181
  interface IntrospectDbStructureParams {
181
182
  roles?: {
@@ -184,7 +185,7 @@ interface IntrospectDbStructureParams {
184
185
  }
185
186
  declare function introspectDbSchema(db: AdapterBase, params?: IntrospectDbStructureParams): Promise<IntrospectedStructure>;
186
187
 
187
- type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.RenameSchema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.ChangeEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.RenameTableItem | RakeDbAst.View | RakeDbAst.Role | RakeDbAst.ChangeRole;
188
+ type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.RenameSchema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.ChangeEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.RenameTableItem | RakeDbAst.View | RakeDbAst.Role | RakeDbAst.RenameRole | RakeDbAst.ChangeRole;
188
189
  declare namespace RakeDbAst {
189
190
  interface Table extends TableData {
190
191
  type: 'table';
@@ -379,6 +380,11 @@ declare namespace RakeDbAst {
379
380
  type: 'role';
380
381
  action: 'create' | 'drop';
381
382
  }
383
+ interface RenameRole {
384
+ type: 'renameRole';
385
+ from: string;
386
+ to: string;
387
+ }
382
388
  interface ChangeRole {
383
389
  type: 'changeRole';
384
390
  name: string;
@@ -1403,8 +1409,9 @@ declare class Migration<CT = unknown> {
1403
1409
  * @param constraintName - name of the constraint
1404
1410
  */
1405
1411
  constraintExists(constraintName: string): Promise<boolean>;
1406
- createRole(name: string, params: Partial<DbStructure.Role>): Promise<void>;
1412
+ createRole(name: string, params?: Partial<DbStructure.Role>): Promise<void>;
1407
1413
  dropRole(name: string, params?: Partial<DbStructure.Role>): Promise<void>;
1414
+ renameRole(from: string, to: string): Promise<void>;
1408
1415
  changeRole(name: string, params: {
1409
1416
  from?: Partial<DbStructure.Role>;
1410
1417
  to: Partial<DbStructure.Role>;
package/dist/index.js CHANGED
@@ -1789,33 +1789,46 @@ const changeAstToQuery = ({ name, from, to }) => {
1789
1789
  const w = [];
1790
1790
  for (const key in to) {
1791
1791
  let value = to[key];
1792
- if (key !== "config") {
1792
+ if (key !== "config" && key !== "name") {
1793
1793
  if (value instanceof Date) value = value.toISOString();
1794
- let other = from[key];
1795
- if (other instanceof Date) other = other.toISOString();
1796
- if (value !== other && key in serializers) {
1797
- w.push(serializers[key](value));
1798
- }
1794
+ w.push(serializers[key](value));
1799
1795
  }
1800
1796
  }
1801
1797
  if (w.length) {
1802
1798
  queries.push(`ALTER ROLE "${name}" WITH ${w.join(" ")}`);
1803
1799
  }
1804
- const config = to.config;
1805
- if (config) {
1806
- const fromConfig = from.config ?? pqb.emptyObject;
1807
- for (const key in config) {
1808
- const value = config[key];
1809
- const other = fromConfig[key];
1810
- if (value !== other) {
1811
- queries.push(
1812
- `ALTER ROLE "${name}" ${value ? `SET ${key} = '${value}'` : `RESET ${key}`}`
1813
- );
1814
- }
1800
+ if (to.config) {
1801
+ for (const key in to.config) {
1802
+ const value = to.config[key];
1803
+ queries.push(
1804
+ `ALTER ROLE "${name}" ${value ? `SET ${key} = '${value}'` : `RESET ${key}`}`
1805
+ );
1806
+ }
1807
+ } else if (from.config) {
1808
+ for (const key in from.config) {
1809
+ queries.push(`ALTER ROLE "${name}" RESET ${key}`);
1815
1810
  }
1816
1811
  }
1817
1812
  return queries.join(";\n");
1818
1813
  };
1814
+ const renameRole = async (migration, up, from, to) => {
1815
+ if (!up) {
1816
+ const f = from;
1817
+ from = to;
1818
+ to = f;
1819
+ }
1820
+ const ast = makeRenameAst(from, to);
1821
+ const sql = renameAstToQuery(ast);
1822
+ if (sql) await migration.adapter.arrays(sql);
1823
+ };
1824
+ const makeRenameAst = (from, to) => ({
1825
+ type: "renameRole",
1826
+ from,
1827
+ to
1828
+ });
1829
+ const renameAstToQuery = (ast) => {
1830
+ return `ALTER ROLE "${ast.from}" RENAME TO "${ast.to}"`;
1831
+ };
1819
1832
 
1820
1833
  const createMigrationInterface = (tx, up, config) => {
1821
1834
  const adapter = Object.create(tx);
@@ -2706,6 +2719,9 @@ class Migration {
2706
2719
  dropRole(name, params) {
2707
2720
  return createOrDropRole(this, !this.up, name, params);
2708
2721
  }
2722
+ renameRole(from, to) {
2723
+ return renameRole(this, this.up, from, to);
2724
+ }
2709
2725
  changeRole(name, params) {
2710
2726
  return changeRole(
2711
2727
  this,
@@ -4086,7 +4102,8 @@ const astToGenerateItem = (config, ast, currentSchema) => {
4086
4102
  break;
4087
4103
  }
4088
4104
  case "role":
4089
- case "changeRole": {
4105
+ case "changeRole":
4106
+ case "renameRole": {
4090
4107
  break;
4091
4108
  }
4092
4109
  default:
@@ -4671,19 +4688,24 @@ const astEncoders = {
4671
4688
  return code;
4672
4689
  },
4673
4690
  role(ast) {
4674
- const params = ast.action === "create" ? roleParams(ast) : void 0;
4691
+ const params = roleParams(ast.name, ast);
4675
4692
  const arr = [
4676
- `await db.${ast.action}Role(${pqb.singleQuote(ast.name)}${params?.length ? ", {" : ");"}`
4693
+ `await db.${ast.action}Role(${pqb.singleQuote(ast.name)}${params.length ? ", {" : ");"}`
4677
4694
  ];
4678
- if (params?.length) {
4695
+ if (params.length) {
4679
4696
  arr.push(params);
4680
4697
  arr.push("});");
4681
4698
  }
4682
4699
  return arr;
4683
4700
  },
4701
+ renameRole(ast) {
4702
+ return `await db.renameRole(${pqb.singleQuote(ast.from)}, ${pqb.singleQuote(
4703
+ ast.to
4704
+ )});`;
4705
+ },
4684
4706
  changeRole(ast) {
4685
- const from = roleParams(ast.from, ast.to);
4686
- const to = roleParams(ast.to, ast.from, true);
4707
+ const from = roleParams(ast.name, ast.from, ast.to);
4708
+ const to = roleParams(ast.name, ast.to, ast.from, true);
4687
4709
  return [
4688
4710
  `await db.changeRole(${pqb.singleQuote(ast.name)}, {`,
4689
4711
  [...from.length ? ["from: {", from, "},"] : [], "to: {", to, "},"],
@@ -4691,7 +4713,7 @@ const astEncoders = {
4691
4713
  ];
4692
4714
  }
4693
4715
  };
4694
- const roleParams = (ast, compare, to) => {
4716
+ const roleParams = (name, ast, compare, to) => {
4695
4717
  const params = [];
4696
4718
  for (const key of [
4697
4719
  "name",
@@ -4706,7 +4728,7 @@ const roleParams = (ast, compare, to) => {
4706
4728
  "bypassRls",
4707
4729
  "config"
4708
4730
  ]) {
4709
- if (key === "name" && !to) continue;
4731
+ if (key === "name" && (!to || ast.name === name)) continue;
4710
4732
  let value = ast[key];
4711
4733
  if (!compare && (!value || key === "connLimit" && value === -1)) {
4712
4734
  continue;
@@ -5130,18 +5152,19 @@ const collationsSql = (version) => `SELECT
5130
5152
  FROM pg_collation
5131
5153
  JOIN pg_namespace n on pg_collation.collnamespace = n.oid
5132
5154
  WHERE ${filterSchema("n.nspname")}`;
5133
- const roleSql = (params) => `SELECT json_agg(json_build_object(
5155
+ const roleSql = (params) => `SELECT COALESCE(json_agg(json_build_object(
5134
5156
  'name', rolname,
5135
5157
  'super', rolsuper,
5136
5158
  'inherit', rolinherit,
5137
5159
  'createRole', rolcreaterole,
5160
+ 'createDb', rolcreatedb,
5138
5161
  'canLogin', rolcanlogin,
5139
5162
  'replication', rolreplication,
5140
5163
  'connLimit', rolconnlimit,
5141
5164
  'validUntil', rolvaliduntil,
5142
5165
  'bypassRls', rolbypassrls,
5143
5166
  'config', rolconfig
5144
- )) FROM pg_roles WHERE ${params.whereSql ?? `name != 'postgres' AND name !~ '^pg_'`}`;
5167
+ )), '[]') FROM pg_roles WHERE ${params.whereSql ?? `rolname != 'postgres' AND rolname !~ '^pg_'`}`;
5145
5168
  const sql = (version, params) => `SELECT (${schemasSql}) AS "schemas", ${jsonAgg(
5146
5169
  tablesSql,
5147
5170
  "tables"
@@ -5245,7 +5268,9 @@ async function introspectDbSchema(db, params) {
5245
5268
  if (result.roles) {
5246
5269
  for (const role of result.roles) {
5247
5270
  nullsToUndefined(role);
5248
- if (role.validUntil) role.validUntil = new Date(role.validUntil);
5271
+ if (role.validUntil) {
5272
+ role.validUntil = role.validUntil === "infinity" ? void 0 : new Date(role.validUntil);
5273
+ }
5249
5274
  if (role.config) {
5250
5275
  const arr = role.config;
5251
5276
  role.config = Object.fromEntries(