Skip to content

Conversation

@pyramation
Copy link
Collaborator

@pyramation pyramation commented Jan 6, 2026

test(plpgsql-deparser): add schema transform demo test with fixture-based snapshots

Summary

Adds demonstration tests showing how to use the heterogeneous AST transformation pipeline for schema renaming in PL/pgSQL functions. The tests demonstrate the full pipeline:

  1. Parse SQL containing PL/pgSQL functions
  2. Hydrate embedded SQL expressions into AST nodes
  3. Traverse and transform schema names in both outer SQL AST and embedded SQL inside function bodies
  4. Dehydrate back to strings
  5. Deparse to final SQL output

Two test files are included:

  1. schema-transform.demo.test.ts - 4 inline test cases covering common patterns (simple SELECT, trigger INSERT, RETURN QUERY, function calls in expressions)

  2. schema-rename-mapped.test.ts - Fixture-based snapshot test that:

    • Reads from __fixtures__/plpgsql/plpgsql_schema_rename.sql (12 complex procedures)
    • Tracks all schema references found during traversal in a rename map
    • Snapshots both the rename map AND the final transformed SQL
    • Covers: CTEs, subqueries, UNION queries, cross-schema references, procedures, triggers, SETOF functions

The rename map snapshot shows 41 total references across 3 schemas:

  • app_public: 39 references (functions, tables, relations, func_calls)
  • app_private: 1 reference
  • app_internal: 1 reference

Review & Testing Checklist for Human

  • Verify the transform functions handle all relevant AST node types (RangeVar, TypeName, FuncCall, CreateFunctionStmt, direct relation references). There may be other schema-qualified references not covered (sequences, views, operators, etc.)
  • Review whether manual recursion is appropriate for this demo vs using existing walk() APIs - the demo uses manual recursion for clarity but production code may want to use the existing infrastructure
  • Check that the regex assertion expect(output).not.toMatch(/\bapp_public\./) is the right approach for avoiding substring false positives (e.g., "myapp_public" contains "app_public")
  • Verify the 12 fixture procedures all exercise distinct transformation scenarios

Test plan:

cd packages/plpgsql-deparser
npx jest __tests__/schema-transform.demo.test.ts  # 4 inline tests
npx jest __tests__/schema-rename-mapped.test.ts   # fixture-based snapshot test

Notes

  • Schema-qualified custom types in DECLARE blocks are not tested due to PL/pgSQL parser limitations
  • This is a demonstration/documentation test showing the pattern, not a production-ready schema transformer
  • The fixture file is included in __fixtures__/plpgsql/ so it can be incorporated into generated tests
  • Requested by: Dan Lynch (@pyramation)
  • Link to Devin run: https://app.devin.ai/sessions/8e1c971e9b194cd9a7dda034c89bd74b

Demonstrates the heterogeneous AST transformation pipeline:
- Parse SQL containing PL/pgSQL functions
- Hydrate embedded SQL expressions into AST nodes
- Traverse and transform schema names in both outer SQL AST and embedded SQL
- Dehydrate back to strings
- Deparse to final SQL output

Includes 4 test cases:
- Simple function with schema-qualified table reference
- Trigger function with INSERT into schema-qualified table
- RETURN QUERY function with schema-qualified table
- Function calls inside PL/pgSQL expressions
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Adds comprehensive schema rename testing with:
- SQL fixture file with 12 complex procedures covering various patterns
  (SELECT, INSERT, UPDATE, DELETE, RETURN QUERY, CTEs, subqueries, etc.)
- Snapshot test that tracks all schema references found during traversal
- Snapshot of final transformed SQL output

The rename map snapshot shows 41 total references across 3 schemas:
- app_public: 39 references (functions, tables, relations, func_calls)
- app_private: 1 reference
- app_internal: 1 reference

All references are correctly transformed in the output SQL.
@devin-ai-integration devin-ai-integration bot changed the title test(plpgsql-deparser): add schema transform demo test test(plpgsql-deparser): add schema transform demo test with fixture-based snapshots Jan 6, 2026
@pyramation pyramation merged commit 39b0985 into main Jan 6, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants