feat: add sql-schema-transform cloud function #604
Closed
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 a new Knative cloud function that transforms schema names in SQL using AST-based parsing with
plpgsql-parser. The function accepts SQL and a schema mapping, then returns the transformed SQL with schema names replaced according to the mapping.The transformation logic is adapted from
@constructive-db/introspection'stransform-schemas-ast.tsand handles:API:
Updates since last revision
Fixed a bug where DDL statements like
CREATE TABLE old_schema.userswere not being transformed. The@pgsql/traversewalker looks for nodes by their key name (e.g.,RangeVar), but DDL statements likeCreateStmtstore their relation directly without theRangeVarwrapper. Added explicit handlers for CreateStmt, IndexStmt, AlterTableStmt, TruncateStmt, CreateTrigStmt, RuleStmt, and CreatePolicyStmt.Local testing confirmed the following transformations now work:
CREATE SCHEMA old_schema→CREATE SCHEMA new_schemaSET search_path TO old_schema→SET search_path TO new_schemaCREATE TABLE old_schema.users→CREATE TABLE new_schema.usersGRANT USAGE ON SCHEMA old_schema→GRANT USAGE ON SCHEMA new_schemaSELECT * FROM old_schema.users→SELECT * FROM new_schema.usersReview & Testing Checklist for Human
transform-schemas-ast.tsin constructive-dbRecommended test plan:
cd functions/sql-schema-transform && pnpm build && PORT=8080 node dist/index.jsNotes
anytypes for AST nodes which is consistent with the source code but reduces type safetyRequested by: @pyramation
Link to Devin run: https://app.devin.ai/sessions/2eed6a2d4f0142349bb3bdd50334b23f