Skip to content

Schema Diff: stop injecting whitespace into applied function/procedure bodies - #10317

Open
dpage wants to merge 1 commit into
pgadmin-org:masterfrom
dpage:fix/10302-function-body-whitespace-diff
Open

Schema Diff: stop injecting whitespace into applied function/procedure bodies#10317
dpage wants to merge 1 commit into
pgadmin-org:masterfrom
dpage:fix/10302-function-body-whitespace-diff

Conversation

@dpage

@dpage dpage commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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 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.

Schema Diff's update path (CREATE OR REPLACE against an existing target object) already suppressed this formatting via an allow_code_formatting flag, added for #7102, but that flag was never threaded through the create/"Source Only" path.

The fix

Extends the existing allow_code_formatting guard to sql() itself, and passes allow_code_formatting=False from the get_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_diff passes against PostgreSQL 18; pycodestyle is clean.

Fixes #10302.

Summary by CodeRabbit

  • Bug Fixes

    • Preserved function and procedure body whitespace in schema-diff-generated SQL.
    • Prevented unwanted leading or trailing newlines around stored code bodies.
    • Ensured source-only schema differences can be applied and subsequently compare as identical.
  • Tests

    • Added regression coverage for whitespace preservation in function and procedure definitions.

…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".
@coderabbitai

coderabbitai Bot commented Aug 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c32aa91-c684-48c7-9a4d-385e45abb34e

📥 Commits

Reviewing files that changed from the base of the PR and between 0ebefaf and bdfecb9.

📒 Files selected for processing (2)
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
  • web/pgadmin/tools/schema_diff/tests/test_schema_diff_function_body.py

Included review availability: Your plan provides up to 8 included reviews per hour; 2 remain after this review.


Walkthrough

Schema Diff SQL generation now accepts an allow_code_formatting option. Target-schema SQL disables formatting to preserve stored function and procedure bodies. A regression test verifies that applying generated SQL produces identical comparisons without ignoring whitespace.

Changes

Schema Diff body formatting

Layer / File(s) Summary
Conditional SQL source formatting
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
SQL generation defaults to code formatting but skips reformatting when disabled. Schema Diff target SQL disables formatting.
End-to-end whitespace regression coverage
web/pgadmin/tools/schema_diff/tests/test_schema_diff_function_body.py
The test covers source-only and differing functions and procedures, applies generated DDL, and verifies identical comparisons without whitespace ignoring.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to bdfec

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: preventing whitespace injection into applied function and procedure bodies.
Linked Issues check ✅ Passed The implementation and regression test address issue #10302 by preserving function and procedure body whitespace during Schema Diff application.
Out of Scope Changes check ✅ Passed The changes are limited to SQL formatting control and regression coverage directly related to issue #10302.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Schema Diff: applying a function or procedure change leaves a whitespace-only difference behind

1 participant