|
| 1 | +import type { GraphileConfig } from 'graphile-config'; |
| 2 | +import { gatherConfig } from 'graphile-build'; |
| 3 | + |
| 4 | +const TABLE_LIKE_RELKINDS = new Set(['r', 'v', 'm', 'f', 'p']); |
| 5 | + |
| 6 | +/** |
| 7 | + * Ensures procedure resources never collide with table/view resource names. |
| 8 | + * |
| 9 | + * Graphile v5 registers both table resources and function resources in the same |
| 10 | + * registry namespace. If a function and a table resolve to the same resource |
| 11 | + * name (e.g. both "table_grant"), schema build fails with p2rc. |
| 12 | + * |
| 13 | + * This plugin renames only the function resource when that collision occurs, |
| 14 | + * preserving table resource names. |
| 15 | + */ |
| 16 | +export const ProcedureResourceNameConflictPlugin: GraphileConfig.Plugin = { |
| 17 | + name: 'ProcedureResourceNameConflictPlugin', |
| 18 | + version: '1.0.0', |
| 19 | + description: |
| 20 | + 'Renames colliding procedure resources to avoid table/procedure name conflicts', |
| 21 | + gather: gatherConfig({ |
| 22 | + hooks: { |
| 23 | + async pgProcedures_PgResourceOptions(info, event) { |
| 24 | + const { serviceName, pgProc, resourceOptions } = event; |
| 25 | + const pgService = info.resolvedPreset.pgServices?.find( |
| 26 | + (svc) => svc.name === serviceName, |
| 27 | + ); |
| 28 | + if (!pgService) return; |
| 29 | + |
| 30 | + const schemas = pgService.schemas ?? ['public']; |
| 31 | + const introspectedService = |
| 32 | + await info.helpers.pgIntrospection.getService(serviceName); |
| 33 | + const tableResourceNames = new Set<string>(); |
| 34 | + |
| 35 | + for (const pgClass of introspectedService.introspection.classes) { |
| 36 | + const namespace = pgClass.getNamespace()?.nspname; |
| 37 | + if (!namespace || !schemas.includes(namespace)) continue; |
| 38 | + if (!TABLE_LIKE_RELKINDS.has(pgClass.relkind)) continue; |
| 39 | + tableResourceNames.add( |
| 40 | + info.inflection.tableResourceName({ serviceName, pgClass }), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + const originalName = resourceOptions.name; |
| 45 | + if (!tableResourceNames.has(originalName)) return; |
| 46 | + |
| 47 | + const registryBuilder = await info.helpers.pgRegistry.getRegistryBuilder(); |
| 48 | + const existingResourceNames = new Set( |
| 49 | + Object.keys(registryBuilder.getRegistryConfig().pgResources), |
| 50 | + ); |
| 51 | + |
| 52 | + const arity = |
| 53 | + typeof pgProc.pronargs === 'number' |
| 54 | + ? pgProc.pronargs |
| 55 | + : (resourceOptions.parameters?.length ?? 0); |
| 56 | + |
| 57 | + const baseName = `${originalName}__proc${arity}`; |
| 58 | + let candidate = baseName; |
| 59 | + let index = 2; |
| 60 | + while ( |
| 61 | + tableResourceNames.has(candidate) || |
| 62 | + existingResourceNames.has(candidate) |
| 63 | + ) { |
| 64 | + candidate = `${baseName}_${index++}`; |
| 65 | + } |
| 66 | + |
| 67 | + resourceOptions.name = candidate; |
| 68 | + resourceOptions.extensions = |
| 69 | + resourceOptions.extensions ?? Object.create(null); |
| 70 | + (resourceOptions.extensions as unknown as Record<string, unknown>) |
| 71 | + .procedureResourceNameCollision = { |
| 72 | + originalName, |
| 73 | + renamedTo: candidate, |
| 74 | + }; |
| 75 | + |
| 76 | + console.warn( |
| 77 | + `[ProcedureResourceNameConflictPlugin] Renamed procedure resource '${originalName}' to '${candidate}' to avoid a table/view conflict.`, |
| 78 | + ); |
| 79 | + }, |
| 80 | + }, |
| 81 | + }), |
| 82 | +}; |
| 83 | + |
| 84 | +export const ProcedureResourceNameConflictPreset: GraphileConfig.Preset = { |
| 85 | + plugins: [ProcedureResourceNameConflictPlugin], |
| 86 | +}; |
0 commit comments