Skip to content

Conversation

@pyramation
Copy link
Collaborator

@pyramation pyramation commented Jan 1, 2026

Summary

This PR enables AST-based transformations for SQL statements inside PL/pgSQL function bodies and improves pretty-printing for INSERT statements.

plpgsql-deparser changes:

  • For sql-stmt kind: Now uses Deparser.deparse() to convert the modified AST back to SQL
  • For sql-expr kind: Still returns original string (backward compatible)
  • For assign kind: Uses target and value strings directly (backward compatible)
  • Added DehydrationOptions interface to thread SQL deparse options through

pgsql-deparser changes:

  • VALUES tuple items: Put each value on its own line when isPretty()
  • ON CONFLICT DO UPDATE SET: Put each assignment on its own line when isPretty()

Use case: Schema renaming in introspection exports - when SQL statements inside function bodies need their schema references transformed.

DehydrationOptions API

export interface DehydrationOptions {
  sqlDeparseOptions?: DeparserOptions;  // { pretty?: boolean, newline?, tab? }
}

// Usage:
dehydratePlpgsqlAst(ast, { sqlDeparseOptions: { pretty: true } });

This threads options through dehydratePlpgsqlAstdehydrateNodedehydrateQueryDeparser.deparse().

Updates since last revision

  • Rebuilt deparser and regenerated snapshot - INSERT VALUES and ON CONFLICT now properly pretty-print with each item on its own line
  • Snapshot confirms the fix is working

Review & Testing Checklist for Human

  • Verify formatting is acceptable: The deparsed output uses lowercase excluded (not EXCLUDED) and no column alignment in SET clauses - verify this is acceptable for your use case
  • Check for formatting regressions: The snapshot shows different formatting (whitespace, AS aliases, parentheses placement) compared to original SQL - review the snapshot diff carefully
  • Test schema renaming end-to-end: Run the schema renaming transform on actual exported SQL to verify schema references inside PL/pgSQL bodies are properly renamed

Recommended test plan:

  1. Run pnpm test in packages/plpgsql-deparser - all 23 tests should pass
  2. Run pnpm test in packages/deparser - verify no new failures
  3. Test the transform script in constructive-db to verify schema renaming works end-to-end

Notes

  • The sql-expr kind intentionally still returns original for backward compatibility
  • Deparse failures fall back to returning the original string (safe default)
  • DehydrationOptions is exported from the package for callers to use
  • The pretty-printing produces multi-line output but does NOT preserve original stylistic choices (alignment, casing)

Link to Devin run: https://app.devin.ai/sessions/cf99eca9a417440f98c23cd9db41555b
Requested by: Dan Lynch (@pyramation)

This change enables AST-based transformations for SQL statements inside
PL/pgSQL function bodies. When the AST is modified (e.g., schema names
renamed), the dehydration process now produces the modified SQL instead
of returning the original string.

Changes:
- Import Deparser from pgsql-deparser
- For sql-stmt kind: deparse the modified parseResult AST back to SQL
- For sql-expr kind: keep returning original string (backward compatible)
- For assign kind: use target and value strings directly

This enables use cases like schema renaming in introspection exports
where SQL statements inside function bodies need to have their schema
references transformed.
@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

…options

- Add DehydrationOptions interface with sqlDeparseOptions field
- Thread options through dehydratePlpgsqlAst -> dehydrateNode -> dehydrateQuery
- Pass sqlDeparseOptions to Deparser.deparse() for sql-stmt kinds
- Export DehydrationOptions from package index

This allows callers to control formatting (pretty printing, etc.) of
embedded SQL statements inside PL/pgSQL function bodies.
…NFLICT SET

- VALUES tuple items: put each value on its own line when isPretty()
- ON CONFLICT DO UPDATE SET: put each assignment on its own line when isPretty()

This enables proper multi-line formatting for INSERT statements with
VALUES lists and ON CONFLICT clauses when pretty mode is enabled.
@devin-ai-integration devin-ai-integration bot changed the title feat(plpgsql-deparser): deparse modified AST for sql-stmt kind feat: deparse modified AST and improve INSERT pretty-printing Jan 1, 2026
…LUES and ON CONFLICT

The snapshot now shows proper multi-line formatting for:
- VALUES tuple items: each value on its own line
- ON CONFLICT DO UPDATE SET: each assignment on its own line
@pyramation pyramation merged commit 37e4c60 into main Jan 1, 2026
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