@akal/supaforge 0.0.35 → 0.0.36

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.
Files changed (39) hide show
  1. package/README.md +4 -1
  2. package/dist/base-command.js +4 -4
  3. package/dist/{chunk-SM7HP6EP.js → chunk-5UHIWFQG.js} +69 -9
  4. package/dist/{chunk-O5ERC2SE.js → chunk-AL6INEFX.js} +3 -3
  5. package/dist/{chunk-4AGJRRBT.js → chunk-CAQ3QQFP.js} +70 -22
  6. package/dist/{chunk-5Q6LSLA3.js → chunk-CCB7ARZU.js} +6 -6
  7. package/dist/{chunk-YZM76W22.js → chunk-EWNZIRCD.js} +4 -4
  8. package/dist/{chunk-YWERIWFD.js → chunk-FD2JKEJA.js} +5 -5
  9. package/dist/{chunk-NEM5AEPQ.js → chunk-GGZ7YWBZ.js} +24 -15
  10. package/dist/{chunk-7N63OPFN.js → chunk-JX5L253P.js} +46 -16
  11. package/dist/{chunk-GQPGH6YR.js → chunk-KHXEES2M.js} +2 -2
  12. package/dist/{chunk-4M6OQLPO.js → chunk-KT7NMELZ.js} +77 -7
  13. package/dist/{chunk-44UVQJV2.js → chunk-M2564IGT.js} +22 -15
  14. package/dist/{chunk-H2D3YSKM.js → chunk-MRXIE55Y.js} +3 -3
  15. package/dist/{chunk-NX2KKUS7.js → chunk-MZPWRWBL.js} +7 -7
  16. package/dist/{chunk-XLZCX6R2.js → chunk-OXS42LJH.js} +4 -4
  17. package/dist/{chunk-HNJ6BNNZ.js → chunk-Q43MN6NE.js} +2 -2
  18. package/dist/{chunk-ZI435CAU.js → chunk-RQCXGO37.js} +2 -2
  19. package/dist/{chunk-KNXHLVF7.js → chunk-SQ52YBKK.js} +3 -2
  20. package/dist/{chunk-T7GMPGUO.js → chunk-YUJ2MHLM.js} +674 -22
  21. package/dist/{chunk-AGONS5JJ.js → chunk-Z2YUOA4W.js} +2 -2
  22. package/dist/commands/clone.js +8 -8
  23. package/dist/commands/diff.d.ts +1 -0
  24. package/dist/commands/diff.js +14 -15
  25. package/dist/commands/hukam.js +14 -15
  26. package/dist/commands/init.js +2 -2
  27. package/dist/commands/mcp.js +9 -10
  28. package/dist/commands/migrate/baseline.js +5 -5
  29. package/dist/commands/migrate/create.js +22 -13
  30. package/dist/commands/migrate/list.js +5 -5
  31. package/dist/commands/migrate/run.js +9 -9
  32. package/dist/commands/report.js +5 -5
  33. package/dist/commands/restore.js +14 -13
  34. package/dist/commands/snapshot.js +8 -8
  35. package/dist/commands/sync.js +14 -15
  36. package/dist/index.d.ts +17 -0
  37. package/dist/index.js +13 -14
  38. package/package.json +2 -2
  39. package/dist/chunk-WNTCZM2J.js +0 -468
@@ -1,468 +0,0 @@
1
- import {
2
- Check,
3
- pgQuery,
4
- quoteName
5
- } from "./chunk-4M6OQLPO.js";
6
- import {
7
- SUPABASE_PLATFORM_SCHEMAS
8
- } from "./chunk-KNXHLVF7.js";
9
-
10
- // src/checks/roles.ts
11
- var PLATFORM_ROLES = [
12
- "postgres",
13
- "supabase_admin",
14
- "authenticator",
15
- "supabase_auth_admin",
16
- "supabase_storage_admin",
17
- "dashboard_user",
18
- "pgbouncer",
19
- "supavisor",
20
- // Created by Supabase's own images for Edge Functions, Realtime, read
21
- // replicas and logical replication. Diffing a project against plain
22
- // PostgreSQL offered to create all four on the target.
23
- "supabase_functions_admin",
24
- "supabase_realtime_admin",
25
- "supabase_read_only_user",
26
- "supabase_replication_admin"
27
- ];
28
- var API_ROLES = ["anon", "authenticated", "service_role"];
29
- var quoted = (names) => names.map((n) => `'${n.replace(/'/g, "''")}'`).join(", ");
30
- var ROLES_SQL = `
31
- SELECT
32
- rolname, rolsuper, rolinherit, rolcreaterole, rolcreatedb,
33
- rolcanlogin, rolreplication, rolbypassrls, rolconnlimit,
34
- rolvaliduntil::text AS rolvaliduntil
35
- FROM pg_roles
36
- WHERE NOT (rolname LIKE 'pg_%')
37
- AND rolname NOT IN (${quoted([...PLATFORM_ROLES, ...API_ROLES])})
38
- AND rolname <> current_user
39
- ORDER BY rolname
40
- `;
41
- function grantSchemaFilter(ignoreSchemas) {
42
- const excluded = [.../* @__PURE__ */ new Set([...SUPABASE_PLATFORM_SCHEMAS, ...ignoreSchemas])];
43
- return `NOT IN (${quoted(excluded)})`;
44
- }
45
- var grantsSql = (ignoreSchemas) => `
46
- SELECT CASE WHEN a.grantee = 0 THEN 'PUBLIC' ELSE pg_get_userbyid(a.grantee) END AS grantee,
47
- n.nspname AS table_schema, c.relname AS table_name,
48
- a.privilege_type, a.is_grantable,
49
- CASE c.relkind WHEN 'S' THEN 'sequence' ELSE 'table' END AS object_kind
50
- FROM pg_class c
51
- JOIN pg_namespace n ON n.oid = c.relnamespace
52
- CROSS JOIN LATERAL aclexplode(c.relacl) a
53
- WHERE c.relacl IS NOT NULL
54
- AND c.relkind IN ('r', 'p', 'v', 'm', 'f', 'S')
55
- AND a.grantee <> c.relowner
56
- AND n.nspname ${grantSchemaFilter(ignoreSchemas)}
57
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT IN (${quoted(PLATFORM_ROLES)}))
58
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT LIKE 'pg_%')
59
- ORDER BY 1, 2, 3, 4
60
- `;
61
- var columnGrantsSql = (ignoreSchemas) => `
62
- SELECT CASE WHEN a.grantee = 0 THEN 'PUBLIC' ELSE pg_get_userbyid(a.grantee) END AS grantee,
63
- n.nspname AS table_schema, c.relname AS table_name, att.attname AS column_name,
64
- a.privilege_type, a.is_grantable
65
- FROM pg_attribute att
66
- JOIN pg_class c ON c.oid = att.attrelid
67
- JOIN pg_namespace n ON n.oid = c.relnamespace
68
- CROSS JOIN LATERAL aclexplode(att.attacl) a
69
- WHERE att.attacl IS NOT NULL AND att.attnum > 0 AND NOT att.attisdropped
70
- AND c.relkind IN ('r', 'p', 'v', 'm', 'f')
71
- AND a.grantee <> c.relowner
72
- AND n.nspname ${grantSchemaFilter(ignoreSchemas)}
73
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT IN (${quoted(PLATFORM_ROLES)}))
74
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT LIKE 'pg_%')
75
- ORDER BY 1, 2, 3, 4, 5
76
- `;
77
- var functionGrantsSql = (ignoreSchemas) => `
78
- SELECT CASE WHEN a.grantee = 0 THEN 'PUBLIC' ELSE pg_get_userbyid(a.grantee) END AS grantee,
79
- n.nspname AS table_schema, p.proname AS table_name,
80
- pg_get_function_identity_arguments(p.oid) AS args,
81
- CASE p.prokind WHEN 'p' THEN 'procedure' ELSE 'function' END AS object_kind,
82
- a.privilege_type, a.is_grantable
83
- FROM pg_proc p
84
- JOIN pg_namespace n ON n.oid = p.pronamespace
85
- CROSS JOIN LATERAL aclexplode(coalesce(p.proacl, acldefault('f', p.proowner))) a
86
- WHERE a.grantee <> p.proowner
87
- AND n.nspname ${grantSchemaFilter(ignoreSchemas)}
88
- AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_proc'::regclass AND d.objid = p.oid AND d.deptype = 'e')
89
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT IN (${quoted(PLATFORM_ROLES)}))
90
- AND (a.grantee = 0 OR pg_get_userbyid(a.grantee) NOT LIKE 'pg_%')
91
- ORDER BY 1, 2, 3, 4, 6
92
- `;
93
- var routinesSql = (ignoreSchemas) => `
94
- SELECT n.nspname AS table_schema, p.proname AS table_name,
95
- pg_get_function_identity_arguments(p.oid) AS args,
96
- CASE p.prokind WHEN 'p' THEN 'procedure' ELSE 'function' END AS object_kind
97
- FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
98
- WHERE n.nspname ${grantSchemaFilter(ignoreSchemas)}
99
- AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_proc'::regclass AND d.objid = p.oid AND d.deptype = 'e')
100
- `;
101
- var relationsSql = (ignoreSchemas) => `
102
- SELECT n.nspname AS table_schema, c.relname AS table_name,
103
- CASE c.relkind WHEN 'S' THEN 'sequence' ELSE 'table' END AS object_kind
104
- FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
105
- WHERE c.relkind IN ('r', 'p', 'v', 'm', 'f', 'S')
106
- AND n.nspname ${grantSchemaFilter(ignoreSchemas)}
107
- `;
108
- var DEFAULT_GRANTS_SQL = `
109
- SELECT CASE d.defaclobjtype WHEN 'S' THEN 'sequence' WHEN 'f' THEN 'function' ELSE 'table' END AS object_kind,
110
- n.nspname AS table_schema,
111
- CASE WHEN a.grantee = 0 THEN 'PUBLIC' ELSE pg_get_userbyid(a.grantee) END AS grantee,
112
- a.privilege_type, a.is_grantable
113
- FROM pg_default_acl d
114
- LEFT JOIN pg_namespace n ON n.oid = d.defaclnamespace
115
- CROSS JOIN LATERAL aclexplode(d.defaclacl) a
116
- WHERE d.defaclobjtype IN ('r', 'S', 'f') AND d.defaclrole = current_user::regrole::oid
117
- AND a.grantee <> d.defaclrole
118
- UNION ALL
119
- SELECT 'function', NULL, 'PUBLIC', 'EXECUTE', false
120
- WHERE NOT EXISTS (
121
- SELECT 1 FROM pg_default_acl d
122
- WHERE d.defaclobjtype = 'f' AND d.defaclnamespace = 0 AND d.defaclrole = current_user::regrole::oid)
123
- `;
124
- function objectKey(o) {
125
- const routine = o.object_kind === "function" || o.object_kind === "procedure";
126
- return `${routine ? "routine" : o.object_kind ?? "table"}:${o.table_schema}.${o.table_name}${routine ? `(${o.args ?? ""})` : ""}`;
127
- }
128
- function defaultKind(kind) {
129
- return kind === "procedure" ? "function" : kind ?? "table";
130
- }
131
- function grantedByDefault(relations, defaults) {
132
- const present = new Set(relations.map(objectKey));
133
- const granted = new Set(defaults.map((d) => `${defaultKind(d.object_kind)}|${d.table_schema ?? "*"}|${d.grantee}|${d.privilege_type}|${Boolean(d.is_grantable)}`));
134
- return (g) => !g.column_name && !present.has(objectKey(g)) && [g.table_schema, "*"].some((schema) => granted.has(`${defaultKind(g.object_kind)}|${schema}|${g.grantee}|${g.privilege_type}|${Boolean(g.is_grantable)}`));
135
- }
136
- function defaultGrantsToRevoke(sourceRelations, targetRelations, defaults, sourceGrants) {
137
- const present = new Set(targetRelations.map(objectKey));
138
- const held = new Set(sourceGrants.filter((g) => !g.column_name).map((g) => `${objectKey(g)}|${g.grantee}|${g.privilege_type}`));
139
- const issues = [];
140
- for (const rel of sourceRelations) {
141
- if (present.has(objectKey(rel))) continue;
142
- const name = `${rel.table_schema}.${rel.table_name}`;
143
- const extra = /* @__PURE__ */ new Map();
144
- for (const d of defaults) {
145
- if (defaultKind(d.object_kind) !== defaultKind(rel.object_kind)) continue;
146
- if (d.table_schema !== null && d.table_schema !== rel.table_schema) continue;
147
- if (held.has(`${objectKey(rel)}|${d.grantee}|${d.privilege_type}`)) continue;
148
- extra.set(d.grantee, (extra.get(d.grantee) ?? /* @__PURE__ */ new Set()).add(d.privilege_type));
149
- }
150
- for (const [grantee, privileges] of [...extra].sort(([a], [b]) => a.localeCompare(b))) {
151
- const gs = [...privileges].sort().map((privilege_type) => ({
152
- grantee,
153
- table_schema: rel.table_schema,
154
- table_name: rel.table_name,
155
- privilege_type,
156
- is_grantable: false,
157
- ...rel.object_kind && rel.object_kind !== "table" ? { object_kind: rel.object_kind } : {},
158
- ...rel.args !== void 0 ? { args: rel.args } : {}
159
- }));
160
- const routine = rel.object_kind === "function" || rel.object_kind === "procedure";
161
- issues.push({
162
- // Tables keep the id they have always had; others say what they are.
163
- id: `roles-grant-default-${grantee}.${name}${routine ? `(${rel.args ?? ""})` : ""}`,
164
- check: "roles",
165
- severity: extraGrantSeverity(grantee),
166
- title: `Default grants to take back: ${describeGroup(gs)}`,
167
- description: `Creating ${describeObject(gs[0])} on the target gives ${grantee} what its default privileges grant. The source does not grant "${describeGroup(gs)}", so it is revoked once it exists.`,
168
- targetValue: gs,
169
- sql: {
170
- up: `REVOKE ${groupTarget(gs)} FROM ${granteeSql(grantee)};`,
171
- down: `GRANT ${groupTarget(gs)} TO ${granteeSql(grantee)};`
172
- }
173
- });
174
- }
175
- }
176
- return issues;
177
- }
178
- var OPEN_GRANTEES = /* @__PURE__ */ new Set(["anon", "authenticated", "PUBLIC"]);
179
- function extraGrantSeverity(grantee) {
180
- return OPEN_GRANTEES.has(grantee) ? "critical" : "warning";
181
- }
182
- var RolesCheck = class extends Check {
183
- constructor(queryFn = pgQuery) {
184
- super();
185
- this.queryFn = queryFn;
186
- }
187
- name = "roles";
188
- async scan(ctx) {
189
- const ignore = ctx.config.ignoreSchemas ?? [];
190
- const version = async (url) => Number((await this.queryFn(url, `SELECT current_setting('server_version_num') AS v`))[0]?.v ?? 0);
191
- const [
192
- sourceRoles,
193
- targetRoles,
194
- sourceGrants,
195
- targetGrants,
196
- sourceColumns,
197
- targetColumns,
198
- sourceVersion,
199
- targetVersion,
200
- sourceRelations,
201
- targetRelations,
202
- targetDefaults,
203
- sourceRoutineGrants,
204
- targetRoutineGrants,
205
- sourceRoutines,
206
- targetRoutines
207
- ] = await Promise.all([
208
- this.queryFn(ctx.source.dbUrl, ROLES_SQL),
209
- this.queryFn(ctx.target.dbUrl, ROLES_SQL),
210
- this.queryFn(ctx.source.dbUrl, grantsSql(ignore)),
211
- this.queryFn(ctx.target.dbUrl, grantsSql(ignore)),
212
- this.queryFn(ctx.source.dbUrl, columnGrantsSql(ignore)),
213
- this.queryFn(ctx.target.dbUrl, columnGrantsSql(ignore)),
214
- version(ctx.source.dbUrl),
215
- version(ctx.target.dbUrl),
216
- this.queryFn(ctx.source.dbUrl, relationsSql(ignore)),
217
- this.queryFn(ctx.target.dbUrl, relationsSql(ignore)),
218
- this.queryFn(ctx.target.dbUrl, DEFAULT_GRANTS_SQL),
219
- this.queryFn(ctx.source.dbUrl, functionGrantsSql(ignore)),
220
- this.queryFn(ctx.target.dbUrl, functionGrantsSql(ignore)),
221
- this.queryFn(ctx.source.dbUrl, routinesSql(ignore)),
222
- this.queryFn(ctx.target.dbUrl, routinesSql(ignore))
223
- ]);
224
- const sourceObjects = [...sourceRelations ?? [], ...sourceRoutines ?? []];
225
- const targetObjects = [...targetRelations ?? [], ...targetRoutines ?? []];
226
- const sourceAll = [...sourceGrants ?? [], ...sourceRoutineGrants ?? []];
227
- const inSource = new Set(sourceObjects.map(objectKey));
228
- const targetAll = [...targetGrants ?? [], ...targetRoutineGrants ?? []].filter((g) => !g.object_kind || g.object_kind === "table" || inSource.has(objectKey(g)));
229
- const comparable = comparablePrivileges(sourceVersion, targetVersion);
230
- const byDefault = grantedByDefault(targetObjects, targetDefaults ?? []);
231
- return [
232
- ...diffRoles(sourceRoles, targetRoles),
233
- ...diffGrants(
234
- [...sourceAll, ...sourceColumns ?? []].filter((g) => comparable(g) && !byDefault(g)),
235
- [...targetAll, ...targetColumns ?? []].filter(comparable),
236
- absentOn(targetRoles, sourceRoles)
237
- ),
238
- ...defaultGrantsToRevoke(
239
- sourceObjects,
240
- targetObjects,
241
- (targetDefaults ?? []).filter((d) => comparable(d)),
242
- sourceAll
243
- )
244
- ];
245
- }
246
- };
247
- function absentOn(target, source) {
248
- const present = new Set((target ?? []).map((r) => r.rolname));
249
- return new Set((source ?? []).map((r) => r.rolname).filter((name) => !present.has(name)));
250
- }
251
- function roleAttrs(r) {
252
- const attrs = [];
253
- if (r.rolsuper) attrs.push("SUPERUSER");
254
- if (r.rolinherit) attrs.push("INHERIT");
255
- if (r.rolcreaterole) attrs.push("CREATEROLE");
256
- if (r.rolcreatedb) attrs.push("CREATEDB");
257
- if (r.rolcanlogin) attrs.push("LOGIN");
258
- if (r.rolreplication) attrs.push("REPLICATION");
259
- if (r.rolbypassrls) attrs.push("BYPASSRLS");
260
- if (r.rolconnlimit >= 0) attrs.push(`CONNECTION LIMIT ${r.rolconnlimit}`);
261
- if (r.rolvaliduntil) attrs.push(`VALID UNTIL '${r.rolvaliduntil}'`);
262
- return attrs.length > 0 ? attrs.join(" ") : "NOLOGIN";
263
- }
264
- function rolesEqual(a, b) {
265
- return a.rolsuper === b.rolsuper && a.rolinherit === b.rolinherit && a.rolcreaterole === b.rolcreaterole && a.rolcreatedb === b.rolcreatedb && a.rolcanlogin === b.rolcanlogin && a.rolreplication === b.rolreplication && a.rolbypassrls === b.rolbypassrls && a.rolconnlimit === b.rolconnlimit && a.rolvaliduntil === b.rolvaliduntil;
266
- }
267
- function diffRoles(source, target) {
268
- const issues = [];
269
- const sourceMap = new Map(source.map((r) => [r.rolname, r]));
270
- const targetMap = new Map(target.map((r) => [r.rolname, r]));
271
- for (const [name, sr] of sourceMap) {
272
- if (!targetMap.has(name)) {
273
- issues.push({
274
- id: `roles-missing-${name}`,
275
- check: "roles",
276
- severity: "critical",
277
- title: `Missing role: ${name}`,
278
- description: `Role "${name}" exists in source but is missing from target. Any RLS policies or grants referencing this role will be silently ineffective.`,
279
- sourceValue: sr,
280
- sql: {
281
- up: `CREATE ROLE ${quoteName(name)} ${roleAttrs(sr)};`,
282
- down: `DROP ROLE IF EXISTS ${quoteName(name)};`
283
- }
284
- });
285
- }
286
- }
287
- for (const [name, tr] of targetMap) {
288
- if (!sourceMap.has(name)) {
289
- issues.push({
290
- id: `roles-extra-${name}`,
291
- check: "roles",
292
- severity: "warning",
293
- title: `Extra role: ${name}`,
294
- description: `Role "${name}" exists in target but not in source.`,
295
- targetValue: tr,
296
- sql: {
297
- up: `DROP ROLE IF EXISTS ${quoteName(name)};`,
298
- down: `CREATE ROLE ${quoteName(name)} ${roleAttrs(tr)};`
299
- }
300
- });
301
- }
302
- }
303
- for (const [name, sr] of sourceMap) {
304
- const tr = targetMap.get(name);
305
- if (tr && !rolesEqual(sr, tr)) {
306
- issues.push({
307
- id: `roles-modified-${name}`,
308
- check: "roles",
309
- severity: "warning",
310
- title: `Modified role: ${name}`,
311
- description: `Role "${name}" has different attributes between source and target.`,
312
- sourceValue: sr,
313
- targetValue: tr,
314
- sql: {
315
- up: `ALTER ROLE ${quoteName(name)} ${roleAttrs(sr)};`,
316
- down: `ALTER ROLE ${quoteName(name)} ${roleAttrs(tr)};`
317
- }
318
- });
319
- }
320
- }
321
- return issues;
322
- }
323
- function grantKey(g) {
324
- const column = g.column_name ? `.${g.column_name}` : "";
325
- const routine = g.object_kind === "function" || g.object_kind === "procedure";
326
- return `${g.grantee}.${g.table_schema}.${g.table_name}${routine ? `(${g.args ?? ""})` : ""}${column}.${g.privilege_type}`;
327
- }
328
- function objectSql(g) {
329
- const name = `${quoteName(g.table_schema)}.${quoteName(g.table_name)}`;
330
- switch (g.object_kind) {
331
- case "sequence":
332
- return `SEQUENCE ${name}`;
333
- case "function":
334
- return `FUNCTION ${name}(${g.args ?? ""})`;
335
- case "procedure":
336
- return `PROCEDURE ${name}(${g.args ?? ""})`;
337
- default:
338
- return name;
339
- }
340
- }
341
- function describeObject(g) {
342
- const name = `${g.table_schema}.${g.table_name}`;
343
- switch (g.object_kind) {
344
- case "sequence":
345
- return `sequence ${name}`;
346
- case "function":
347
- return `function ${name}(${g.args ?? ""})`;
348
- case "procedure":
349
- return `procedure ${name}(${g.args ?? ""})`;
350
- default:
351
- return name;
352
- }
353
- }
354
- function granteeSql(grantee) {
355
- return grantee === "PUBLIC" ? "PUBLIC" : `"${grantee.replace(/"/g, '""')}"`;
356
- }
357
- function grantTarget(g) {
358
- const column = g.column_name ? ` ("${g.column_name.replace(/"/g, '""')}")` : "";
359
- return `${g.privilege_type}${column} ON ${objectSql(g)}`;
360
- }
361
- function describeGrant(g) {
362
- const column = g.column_name ? `(${g.column_name}) ` : "";
363
- return `${g.privilege_type} ${column}ON ${describeObject(g)} TO ${g.grantee}`;
364
- }
365
- var grantSql = (g, withOption = g.is_grantable) => `GRANT ${grantTarget(g)} TO ${granteeSql(g.grantee)}${withOption ? " WITH GRANT OPTION" : ""};`;
366
- function comparablePrivileges(sourceVersion, targetVersion) {
367
- const both = Math.min(sourceVersion || Infinity, targetVersion || Infinity);
368
- return (g) => g.privilege_type !== "MAINTAIN" || both >= 17e4;
369
- }
370
- function grouped(grants) {
371
- const groups = /* @__PURE__ */ new Map();
372
- for (const g of grants) {
373
- const key = `${grantKey(g).replace(/\.[^.]+$/, "")}.${Boolean(g.is_grantable)}`;
374
- groups.set(key, [...groups.get(key) ?? [], g]);
375
- }
376
- return [...groups.values()];
377
- }
378
- function groupTarget(gs) {
379
- const column = gs[0].column_name ? ` ("${gs[0].column_name.replace(/"/g, '""')}")` : "";
380
- return `${gs.map((g) => `${g.privilege_type}${column}`).join(", ")} ON ${objectSql(gs[0])}`;
381
- }
382
- function describeGroup(gs) {
383
- const column = gs[0].column_name ? `(${gs[0].column_name}) ` : "";
384
- return `${gs.map((g) => g.privilege_type).join(", ")} ${column}ON ${describeObject(gs[0])} TO ${gs[0].grantee}`;
385
- }
386
- var groupKey = (gs) => grantKey(gs[0]).replace(/\.[^.]+$/, "");
387
- function grantsToMissingRoles(grants) {
388
- const byRole = /* @__PURE__ */ new Map();
389
- for (const g of grants) byRole.set(g.grantee, [...byRole.get(g.grantee) ?? [], g]);
390
- return [...byRole].map(([role, gs]) => {
391
- const groups = grouped(gs);
392
- const objects = new Set(gs.map((g) => describeObject(g))).size;
393
- const what = `${gs.length} privilege${gs.length === 1 ? "" : "s"} on ${objects} object${objects === 1 ? "" : "s"}`;
394
- return {
395
- id: `roles-grant-missing-role-${role}`,
396
- check: "roles",
397
- severity: "warning",
398
- title: `Missing grants: ${what} to ${role}, a role the target does not have`,
399
- description: `The source grants ${what} to "${role}", which the target lacks. They can only be applied once the role exists (see "Missing role"); on a target that is not meant to have it, skip the roles check.`,
400
- sourceValue: gs,
401
- sql: {
402
- up: groups.map((g) => `GRANT ${groupTarget(g)} TO ${granteeSql(role)}${g[0].is_grantable ? " WITH GRANT OPTION" : ""};`).join("\n"),
403
- down: groups.map((g) => `REVOKE ${groupTarget(g)} FROM ${granteeSql(role)};`).reverse().join("\n")
404
- }
405
- };
406
- });
407
- }
408
- function diffGrants(source, target, missingRoles = /* @__PURE__ */ new Set()) {
409
- const issues = [];
410
- const sourceMap = new Map(source.map((g) => [grantKey(g), g]));
411
- const targetMap = new Map(target.map((g) => [grantKey(g), g]));
412
- const missing = [...sourceMap].filter(([key]) => !targetMap.has(key)).map(([, g]) => g);
413
- issues.push(...grantsToMissingRoles(missing.filter((g) => missingRoles.has(g.grantee))));
414
- for (const gs of grouped(missing.filter((g) => !missingRoles.has(g.grantee)))) {
415
- const plural = gs.length > 1 ? "s" : "";
416
- issues.push({
417
- id: `roles-grant-missing-${groupKey(gs)}`,
418
- check: "roles",
419
- severity: "warning",
420
- title: `Missing grant${plural}: ${describeGroup(gs)}`,
421
- description: `Grant${plural} "${describeGroup(gs)}" ${gs.length > 1 ? "are" : "is"} missing from target.`,
422
- sourceValue: gs.length > 1 ? gs : gs[0],
423
- sql: {
424
- up: `GRANT ${groupTarget(gs)} TO ${granteeSql(gs[0].grantee)}${gs[0].is_grantable ? " WITH GRANT OPTION" : ""};`,
425
- down: `REVOKE ${groupTarget(gs)} FROM ${granteeSql(gs[0].grantee)};`
426
- }
427
- });
428
- }
429
- for (const [key, sg] of sourceMap) {
430
- const tg = targetMap.get(key);
431
- if (tg && Boolean(sg.is_grantable) !== Boolean(tg.is_grantable)) {
432
- const revokeOption = `REVOKE GRANT OPTION FOR ${grantTarget(sg)} FROM ${granteeSql(sg.grantee)};`;
433
- issues.push({
434
- id: `roles-grant-option-${key}`,
435
- check: "roles",
436
- severity: "warning",
437
- title: `Grant option differs: ${describeGrant(sg)}`,
438
- description: `"${describeGrant(sg)}" is held ${sg.is_grantable ? "with" : "without"} the grant option in source and ${tg.is_grantable ? "with" : "without"} it in target.`,
439
- sourceValue: sg,
440
- targetValue: tg,
441
- sql: sg.is_grantable ? { up: grantSql(sg, true), down: revokeOption } : { up: revokeOption, down: grantSql(tg, true) }
442
- });
443
- }
444
- }
445
- const extra = [...targetMap].filter(([key]) => !sourceMap.has(key)).map(([, g]) => g);
446
- for (const gs of grouped(extra)) {
447
- const plural = gs.length > 1 ? "s" : "";
448
- issues.push({
449
- id: `roles-grant-extra-${groupKey(gs)}`,
450
- check: "roles",
451
- severity: extraGrantSeverity(gs[0].grantee),
452
- title: `Extra grant${plural}: ${describeGroup(gs)}`,
453
- description: `Grant${plural} "${describeGroup(gs)}" exist${gs.length > 1 ? "" : "s"} in target but not in source.`,
454
- targetValue: gs.length > 1 ? gs : gs[0],
455
- sql: {
456
- up: `REVOKE ${groupTarget(gs)} FROM ${granteeSql(gs[0].grantee)};`,
457
- down: `GRANT ${groupTarget(gs)} TO ${granteeSql(gs[0].grantee)}${gs[0].is_grantable ? " WITH GRANT OPTION" : ""};`
458
- }
459
- });
460
- }
461
- return issues;
462
- }
463
-
464
- export {
465
- PLATFORM_ROLES,
466
- RolesCheck
467
- };
468
- //# sourceMappingURL=chunk-WNTCZM2J.js.map