-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix adding extra unique constraint to columns that are already unique #579
Fix adding extra unique constraint to columns that are already unique #579
Conversation
9373551
to
5993661
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a test case for the problem that this fixes?
Based on the PR description I tried (using the migrations in this Gist) to add a UNIQUE
constraint involving a column that was already UNIQUE
and it worked as expected:
After completing both migrations the schema was:
+----------+------------------------+-----------------------------------------------------+----------+--------------+-------------+
| Column | Type | Modifiers | Storage | Stats target | Description |
|----------+------------------------+-----------------------------------------------------+----------+--------------+-------------|
| id | integer | not null default nextval('items_id_seq'::regclass) | plain | <null> | <null> |
| name | character varying(255) | not null | extended | <null> | <null> |
| producer | character varying(255) | not null | extended | <null> | <null> |
+----------+------------------------+-----------------------------------------------------+----------+--------------+-------------+
Indexes:
"items_pkey" PRIMARY KEY, btree (id)
"items_name_key" UNIQUE CONSTRAINT, btree (name)
"unique_name_producer" UNIQUE CONSTRAINT, btree (name, producer)
This was using commit daba767
Haha, yes. I will redo the example. The test case I put together seemed too complex, but apparently I simplified to too much :D |
I updated the migration file to trigger the bug. I had to reorder the columns in the constraint definition. I am adding more details to the pr description about the bug. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating the example and PR description.
Is it worth also adding a testcase for it?
The example I added is kind of an e2e test for this. Or do you want to add a lower level test for |
…traint-to-column-that-is-already-unique
I was wondering if it was possible/worthwhile to add a test to The problem with using examples as e2e tests is that we have sometimes in the past gone back and rewritten/modified the example migrations. |
…traint-to-column-that-is-already-unique
Previously,
pgroll
failed to add unique constraint to columns that were already included in another unique constraint in some cases. The bug is triggered when the name of the column that is not yet unique comes earlier than the already unique column.The error message we get:
The issue is during renaming phase.
pgroll
checked if the unique index is duplicated (thus, has to be renamed). Thenpgroll
checked if the column (being renamed) is included in the index. If it was part of the index,pgroll
renamed the index to the final name. If that unique index happened to be a unique constraintpgroll
transformed the index into a unique constraint. Good, yay.However, if the unique index is duplicated, but the column (that was being renamed) was not part of the index definition,
pgroll
did not rename the index. If this unique index is a constraint,pgroll
still tried to transform the index with the original name into a constraint. So we got the error message above.Now this problem is fixed.
Related to #227