Skip to content

fix: skip comment cleanup for renamed objects in psqldef#1101

Merged
gfx merged 1 commit intosqldef:masterfrom
178inaba:feature/fix-rename-comment-bug
Jan 7, 2026
Merged

fix: skip comment cleanup for renamed objects in psqldef#1101
gfx merged 1 commit intosqldef:masterfrom
178inaba:feature/fix-rename-comment-bug

Conversation

@178inaba
Copy link
Contributor

@178inaba 178inaba commented Jan 7, 2026

Summary

This PR fixes an issue where psqldef generates invalid COMMENT ... IS NULL statements for renamed columns, tables, and indexes when using the @renamed from=xxx annotation.

Problem

When renaming an object (column, table, or index) that has a comment, the generator incorrectly attempts to clean up the comment on the old name, resulting in errors like:

pq: column "username" of relation "public.users" does not exist

Root Cause

PostgreSQL automatically transfers comments when renaming objects. However, the generator was not tracking renamed objects as "dropped" for comment cleanup purposes, causing it to generate unnecessary COMMENT ... IS NULL statements for names that no longer exist.

Solution

Track renamed objects in the existing droppedColumns, droppedTables, and droppedIndexes maps so that comment cleanup logic correctly skips them:

  • Columns: Call trackDroppedColumn() after RENAME COLUMN
  • Tables: Add to droppedTables map after RENAME TABLE
  • Indexes: Call trackDroppedIndex() after RENAME INDEX

Changes

  • schema/generator.go: Add tracking for renamed objects (3 locations, 6 lines total)
  • cmd/psqldef/tests_comments.yml: Add test cases for rename + comment scenarios

Test Plan

  • make build
  • make test-all-flavors (all tests pass except mssqldef which requires SQL Server)
  • make format
  • make lint

Thank you for reviewing this PR!

PostgreSQL automatically transfers comments when renaming columns,
tables, or indexes. Track renamed objects as "dropped" for comment
cleanup to avoid generating invalid COMMENT ... IS NULL statements.
@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
schema/generator.go 80.97% <100.00%> (+0.01%) ⬆️
🚀 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 Jan 7, 2026

Good catch. Thank you for your contributions!

@gfx gfx added this pull request to the merge queue Jan 7, 2026
Merged via the queue into sqldef:master with commit d177ea4 Jan 7, 2026
25 checks passed
@sqldef-bot sqldef-bot bot mentioned this pull request Jan 7, 2026
@178inaba 178inaba deleted the feature/fix-rename-comment-bug branch January 7, 2026 06:00
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