Skip to content

Conversation

@pyramation
Copy link
Collaborator

@pyramation pyramation commented Dec 31, 2025

feat: add plpgsql-parser package for combined SQL + PL/pgSQL parsing

Summary

Adds a new plpgsql-parser package that provides a unified API for parsing SQL scripts containing PL/pgSQL functions. The package combines the SQL parser and PL/pgSQL parser, automatically detecting and hydrating PL/pgSQL function bodies.

Key features:

  • parse(sql) - Auto-detects CREATE FUNCTION statements with LANGUAGE plpgsql and hydrates them
  • transform(sql, callback) - Pipeline for parse → modify → deparse workflows
  • deparseSync(parsed) - Converts modified AST back to SQL with pretty printing
  • Re-exports underlying primitives (parseSql, parsePlpgsqlBody, deparseSql, etc.) for power users

Updates since last revision

  • Updated README.md with pretty headers (logo, CI badge, license badge, version badge) to match plpgsql-deparser style
  • Added experimental warning with links to related packages (pgsql-parser, plpgsql-deparser)

Review & Testing Checklist for Human

  • Verify workspace dependencies for publishing - The package uses workspace:* for pgsql-deparser and plpgsql-deparser. Confirm the publish pipeline rewrites these to actual version numbers.
  • Test with multiple PL/pgSQL functions - The current tests only cover single-function scenarios. Verify behavior when a script contains multiple CREATE FUNCTION statements.
  • Review any type usage - PlpgsqlFunctionData.hydrated is typed as any (types.ts:15). Consider if stronger typing is needed for downstream consumers.
  • Test async transform() function - Only transformSync is tested; the async variant should be verified manually.
  • Verify error handling - extractFunctionInfo silently returns null on parse errors (parse.ts:90-92). Confirm this behavior is acceptable vs surfacing errors.

Recommended test plan:

  1. Run npm test in packages/plpgsql-parser - all 7 tests should pass
  2. Try parsing a script with 2+ PL/pgSQL functions and verify both are detected
  3. Try the transform API to rename a function and verify the output SQL is correct
  4. Build the package with npm run build and verify dist output looks correct

Notes

  • This is a thin orchestration layer that composes existing packages (@libpg-query/parser, pgsql-deparser, plpgsql-deparser)
  • The deparseSync function uses require() inside the function body (deparse.ts:85) to avoid import conflicts - this is intentional but worth noting
  • Requested by Dan Lynch (@pyramation)
  • Link to Devin run: https://app.devin.ai/sessions/50192c7bb0454b26845853867bd795ce

- Combined parse function that auto-detects PL/pgSQL functions and hydrates them
- Transform API for parse -> modify -> deparse pipeline
- Deparse function that handles dehydration and stitching
- Re-exports underlying primitives for power users
- 7 tests passing
@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

@pyramation pyramation merged commit 219df51 into main Dec 31, 2025
14 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