Revert "fix: preserve column order in UNIQUE constraints (#17)"#19
Revert "fix: preserve column order in UNIQUE constraints (#17)"#19
Conversation
This reverts commit 109ca81.
There was a problem hiding this comment.
Pull Request Overview
This PR reverts a previous fix that preserved column order in UNIQUE constraints, effectively removing the position-based sorting functionality for constraint columns.
- Removes the
requiresPositionSortingfunction that determined which constraint types needed position-based sorting - Eliminates position-based sorting for constraint columns and referenced columns in foreign keys
- Removes associated comments explaining the column ordering behavior
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| table.Constraints[key.name] = constraint | ||
|
|
||
| // For partitioned tables, ensure primary key columns are ordered with partition key first |
There was a problem hiding this comment.
The comment on line 524 references 'position-based sorting' but the position-based sorting code has been removed. This comment should be updated to reflect the current behavior or removed entirely.
| // For partitioned tables, ensure primary key columns are ordered with partition key first | |
| // For partitioned tables, handle primary key columns as needed |
|
@screenfluent I have taken a closer look today. And I think the original code has already handled this case https://github.com/pgschema/pgschema/blob/109ca81eb514570f443c7c12cd0a2cad1e8cf988/internal/diff/table.go#L29 I also ran the test cases in #17 (comment) and #17 (comment) before #17, and they are working as expected. Our existing test case should have caught this:
The reason is our underlying dump implementation is similar to migrate. The dump is a diff between an empty schema and the target database schema. And it will go through the same diff logic. |
|
@screenfluent I am going to revert #17 for now. But I am happy to take another look if there are things that I have overlooked. |
|
@tianzhou hey I think there's a misunderstanding here - the bug is real and I can reproduce it right now. The Here's quick test: # In PostgreSQL:
psql -c "SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'account_provider_unique';"
# Shows: UNIQUE ("providerId", "accountId")
# pgschema dump after revert:
pgschema dump --db directory_dev | grep "UNIQUE.*account"
# Shows: UNIQUE ("accountId", "providerId") <- wrong order!The problem is dump workflow goes like this:
So the sort in table.go can't fix what inspector gives it wrong in first place. I can make a test specifically for dump command if that helps? The existing tests work because they test diff between SQL files not dump from real database. |
|
@screenfluent, a test would be great! Don't worry about where to put the test. I can always adjust it later if needed. |
|
@tianzhou here's the test! Put it wherever you think fits best. func TestDumpPreservesConstraintColumnOrder(t *testing.T) {
// Setup test database with UNIQUE (provider_id, account_id)
db.Exec(`CREATE TABLE test_order (
id text PRIMARY KEY,
provider_id text NOT NULL,
account_id text NOT NULL,
CONSTRAINT test_unique UNIQUE (provider_id, account_id)
)`)
// Build IR from database
inspector := ir.NewInspector(db)
schema, _ := inspector.BuildIR(ctx, "public")
// Check constraint column order
constraint := schema.Schemas["public"].Tables["test_order"].Constraints["test_unique"]
// This fails without the fix - columns are in alphabetical order
assert.Equal(t, "provider_id", constraint.Columns[0].Name)
assert.Equal(t, "account_id", constraint.Columns[1].Name)
}The test fails without sorting columns by Position in buildConstraints(). Let me know if you need anything else! |
|
@screenfluent I noticed the test case missed That's where we handle position sorting |
|
I see what you mean, but the problem is that Here's the issue flow:
Look at getColumnNames := func(columns []*ir.ConstraintColumn) []string {
var names []string
for _, col := range columns { // <-- No sorting here!
names = append(names, util.QuoteIdentifier(col.Name))
}
return names
}Meanwhile, sortConstraintColumnsByPosition is only used in:
But NOT in generateConstraintSQL which is used for inline constraints in CREATE TABLE! Here's proof the bug exists: The Position values ARE correctly loaded from database (providerId=1, accountId=2), but generateConstraintSQL doesn't use them. The fix is either:
|
This reverts commit 109ca81.
#17