orchid-orm 1.64.5 → 1.64.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.
@@ -2166,6 +2166,90 @@ const processTableChange = async (adapter, structureToAstCtx, dbStructure, domai
2166
2166
  }
2167
2167
  };
2168
2168
 
2169
+ const processRoles = async (ast, dbStructure, { verifying, internal: { roles } }) => {
2170
+ if (!dbStructure.roles || !roles) return;
2171
+ const codeRoles = roles.map(
2172
+ (role) => ({
2173
+ super: false,
2174
+ inherit: false,
2175
+ createRole: false,
2176
+ createDb: false,
2177
+ canLogin: false,
2178
+ replication: false,
2179
+ connLimit: -1,
2180
+ bypassRls: false,
2181
+ ...role
2182
+ })
2183
+ );
2184
+ const found = /* @__PURE__ */ new Set();
2185
+ const dropRoles = [];
2186
+ for (const dbRole of dbStructure.roles) {
2187
+ const codeRole = codeRoles.find(
2188
+ (codeRole2) => dbRole.name === codeRole2.name
2189
+ );
2190
+ if (codeRole) {
2191
+ found.add(dbRole.name);
2192
+ if (!pqb.deepCompare(dbRole, codeRole)) {
2193
+ ast.push({
2194
+ type: "changeRole",
2195
+ name: dbRole.name,
2196
+ from: dbRole,
2197
+ to: codeRole
2198
+ });
2199
+ }
2200
+ continue;
2201
+ }
2202
+ dropRoles.push(dbRole);
2203
+ }
2204
+ for (const codeRole of codeRoles) {
2205
+ if (found.has(codeRole.name)) continue;
2206
+ if (dropRoles.length) {
2207
+ const i = await promptCreateOrRename(
2208
+ "table",
2209
+ codeRole.name,
2210
+ dropRoles.map((x) => x.name),
2211
+ verifying
2212
+ );
2213
+ if (i) {
2214
+ const dbRole = dropRoles[i - 1];
2215
+ dropRoles.splice(i - 1, 1);
2216
+ ast.push(makeRenameOrChangeAst(dbRole, codeRole));
2217
+ continue;
2218
+ }
2219
+ }
2220
+ ast.push({
2221
+ type: "role",
2222
+ action: "create",
2223
+ ...codeRole
2224
+ });
2225
+ }
2226
+ for (const dbRole of dropRoles) {
2227
+ ast.push({
2228
+ type: "role",
2229
+ action: "drop",
2230
+ ...dbRole
2231
+ });
2232
+ }
2233
+ };
2234
+ const makeRenameOrChangeAst = (dbRole, codeRole) => {
2235
+ const { name: dbRoleName, ...dbRoleRest } = dbRole;
2236
+ const { name: codeRoleName, ...codeRoleRest } = codeRole;
2237
+ if (pqb.deepCompare(dbRoleRest, codeRoleRest) && dbRoleName !== codeRoleName) {
2238
+ return {
2239
+ type: "renameRole",
2240
+ from: dbRoleName,
2241
+ to: codeRoleName
2242
+ };
2243
+ } else {
2244
+ return {
2245
+ type: "changeRole",
2246
+ name: dbRole.name,
2247
+ from: dbRole,
2248
+ to: codeRole
2249
+ };
2250
+ }
2251
+ };
2252
+
2169
2253
  class PendingDbTypes {
2170
2254
  constructor() {
2171
2255
  this.set = /* @__PURE__ */ new Set();
@@ -2176,6 +2260,7 @@ class PendingDbTypes {
2176
2260
  }
2177
2261
  const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2178
2262
  const { structureToAstCtx, currentSchema } = params;
2263
+ await processRoles(ast, dbStructure, params);
2179
2264
  const domainsMap = rakeDb.makeDomainsMap(structureToAstCtx, dbStructure);
2180
2265
  await processSchemas(ast, dbStructure, params);
2181
2266
  processExtensions(ast, dbStructure, params);
@@ -2202,7 +2287,7 @@ const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2202
2287
  };
2203
2288
 
2204
2289
  const rollbackErr = new Error("Rollback");
2205
- const verifyMigration = async (adapter, config, migrationCode, generateMigrationParams) => {
2290
+ const verifyMigration = async (adapter, config, migrationCode, generateMigrationParams, roles) => {
2206
2291
  const migrationFn = new Function("change", migrationCode);
2207
2292
  let code;
2208
2293
  try {
@@ -2218,7 +2303,9 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
2218
2303
  for (const changeFn of changeFns) {
2219
2304
  await changeFn(db, true);
2220
2305
  }
2221
- const dbStructure = await rakeDb.introspectDbSchema(trx);
2306
+ const dbStructure = await rakeDb.introspectDbSchema(trx, {
2307
+ roles
2308
+ });
2222
2309
  generateMigrationParams.verifying = true;
2223
2310
  try {
2224
2311
  code = await composeMigration(
@@ -2541,7 +2628,17 @@ const report = (ast, config, currentSchema) => {
2541
2628
  break;
2542
2629
  }
2543
2630
  case "role":
2631
+ code.push(
2632
+ `${a.action === "create" ? green("+ create role") : red("- drop role")} ${a.name}`
2633
+ );
2634
+ break;
2635
+ case "renameRole":
2636
+ code.push(
2637
+ `${yellow("~ rename role")} ${a.from} ${yellow("=>")} ${a.to}`
2638
+ );
2639
+ break;
2544
2640
  case "changeRole": {
2641
+ code.push(`${yellow("~ change role")} ${a.name}`);
2545
2642
  break;
2546
2643
  }
2547
2644
  default:
@@ -2573,16 +2670,18 @@ const generate = async (adapters, config, args, afterPull) => {
2573
2670
  if (afterPull) {
2574
2671
  adapters = [afterPull.adapter];
2575
2672
  }
2673
+ const db = await getDbFromConfig(config, dbPath);
2674
+ const { columnTypes, internal } = db.$qb;
2675
+ const rolesDbStructureParam = internal.roles ? internal.managedRolesSql ? { whereSql: internal.managedRolesSql } : pqb.emptyObject : void 0;
2576
2676
  const { dbStructure } = await migrateAndPullStructures(
2577
2677
  adapters,
2578
2678
  config,
2679
+ rolesDbStructureParam,
2579
2680
  afterPull
2580
2681
  );
2581
2682
  const [adapter] = adapters;
2582
2683
  const adapterSchema = adapter.getSchema();
2583
2684
  const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
2584
- const db = await getDbFromConfig(config, dbPath);
2585
- const { columnTypes, internal } = db.$qb;
2586
2685
  const codeItems = await getActualItems(
2587
2686
  db,
2588
2687
  currentSchema,
@@ -2618,7 +2717,8 @@ const generate = async (adapters, config, args, afterPull) => {
2618
2717
  adapter,
2619
2718
  config,
2620
2719
  migrationCode,
2621
- generateMigrationParams
2720
+ generateMigrationParams,
2721
+ rolesDbStructureParam
2622
2722
  );
2623
2723
  if (result !== void 0) {
2624
2724
  throw new Error(
@@ -2676,7 +2776,7 @@ const getDbFromConfig = async (config, dbPath) => {
2676
2776
  }
2677
2777
  return db;
2678
2778
  };
2679
- const migrateAndPullStructures = async (adapters, config, afterPull) => {
2779
+ const migrateAndPullStructures = async (adapters, config, roles, afterPull) => {
2680
2780
  if (afterPull) {
2681
2781
  return {
2682
2782
  dbStructure: {
@@ -2698,7 +2798,11 @@ const migrateAndPullStructures = async (adapters, config, afterPull) => {
2698
2798
  await rakeDb.migrate(adapter, config);
2699
2799
  }
2700
2800
  const dbStructures = await Promise.all(
2701
- adapters.map((adapter) => rakeDb.introspectDbSchema(adapter))
2801
+ adapters.map(
2802
+ (adapter) => rakeDb.introspectDbSchema(adapter, {
2803
+ roles
2804
+ })
2805
+ )
2702
2806
  );
2703
2807
  const dbStructure = dbStructures[0];
2704
2808
  for (let i = 1; i < dbStructures.length; i++) {