Skip to content

fix remove unique constraint for collection_series. #1124

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

Conversation

mukeshk
Copy link
Contributor

@mukeshk mukeshk commented Sep 21, 2019

Fix #986

@mukeshk mukeshk requested a review from php-coder as a code owner September 21, 2019 01:34
@mystamps-bot
Copy link

mystamps-bot commented Sep 21, 2019

1 Warning
⚠️ danger check: pull request description doesn’t contain a link to original issue.
Consider adding a comment in the following format: Addressed to #XXX where XXX is an issue number

Generated by 🚫 Danger

@mukeshk
Copy link
Contributor Author

mukeshk commented Sep 21, 2019

On my local pc,

alter table mystamps.collections_series drop index uc_collections_series_collection_id_series_id

Error on rename of '.\mystamps#sql-c38_6' to '.\mystamps\collections_series' (errno: 150 - Foreign key constraint is incorrectly formed)

@mukeshk
Copy link
Contributor Author

mukeshk commented Sep 21, 2019

image

@mukeshk mukeshk force-pushed the gh986_rollback_collection_series_unique_constraint branch 2 times, most recently from 45258c6 to 8de6358 Compare September 21, 2019 05:30
@mukeshk
Copy link
Contributor Author

mukeshk commented Sep 21, 2019

Alter table mystamps.collections_series
drop index uc_collections_series_collection_id_series_id,
drop foreign key fk_collections_series_collection_id,
drop foreign key fk_collections_series_series_id

MysqlError #1025
Error on rename of '.\mystamps\collections_series' to '.\mystamps#sql2-c38-6' (errno: 152 - Cannot delete a parent row)


<changeSet author="mukeshk" id="drop-unique-constraint-from-collections_series-table">
<comment>Remove unique constraint (collection_id,series_id) from collection_series</comment>
<dropForeignKeyConstraint baseTableName="collections_series" constraintName="fk_collections_series_series_id" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's counter intuitive that we remove and re-create foreign keys. Is it required? Would dropUniqueConstraint work without it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would dropUniqueConstraint work without it?

I've just tested it locally (with H2) and it works. So, it's not clear so far why you've added it as it's unnecessary.

xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet author="mukeshk" id="drop-unique-constraint-from-collections_series-table">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically "id" come first and also "context" attribute is missing here.

@php-coder
Copy link
Owner

Ok, I'll look on this failure later.

<changeSet author="mukeshk" id="drop-unique-constraint-from-collections_series-table">
<comment>Remove unique constraint (collection_id,series_id) from collection_series</comment>
<dropForeignKeyConstraint baseTableName="collections_series" constraintName="fk_collections_series_series_id" />
<dropUniqueConstraint constraintName="uc_collections_series_collection_id_series_id" tableName="collections_series"/>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has trailing space BTW.

<comment>Remove unique constraint (collection_id,series_id) from collection_series</comment>
<dropForeignKeyConstraint baseTableName="collections_series" constraintName="fk_collections_series_series_id" />
<dropUniqueConstraint constraintName="uc_collections_series_collection_id_series_id" tableName="collections_series"/>
<addForeignKeyConstraint baseTableName="collections_series" baseColumnNames="series_id"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has trailing space BTW.

http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet author="mukeshk" id="drop-unique-constraint-from-collections_series-table">
<comment>Remove unique constraint (collection_id,series_id) from collection_series</comment>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather remove this comment as it looks obvious to me.

@mukeshk
Copy link
Contributor Author

mukeshk commented Sep 21, 2019 via email

@mukeshk mukeshk force-pushed the gh986_rollback_collection_series_unique_constraint branch from 8de6358 to 43ae664 Compare September 21, 2019 15:15
@php-coder
Copy link
Owner

Yes. The drop unique constraint worked on H2 db. But Travis failed for MySQL

I'll look on this but anyway it should be noted in the comments. Mostly anything that we solve in a unexpected way because of the obstacles, should be expressed in the comments.

I will push again so that you will notice in Travis.

JFYI: I don't get notified on your builds. So the only way to see is when you push to this PR and I look on the result manualy.

http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet author="mukeshk" id="drop-unique-constraint-from-collections_series-table">
<dropUniqueConstraint constraintName="uc_collections_series_collection_id_series_id" tableName="collections_series"/>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 spaces before "tableName" attribute :)

@php-coder
Copy link
Owner

I'm a little confused why it uses DROP KEY instead of DROP INDEX:

Caused by: liquibase.exception.DatabaseException: Error on rename of './mystamps/#sql-e87_6' to './mystamps/collections_series' (errno: 150 - Foreign key constraint is incorrectly formed) [Failed SQL: ALTER TABLE mystamps.collections_series DROP KEY uc_collections_series_collection_id_series_id]

I wouldn't surprise if that's a liquibase bug, for instance.

@php-coder
Copy link
Owner

I'm a little confused why it uses DROP KEY instead of DROP INDEX:

Both are failing in this case :(

mysql> alter table collections_series2 drop key uc_collections_series_collection_id_series_id;
ERROR 1025 (HY000): Error on rename of './mystamps/#sql-488_b7' to './mystamps/collections_series2' (errno: 150 - Foreign key constraint is incorrectly formed)
mysql> alter table collections_series2 drop index uc_collections_series_collection_id_series_id;
ERROR 1025 (HY000): Error on rename of './mystamps/#sql-488_b7' to './mystamps/collections_series2' (errno: 150 - Foreign key constraint is incorrectly formed)

@php-coder
Copy link
Owner

php-coder commented Sep 21, 2019

I found an advise (see https://dba.stackexchange.com/questions/190153/mysql-create-table-error-1005-errno-150-foreign-key-constraint-is-incorrectly/201950#201950) to use SHOW ENGINE INNODB STATUS. Here is the related output:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2019-09-21 20:14:56 0x7fbeb021d700 Error in foreign key constraint of table mystamps/collections_series2:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
  CONSTRAINT `collections_series2_ibfk_1` FOREIGN KEY (`collection_id`) REFERENCES `collections` (`id`)

After closer look on the table definition I see the following:

mysql> show create table collections_series2;
...
  UNIQUE KEY `uc_collections_series_collection_id_series_id` (`collection_id`,`series_id`),
  KEY `fk_collections_series_series_id` (`series_id`),
  CONSTRAINT `collections_series2_ibfk_1` FOREIGN KEY (`collection_id`) REFERENCES `collections` (`id`),
  CONSTRAINT `collections_series2_ibfk_2` FOREIGN KEY (`series_id`) REFERENCES `series`
...

Note that we have KEY fk_collections_series_series_id (series_id) that creates an index for series_id field but there is no similar key for collection_id field. Why? I suppose this is because MySQL uses compound index instead (uc_collections_series_collection_id_series_id). And when we try to remove it, we get refused because removal leads to the state when this field won't have an index at all. And we have to have an index because this field has a foreign key. So, let's try to add this index manually:

mysql> alter table collections_series2 add key fk_collections_series_collection_id(collection_id);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> alter table collections_series2 drop key uc_collections_series_collection_id_series_id;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show create table collections_series2;
...
  KEY `fk_collections_series_series_id` (`series_id`),
  KEY `fk_collections_series_collection_id` (`collection_id`),
  CONSTRAINT `collections_series2_ibfk_1` FOREIGN KEY (`collection_id`) REFERENCES `collections` (`id`),
  CONSTRAINT `collections_series2_ibfk_2` FOREIGN KEY (`series_id`) REFERENCES `series` (`id`)
...

So, it seems like we should add this key then before dropping an unique constraint.

I'll check how it works in H2 later to confirm whether it's right way to go.

@php-coder
Copy link
Owner

php-coder commented Sep 22, 2019

I'll check how it works in H2 later to confirm whether it's right way to go.

It looks like H2 has 2 indices already and it was MySQL trick to optimize number of indices. So, I suggest to: before dropping a unique index, first create an index for collection_id column. It will be a separate changeset I think. If this operation fails on H2, then we should execute it only on MySQL (with dbms="mysql" attribute).

Copy link
Owner

@php-coder php-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments.

@mukeshk
Copy link
Contributor Author

mukeshk commented Sep 29, 2019

It seems I messed up with my branch. it created a detached head.

git status
HEAD detached from 08d659f6
nothing to commit, working tree clean

git rebase master origin/gh986_rollback_collection_series_unique_constraint -i
Successfully rebased and updated detached HEAD.

git status
On branch gh986_rollback_collection_series_unique_constraint
Your branch is up to date with 'origin/gh986_rollback_collection_series_unique_constraint'.

git rebase master origin/gh986_rollback_collection_series_unique_constraint -i
Successfully rebased and updated detached HEAD.

git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)

Any idea what i can do to fix this? sorry for the trouble.

@php-coder
Copy link
Owner

Your branch is ahead of 'origin/master' by 4 commits.

If you only need to reset master to be even with origin/master, use git reset --hard origin/master within master branch. This removes 4 commits. If you need them, copy them with git cherry-pick to a branch where you need them.

xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet author="mukeshk" id="create-index-seriesid-on-collections-series-table">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put 2 change sets to a single file.

@php-coder
Copy link
Owner

No, there shouldn't be my commits, only yours :)

@mukeshk mukeshk closed this Oct 3, 2019
@mukeshk mukeshk force-pushed the gh986_rollback_collection_series_unique_constraint branch from 0e1b9c2 to f3c2518 Compare October 3, 2019 11:58
@php-coder
Copy link
Owner

BTW for a new PR -- don't forget to move a file with migration to 0.4.2 folder as 0.4.1 milestone is closed.

@mukeshk mukeshk deleted the gh986_rollback_collection_series_unique_constraint branch November 10, 2019 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

/collection/{slug}: exception happens when user adds a series to collection that already has this series
3 participants