Schema Diff: stop injecting whitespace into applied function/procedure bodies - #10317
Conversation
…edure bodies (pgadmin-org#10302) Generating a script for a function or procedure that only exists on the source ("Source Only") routed through FunctionView.sql(), which always calls reformat_prosrc_code() to wrap the body with a leading and trailing newline around $BODY$...$BODY$ purely for readability in the SQL panel. That reformatted body was then executed verbatim against the target, so PostgreSQL stored the extra newlines in prosrc: the target's body no longer matched the source's byte for byte, and re-comparing kept reporting a whitespace-only difference unless "Ignore whitespace" was ticked. Schema Diff's update path (CREATE OR REPLACE against an existing target object) already suppressed this formatting via allow_code_formatting, added for pgadmin-org#7102; this extends the same guard to the create/"Source Only" path so Schema Diff never adds whitespace of its own to the SQL it actually applies. Added a dedicated regression test covering both the "Source Only" and "Different" cases for a function and a procedure: applying the generated script now leaves every object comparing as Identical without needing "Ignore whitespace".
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (1)
WalkthroughSchema Diff SQL generation now preserves stored function and procedure body whitespace. Function edit-mode SQL also handles missing change data and rejects unsupported added arguments. Regression tests verify identical comparisons after applying generated SQL. ChangesSchema Diff body formatting and function edit validation
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: ⚪ Minimal · up to This localized change prevents formatting-only differences when applying function and procedure definitions, with dedicated regression coverage. No actionable merge-blocking risk remains beyond normal checks and review. Sequence Diagram(s)sequenceDiagram
participant SchemaDiffTest
participant SchemaDiff
participant FunctionSQLGenerator
participant TargetDatabase
SchemaDiffTest->>SchemaDiff: generate routine diff SQL
SchemaDiff->>FunctionSQLGenerator: disable code formatting
FunctionSQLGenerator-->>SchemaDiff: return SQL with stored body whitespace
SchemaDiff->>TargetDatabase: apply generated DDL
SchemaDiffTest->>SchemaDiff: compare source and target routines
SchemaDiff-->>SchemaDiffTest: report identical without ignored whitespace
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
hiteshjambhale
left a comment
There was a problem hiding this comment.
Tested this hands-on. Created a source-only function with a spaced body, ran Schema Diff, and checked the generated script — the body now stays exactly as written (
also applied it to the target and the stored body matched the source exactly, so a re-compare shows Identical without ticking "Ignore whitespace"
Looks good to me.
What this is
Applying a Schema Diff script that creates a function or procedure that only exists on the source ("Source Only") left a whitespace-only difference behind, so re-comparing the two databases kept reporting the object as different unless "Ignore whitespace" was ticked.
The "Source Only" path routed through
FunctionView.sql(), which always callsreformat_prosrc_code()to wrap the body with a leading and trailing newline around$BODY$...$BODY$purely for readability in the SQL panel. That reformatted body was then executed verbatim against the target, so PostgreSQL stored the extra newlines inprosrc: the target's body no longer matched the source's byte for byte.Schema Diff's update path (
CREATE OR REPLACEagainst an existing target object) already suppressed this formatting via anallow_code_formattingflag, added for #7102, but that flag was never threaded through the create/"Source Only" path.The fix
Extends the existing
allow_code_formattingguard tosql()itself, and passesallow_code_formatting=Falsefrom theget_sql_from_diff()branch that builds the DDL actually applied to the target.Testing
Added a dedicated regression test covering both the "Source Only" and "Different" cases for a function and a procedure: applying the generated script now leaves every object comparing as Identical without needing "Ignore whitespace".
tools.schema_diffpasses against PostgreSQL 18;pycodestyleis clean.Fixes #10302.
Summary by CodeRabbit
Bug Fixes
Tests