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(database): drop fk before dropping index #20113
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
I'm a bit surprised, Does this work everywhere else ? I seem to remember I had to drop the index on foreign keys before dropping them to avoid errors in the past 🤔
Otherwise code looks fine
Oh, that's odd, I hope there's not some dialectal difference where postgres requires it one way and mysql requires it the other. I will add some additional notes on QA on this. |
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.
It turns out only mysql (and nobody else) drops the index along with the fk when you drop a fk. So I added a check that for mysql only, we skip dropping the index if we already dropped a fk of the same name. That may have been the error you faced a long time ago @alexandrebodin -- but I think this improvement makes it more stable, because this actually fixes an extremely rare bug (only happens when fk columns are renamed I think, and we don't offer graceful renaming yet) because mysql doesn't allow it the other way around either, where you drop the index before the fk. They must be done together. |
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.
Code looks good, I didn't QA
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.
I wonder if that's an issue upgrading from 5.0.0-beta.1 instead of from 4.x. I've tried all 4 supported databases now going from 4.x -> this PR and also tried modifying and dropping relations, and didn't have any problem. |
What does it do?
Changes the order of operations in
syncSchema
. In some databases (like mysql) it is impossible to drop an index on a foreign key unless you first drop the fk. The previous order of removal was: index -> fk -> column, I've changed it to fk -> column -> indexWhy is it needed?
see above
more specifically, it also prevents the v4->v5 migration on mysql, because the long version of shortened db identifiers for fks cannot be dropped
How to test it?
NOTE: there's currently an issue with document id migrations on mysql as well preventing startup, which will prevent you testing this as a v4->v5 migration directly, but otherwise doing a v4->v5 migrations would be the best way to test this since that's what this PR is really aiming to solve
Given the comments below, also check some other cases that might trigger a reverse problem. Try restarting strapi after the following cases:
Manually tested:
with
Related issue(s)/PR(s)
Fixes #20020
DX-1374