feat: read RLS config from typed rls_settings table with api_modules fallback#1086
Merged
pyramation merged 3 commits intomainfrom May 9, 2026
Merged
feat: read RLS config from typed rls_settings table with api_modules fallback#1086pyramation merged 3 commits intomainfrom
pyramation merged 3 commits intomainfrom
Conversation
…fallback Update the server to prefer services_public.rls_settings (joined with metaschema_public.schema and metaschema_public.function to resolve names) over the legacy api_modules JSONB path. api.ts: - Add RLS_SETTINGS_SQL query that joins rls_settings with schema/function tables to resolve UUID FKs back to names - queryRlsModule now tries rls_settings by database_id first, falls back to api_modules by api_id upload.ts: - Add RLS_SETTINGS_BY_DATABASE_ID_SQL and RLS_SETTINGS_BY_DBNAME_SQL - queryRlsModuleByDatabaseId and queryRlsModuleByDbname try typed tables first with api_modules fallback - queryRlsModuleByApiId unchanged (no database_id available at that call site, falls through to api_modules) All fallbacks are wrapped in try/catch so the server gracefully degrades to api_modules if rls_settings doesn't exist yet (e.g. pre-migration DBs). Refs: constructive-planning#812
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This comment has been minimized.
This comment has been minimized.
metaschema_public.database.name is not the same as the PG database name (apis.dbname). Join through services_public.apis instead to correctly resolve dbname to database_id for the rls_settings lookup.
The new typed table queries (queryRlsSettingsByDatabaseId,
queryRlsSettingsByDbname) make an additional pool.query call before the
legacy api_modules fallback. Add mockResolvedValueOnce({ rows: [] }) for
these calls so the legacy mock data is consumed by the correct query.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Updates the GraphQL server to prefer the new typed
services_public.rls_settingstable (introduced in constructive-db PR #1075) over the legacyservices_public.api_modulesJSONB blob when resolving RLS module configuration. This is the server-side component of Phase 2 of the unified settings architecture (constructive-planning#812).Three files changed:
api.ts—queryRlsModulenow takes(pool, databaseId, apiId)and triesrls_settingsbydatabase_idfirst; falls back toapi_modulesbyapi_id.toApiStructurenow accepts an already-resolvedRlsModuleinstead of a rawRlsModuleRow.upload.ts—queryRlsModuleByDatabaseIdandqueryRlsModuleByDbnameeach try the typed table first via newqueryRlsSettingsByDatabaseId/queryRlsSettingsByDbnamehelpers.queryRlsModuleByApiIdis unchanged (no database_id available at that call site; the upload resolver tries database_id and dbname paths first anyway). TheRLS_SETTINGS_BY_DBNAME_SQLquery joins throughservices_public.apis(ona.dbname = $1) rather thanmetaschema_public.database, sinceapis.dbnameholds the actual PostgreSQL database name used by the server.upload.test.ts— Existing tests updated with additionalmockResolvedValueOnce({ rows: [] })calls for the new typed-table queries that now precede the legacyapi_modulesqueries.All new typed-table queries are wrapped in
try/catchreturningundefinedon failure, so the server gracefully degrades toapi_modulesfor databases that haven't been reprovisioned yet or where therls_settingstable doesn't exist.Review & Testing Checklist for Human
toRlsModuleFromSettingstruthy-object behavior: If anrls_settingsrow exists but all FK joins resolve toNULL(e.g. stale/orphaned row),toRlsModuleFromSettingsreturns{ authenticate: null, ... }— a truthy object that prevents fallback toapi_modules. Consider whether the converter should check that at leastauthenticateandauthenticate_schemaare non-null before returning a result.RlsModuleDatainterface: The 8-way LEFT JOIN queries selectauthenticate_schema,role_schema,authenticate,authenticate_strict,current_role,current_role_id,current_user_agent,current_ip_address. These must exactly match the fields onRlsModuleData(lines 133-142 in api.ts, mirrored in upload.ts). A mismatch produces anRlsModulewithundefinedfields instead of falling back.queryRlsSettings/queryRlsSettingsByDatabaseId/queryRlsSettingsByDbnamecatch all exceptions and returnundefinedwith no logging. This is intentional for backwards compat but means a broken query (typo, permission issue) is invisible. Consider whether alog.debugshould be added inside the catch.rls_settingsis populated, then verify the server actually reads from the typed table (not just silently falling back). One way: temporarily add a log line inqueryRlsSettingson success, provision a DB, and confirm the log fires.Notes
queryRlsModuleByApiIdinupload.tsintentionally has no typed-table path — inresolveUploadRlsModule, the database_id and dbname paths (which do use typed tables) are tried first;apiIdis the last resort.RLS_SETTINGS_BY_DBNAME_SQLjoins throughservices_public.apis(notmetaschema_public.database) becauseapis.dbnameis the PostgreSQL database name the server uses at runtime, whereasmetaschema_public.database.nameis a display name.rls_settings,pubkey_settings,webauthn_settings).Link to Devin session: https://app.devin.ai/sessions/94a2728a9c414500bead29cbbc829c15
Requested by: @pyramation