Skip to content

Comments

fix: skip COMMENT cleanup for dropped indexes in psqldef#1064

Merged
gfx merged 1 commit intosqldef:masterfrom
178inaba:fix-dropped-index-comment
Dec 27, 2025
Merged

fix: skip COMMENT cleanup for dropped indexes in psqldef#1064
gfx merged 1 commit intosqldef:masterfrom
178inaba:fix-dropped-index-comment

Conversation

@178inaba
Copy link
Contributor

Summary

This PR fixes an issue where psqldef generates COMMENT ON INDEX ... IS NULL statements for indexes that have already been dropped, causing errors like:

pq: relation "public.idx_example" does not exist

Problem

When dropping indexes that have comments, psqldef would:

  1. Generate DROP INDEX statements
  2. Then generate COMMENT ON INDEX ... IS NULL statements for the same indexes

Since PostgreSQL automatically removes index comments when an index is dropped, the second step fails because the index no longer exists.

Solution

This fix follows the same pattern used for dropped columns (#1055) and dropped tables:

  • Track dropped indexes in a droppedIndexes map during DDL generation
  • Skip generating COMMENT ... IS NULL statements for indexes that are being dropped

Changes

  • schema/generator.go: Added droppedIndexes map and three helper functions (trackDroppedIndex, droppedIndexKey, isCommentOnDroppedIndex)
  • cmd/psqldef/tests_comments.yml: Added two test cases to verify the fix

Test Plan

  • make build passes
  • make format - no changes
  • make lint - no errors
  • make vulncheck - no vulnerabilities
  • make modernize - no changes
  • go test ./cmd/psqldef -run="TestApply/DropIndexWithComment" passes
  • All psqldef tests pass

Thank you for maintaining this excellent tool!

When an index with a comment is dropped, psqldef was generating
COMMENT ON INDEX ... IS NULL after DROP INDEX, causing an error
because the index no longer exists. PostgreSQL automatically
removes index comments when the index is dropped.

This follows the same pattern as dropped columns (ee7a539).
@codecov
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

❌ Patch coverage is 83.87097% with 5 lines in your changes missing coverage. Please review.

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

@gfx
Copy link
Contributor

gfx commented Dec 27, 2025

Thank you!

@gfx gfx added this pull request to the merge queue Dec 27, 2025
Merged via the queue into sqldef:master with commit 9a08a96 Dec 27, 2025
26 checks passed
@sqldef-bot sqldef-bot bot mentioned this pull request Dec 18, 2025
@178inaba 178inaba deleted the fix-dropped-index-comment branch December 27, 2025 09:38
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