feat(pgpm): object-level apply routing (route a table and a function to different schemas) - #1514
Merged
Merged
Conversation
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
Extends
pgpm.apply.jsonproxy modules with object-level routing: a single source schema's objects can now fan out to different target schemas — e.g. a table toshop_aand a function toanalytics_a— instead of the whole schema going to one place. Targets are expressed as individual properties (no dotted identity strings), and cross-object references inside function bodies are rewritten to follow each object to its new home.This consumes the upstream object-routing primitive shipped in
@pgsql/transform@18.4.1(constructive-io/pgsql-parser#319) — theSchemaRoutermodel resolving a target per(schema, name, namespace). This PR bumps the dep and wires it through the pgpm apply path. Backwards compatible: the existing whole-schemaschemasmap is unchanged.Spec syntax (
pgpm.apply.json){ "source": "catalog-module", "schemas": { "catalog": "shop_a", "reporting": "analytics_a" }, "route": [ { "fromSchema": "catalog", "kind": "function", "name": "product_count", "toSchema": "analytics_a" } ] }schemas— whole-schema default (now optional; at least one ofschemas/routerequired).route[]— per-object overrides.kind∈table | view | function | procedure | typeselects the routing namespace, so a table and a function of the same name route independently. Each route wins over the schema default for its object.How it works
makeSchemaTranspilernow builds a singleSchemaRouter(fromschemaMap+routes) that drives both dimensions in lockstep:Change identity (
renameChange): the object is read straight from the pgpm pathschemas/<schema>/<kind>/<name>and its schema segment rewritten to the routed target —schemas/catalog/procedures/product_count→schemas/analytics_a/procedures/product_countwhileschemas/catalog/tables/products/table→schemas/shop_a/tables/products/table.SQL bodies (
transformScript):transformSql(sql, router, …)moves each definition to its target and rewrites cross-object refs, soCREATE FUNCTION catalog.product_count() … SELECT count(*) FROM catalog.productsbecomes:(definition follows the function route; the
productsreference follows the table's schema-level target).Destinations that may or may not exist
Apply deploys into consumer-chosen namespaces that can pre-exist (a shared/reporting schema, another tenant). Every distinct target schema is passed to the transform's
assumeSchemasExist, soCREATE SCHEMAis emitted idempotently (CREATE SCHEMA IF NOT EXISTS) — the same source applies cleanly whether or not the destination already exists.Files
pgpm/transform/src/bundle-driver.ts—SchemaObjectRoute,buildSchemaRouter, router-drivenrenameChange/transformScript;schemaMapnow optional.pgpm/core/src/apply/{types,apply-spec}.ts—routefield + validation (kinds, non-empty, at-least-one-of).pgpm/core/src/apply/materialize.ts— build router fromschemas+route; idempotent target-schema creation.pgpm/transform/package.json+ lockfile — bump@pgsql/transformto^18.4.1.__fixtures__/apply/routing/*+pgpm/core/__tests__/apply/apply-routing.test.ts— acatalog-modulesource (schemascatalog+reporting) applied as two instances that route the table to the schema-level target and the function to the reporting-derived target.Tests
materializeApplyModule: deploy-order rename, cross-schema body rewrite, idempotent schema DDL.shop_a/shop_b, function inanalytics_a/analytics_b; a pre-existing destination schema deploys without error; per-instance counts are independent; verify + independent revert.@pgpmjs/transform(26) +@pgpmjs/coreapply suite (23) pass; typecheck clean for both. Repo-widepnpm lintis a pre-existing failure (ESLint 9 expectseslint.config.js; repo has.eslintrc.json) and is not run in CI.Follow-up (not in this PR)
Object reuse/dedup across instances (a shared, tenant-independent helper deployed once) is deliberately out of scope: a table-dependent function cannot be shared across tenants, and safe shared-object revert needs reference counting. The durable model is shared-module decomposition (a dedicated pgpm module both instances
require) rather thanCREATE OR REPLACEemission. Tracked in constructive-io/constructive-planning#1298.Link to Devin session: https://app.devin.ai/sessions/025fb88043964fdbb335ac5e39df2478
Requested by: @pyramation