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