Skip to content

fix: preserve unique constraint when renaming column in psqldef#1103

Merged
178inaba merged 1 commit intosqldef:masterfrom
178inaba:fix-inline-unique-constraint
Jan 8, 2026
Merged

fix: preserve unique constraint when renaming column in psqldef#1103
178inaba merged 1 commit intosqldef:masterfrom
178inaba:fix-inline-unique-constraint

Conversation

@178inaba
Copy link
Contributor

@178inaba 178inaba commented Jan 8, 2026

Summary

When renaming a column with an inline UNIQUE constraint using the @renamed annotation, psqldef was incorrectly generating DROP CONSTRAINT for the unique index. This caused the UNIQUE constraint to be lost after applying the migration.

Problem

PostgreSQL automatically updates constraint column references when a column is renamed via ALTER TABLE ... RENAME COLUMN. Therefore, the DROP CONSTRAINT generated by psqldef was unnecessary and resulted in data integrity loss.

Before this fix:

-- Current schema
CREATE TABLE users (
  username varchar(100) NOT NULL UNIQUE
);

-- Desired schema  
CREATE TABLE users (
  user_name varchar(100) NOT NULL UNIQUE -- @renamed from=username
);

-- Generated DDL (incorrect)
ALTER TABLE "public"."users" RENAME COLUMN "username" TO "user_name";
ALTER TABLE "public"."users" DROP CONSTRAINT "users_username_key";  -- This was wrong!

After this fix:

-- Generated DDL (correct)
ALTER TABLE "public"."users" RENAME COLUMN "username" TO "user_name";
-- No DROP CONSTRAINT - PostgreSQL handles the constraint update automatically

Changes

  • Modified generateDDLsForAbsentIndex in schema/generator.go to check if a column was renamed from the name referenced by the unique index
  • Added test case RenameColumnWithInlineUniqueConstraint to verify the fix

Testing

  • All existing tests pass
  • New test case covers the specific scenario

When renaming a column with a UNIQUE constraint using @renamed annotation,
psqldef was incorrectly generating DROP CONSTRAINT for the unique index.
PostgreSQL automatically updates constraint column references when a column
is renamed, so this DROP was unnecessary and caused the constraint to be lost.

The fix checks if the column was renamed from the name referenced by the
unique index, treating renamed columns as matching.
@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

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

@178inaba 178inaba self-assigned this Jan 8, 2026
@178inaba 178inaba requested a review from gfx January 8, 2026 10:00
@gfx
Copy link
Contributor

gfx commented Jan 8, 2026

Thanks! Please merge it by yourself.

@178inaba 178inaba added this pull request to the merge queue Jan 8, 2026
Merged via the queue into sqldef:master with commit ac44e65 Jan 8, 2026
25 checks passed
@178inaba 178inaba deleted the fix-inline-unique-constraint branch January 8, 2026 10:39
@sqldef-bot sqldef-bot bot mentioned this pull request Jan 8, 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