Skip to content

Comments

fix: normalize view definitions to strip same-schema qualifiers (#314)#316

Merged
tianzhou merged 1 commit intomainfrom
fix/issue-314-view-schema-qualification
Feb 23, 2026
Merged

fix: normalize view definitions to strip same-schema qualifiers (#314)#316
tianzhou merged 1 commit intomainfrom
fix/issue-314-view-schema-qualification

Conversation

@tianzhou
Copy link
Contributor

Summary

  • pgschema plan was reporting the same view change on every run when extension functions (e.g., ltree's nlevel()) appeared with different schema qualification between desired and current state (nlevel() vs public.nlevel())
  • Applied the existing stripSchemaPrefixFromBody() normalization to view definitions, matching what already happens for function and procedure bodies
  • This ensures view definitions compare as equal when the only difference is same-schema qualification

Fixes #314

Test plan

  • Unit test TestNormalizeViewStripsSchemaPrefixFromDefinition added with 6 cases covering: same-schema function stripping, cross-schema preservation, table references, no-op, multiple occurrences, string literal safety
  • All existing view diff tests pass: PGSCHEMA_TEST_FILTER="create_view/" go test -v ./internal/diff -run TestDiffFromFiles
  • All existing view integration tests pass: PGSCHEMA_TEST_FILTER="create_view/" go test -v ./cmd -run TestPlanAndApply
  • All materialized view tests pass
  • Full IR package tests pass: go test -v ./ir

🤖 Generated with Claude Code

pg_get_viewdef() can produce different schema qualification for extension
functions depending on search_path differences between embedded postgres
(desired state) and target database (current state). For example, ltree's
nlevel() might appear as "public.nlevel()" on one side and "nlevel()" on
the other, causing an infinite plan/apply loop.

Apply the existing stripSchemaPrefixFromBody() to view definitions during
normalization, matching what already happens for function and procedure
bodies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 23, 2026 15:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes non-converging diffs for views where pg_get_viewdef() renders same-schema references inconsistently (e.g., nlevel() vs public.nlevel()), by normalizing view definitions to strip same-schema qualifiers similarly to existing function/procedure normalization.

Changes:

  • Apply stripSchemaPrefixFromBody() normalization to View.Definition during IR normalization.
  • Update normalizeView documentation to reflect real-world pg_get_viewdef() qualification differences (Issue #314).
  • Add unit test coverage validating stripping behavior across several view-definition cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
ir/normalize.go Normalizes view definitions by stripping same-schema qualifiers to prevent repeated view diffs.
ir/normalize_test.go Adds unit tests ensuring view definition normalization strips only same-schema qualifiers and preserves cross-schema/literals.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@greptile-apps
Copy link

greptile-apps bot commented Feb 23, 2026

Greptile Summary

Applies existing schema prefix normalization logic to view definitions to fix false positives in schema comparison. Previously, normalizeView() did no normalization, assuming both desired and current states would be identical from pg_get_viewdef(). However, extension functions (like ltree's nlevel()) and search_path differences can cause one side to produce public.func() while the other produces func(), causing pgschema plan to report spurious diffs on every run.

The fix reuses the battle-tested stripSchemaPrefixFromBody() function that was already being used for function and procedure body normalization. This function:

  • Strips same-schema qualifiers (e.g., public.nlevel()nlevel() when schema is "public")
  • Preserves cross-schema qualifiers (e.g., other_schema.func() remains unchanged)
  • Correctly handles string literals containing schema names
  • Works for both function calls and table references

Key changes:

  • Updated normalizeView() to call stripSchemaPrefixFromBody(view.Definition, view.Schema)
  • Added unit test TestNormalizeViewStripsSchemaPrefixFromDefinition with 6 comprehensive test cases
  • Updated function comments to explain the schema qualification issue

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The change reuses an existing, well-tested normalization function (stripSchemaPrefixFromBody) that is already used for function and procedure bodies. The fix is minimal (one line of actual code change), addresses a specific reported issue (plan keeps reporting the same view change after apply #314), includes comprehensive unit tests with 6 test cases covering various edge cases, and passes all existing view-related integration tests according to the test plan
  • No files require special attention

Important Files Changed

Filename Overview
ir/normalize.go Applied existing stripSchemaPrefixFromBody() normalization to view definitions to fix schema qualification differences
ir/normalize_test.go Added comprehensive test coverage with 6 test cases for view normalization including edge cases

Last reviewed commit: de06964

@tianzhou tianzhou merged commit 7397729 into main Feb 23, 2026
6 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.

plan keeps reporting the same view change after apply

1 participant