feat(transform): extension + role routing (portable installs, symbol qualification, role renaming) - #321
Merged
Merged
Conversation
…symbol qualification, role renaming)
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 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
Adds two new routing dimensions to
@pgsql/transform, alongside the existingSchemaRouter, so SQL can be made portable across databases that install extensions into different schemas or use different role names. This is the first transform that changes AST shape — constructing/removing nodes — not just rewriting string fields on existing nodes.Everything is platform-agnostic (no product/platform names): callers supply the route spec and (for extensions) a symbol inventory.
1. Extension classification (
facts.ts)New
StatementKind: 'extension'+ExtensionFact { name, schema, action, ifNotExists? }onStatementFacts, recognizing the three distinct node forms:2.
ExtensionRouter(extension-router.ts)A bare
crypt(...)gives no hint it belongs topgcrypto, so routing extension symbols needs an inventory of which symbols each extension provides. The inventory is version-aware: a symbol can graduate into core and stop being extension-owned —gen_random_uuid()is pgcrypto's before PG13 and corepg_catalogfrom 13 on, so it must never be routed on 13+.nulldenotes the unqualified site (bare reference / noSCHEMAclause), making the router fully bidirectional (null -> 'extensions'qualifies,'extensions' -> nullstrips).fixedSchemaextensions (control-file pinned, non-relocatable) are refused.COMMON_EXTENSIONSships a curated, conservative starter inventory (pgcrypto, uuid-ossp, citext, pg_trgm, ltree, hstore, unaccent); callers can extend/replace it.3. Extension transform (
extension-transform.ts) — the node-construction partRewrites references in plain statements,
LANGUAGE sqlbodies, and hydrated PL/pgSQL bodies (mirrors thequalify.tsmachinery). Symbols not in the inventory (app.crypt,my_helper()) and core-graduated symbols are left untouched.4. Role routing (
role-router.ts,role-transform.ts)RoleRouterrenames role identifiers only — deliberately not role attributes (BYPASSRLS,LOGIN, ...): two roles sharing a name across conventions don't necessarily share privilege scope, so fabricating attribute changes would misrepresent the security model. A rename is pure substitution, soRoleRouter.invert()gives bidirectional translation.Most role positions flow through one
RoleSpecnode (grantees,OWNER TO,CREATE POLICY ... TO,ALTER DEFAULT PRIVILEGES [FOR ROLE] ... TO,ALTER ROLE,DROP ROLE, membership grantees,GRANTED BY,REASSIGN OWNED BY). Four carry a name outside aRoleSpecand are handled explicitly:CREATE ROLE(string),GRANT <role> TOgranted roles (AccessPriv.priv_name),SET ROLE/SET SESSION AUTHORIZATION(string const),ALTER ROLE ... RENAME TO.PUBLIC/CURRENT_USER/SESSION_USER(non-CSTRINGroletype) are never rewritten.Testing
219 transform tests pass (160 pre-existing + 59 new across
facts,extension-router,extension-transform,role-transform). New tests include parse → deparse → parse round-trip assertions on every transformed form, covering the newly constructed/removed nodes.npm run build(CJS+ESM) +tsc --noEmitclean.Note:
npm run lintfails repo-wide with the pre-existing ESLint 9 vs legacy.eslintrcincompatibility (not introduced here; unchanged from prior merged PRs).Follow-ups (not in this PR)
ALTER EXTENSION ... SET SCHEMAbehavior.Link to Devin session: https://app.devin.ai/sessions/025fb88043964fdbb335ac5e39df2478
Requested by: @pyramation