Skip to content

fix: spurious plan changes for column defaults with schema-qualified functions (#283)#290

Merged
tianzhou merged 4 commits intomainfrom
fix/issue-283-function-default-schema-qualifier
Feb 15, 2026
Merged

fix: spurious plan changes for column defaults with schema-qualified functions (#283)#290
tianzhou merged 4 commits intomainfrom
fix/issue-283-function-default-schema-qualifier

Conversation

@tianzhou
Copy link
Contributor

@tianzhou tianzhou commented Feb 15, 2026

Summary

  • Fixed spurious ALTER TABLE ... ALTER COLUMN ... SET DEFAULT changes when using pgschema plan with column defaults that reference functions in the target schema (e.g., uuid_generate_v1mc())
  • Root cause: normalizeIR first ran with temporary schema names (pgschema_tmp_*), so same-schema qualifier stripping couldn't match the public. prefix. After normalizeSchemaNames renamed temp→target, the qualifiers were already baked in
  • Fix: added newSameSchemaQualifierStripper in normalizeSchemaNames that strips redundant same-schema function/type qualifiers from expression fields (column defaults, generated expressions, check clauses, policy expressions, trigger conditions) after replacing temp schema names with the target schema

Fixes #283

Test plan

  • Integration test issue_283_function_default_schema_qualifier validates the full plan workflow with function defaults produces no spurious changes
  • Existing dependency_table_fk_to_generated_column test updated — generated column expressions now correctly omit redundant public. qualifier
  • Full test suite passes (no regressions)
# Run the regression tests
PGSCHEMA_TEST_FILTER="create_table/issue_283" go test -v ./cmd -run TestPlanAndApply
PGSCHEMA_TEST_FILTER="dependency/table_fk_to_generated_column" go test -v ./cmd -run TestPlanAndApply

🤖 Generated with Claude Code

…functions (#283)

When using temporary schemas for plan generation, column defaults referencing
functions in the target schema (e.g., public.uuid_generate_v1mc()) were not
properly normalized, causing pgschema to detect changes where none existed.

The root cause: normalizeIR ran with temp schema names (pgschema_tmp_*), so
same-schema qualifier stripping couldn't match the target schema prefix. After
normalizeSchemaNames renamed temp→target, re-running normalization with the
correct schema context fixes the mismatch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 15, 2026 16:54
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

This pull request fixes a bug where pgschema plan would generate spurious ALTER TABLE statements for column defaults that reference functions in the target schema. The issue occurred because IR normalization ran with temporary schema names before they were replaced with target schema names, preventing same-schema qualifier stripping from working correctly.

Changes:

  • Exported NormalizeIR function to allow re-running normalization after schema name replacement
  • Added call to re-run ir.NormalizeIR() after normalizeSchemaNames() in plan generation
  • Added comprehensive test coverage including unit tests and integration test

Reviewed changes

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

File Description
ir/normalize.go Exported NormalizeIR function with documentation explaining idempotency and the need to re-run after schema renaming
ir/normalize_test.go Added unit tests validating normalization behavior with temp schemas and the re-run mechanism
cmd/plan/plan.go Added call to re-run ir.NormalizeIR() after schema name replacement with detailed explanatory comments
testdata/diff/create_table/issue_283_function_default_schema_qualifier/* Added integration test data validating the full plan workflow shows no spurious changes

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

tianzhou and others added 3 commits February 15, 2026 09:17
…re-running normalizeIR (#283)

Move the fix for issue #283 into normalizeSchemaNames itself: after replacing
temp schema names with the target schema, strip redundant same-schema function
and type cast qualifiers from expression fields (column defaults, generated
expressions, check clauses, policy expressions, trigger conditions). This is
more targeted than re-running the full normalizeIR pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ifier stripping

The table_fk_to_generated_column test fixture had "public.calc_priority()" in
its expected plan output. With the same-schema qualifier stripping fix, the
correct output is now "calc_priority()" without the redundant qualifier.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Integration tests (issue_283_function_default_schema_qualifier and
dependency_table_fk_to_generated_column) already cover the end-to-end
behavior. The unit tests for normalizeDefaultValue and normalizeSchemaNames
were testing pre-existing or already-covered behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tianzhou tianzhou merged commit 29c1b67 into main Feb 15, 2026
1 check 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 wants to change a column that hasn't changed.

1 participant