orchid-orm 1.64.5 → 1.64.7

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