Skip to content

Fix unnecessary ALTER for ENUM type default values in psqldef#1135

Merged
gfx merged 3 commits intomasterfrom
fix/enum-default-unnecessary-diff
Feb 15, 2026
Merged

Fix unnecessary ALTER for ENUM type default values in psqldef#1135
gfx merged 3 commits intomasterfrom
fix/enum-default-unnecessary-diff

Conversation

@178inaba
Copy link
Contributor

What

psqldef generates unnecessary ALTER TABLE ... SET DEFAULT statements for columns with ENUM (or domain) type defaults on every run, even when the schema is already up to date.

Why

PostgreSQL internally stores ENUM column defaults with an explicit type cast (e.g., 'pending'::order_status), while users typically write DEFAULT 'pending' without the cast in their schema files. The existing normalizeExpr handles built-in type casts (text, integer, date, etc.) but not custom types like ENUMs and domains, causing the two representations to be compared as different strings.

How

Added unwrapCast() to strip remaining type casts after normalizeExpr when comparing default values in areSameDefaultValue(). This ensures that 'pending'::order_status and 'pending' are correctly recognized as equivalent.

PostgreSQL stores ENUM column defaults with explicit type casts
(e.g., 'pending'::order_status), but users write DEFAULT 'pending'
without the cast. These were compared as different strings, causing
psqldef to generate ALTER TABLE SET DEFAULT on every run.

Strip identity casts (where the cast type matches the column type)
before comparing default values.
Remove the column type check since this comparison is always for the
same column, and PostgreSQL rejects casts to incompatible types.
@178inaba 178inaba self-assigned this Feb 15, 2026
@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
schema/generator.go 81.02% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gfx gfx added this pull request to the merge queue Feb 15, 2026
Merged via the queue into master with commit f3434d6 Feb 15, 2026
26 checks passed
@gfx gfx deleted the fix/enum-default-unnecessary-diff branch February 15, 2026 11:28
@sqldef-bot sqldef-bot bot mentioned this pull request Feb 15, 2026
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