Mysql UNIQUE_CHECKS is disabled for data manipulation while it should only be disabled for schema manipulations #32283
Labels
Issue: Confirmed
Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed
Issue: ready for confirmation
Priority: P1
Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing.
Progress: done
Reproduced on 2.4.x
The issue has been reproduced on latest 2.4-develop branch
Severity: S1
Affects critical data or functionality and forces users to employ a workaround.
Projects
Preconditions (*)
Steps to reproduce (*)
bin/magento setup:upgrade
Expected result (*)
Actual result (*)
Discussion
This was seen on two completely different shops when upgrading them from Magento 2.1.11 and 2.1.16 to 2.3.6-p1
This UpgradeWebsiteAttributes patch (which only executes when you upgrade from Magento < 2.2.0 to 2.2.0 or higher) can add duplicated values in certain
catalog_product_entity_xxx
tables. TheON DUPLICATE KEY UPDATE
statement is being ignored because unique_checks are disabled by the OperationsExecutor.After talking to some Magento core devs on Slack, they said the UNIQUE_CHECKS setting is supposed to be disabled for schema changes, but not for data manipulation. However UNIQUE_CHECKS is never enabled again after being disabled.
They further explained that the connection to the mysql server and thus the temporary changes being made to UNIQUE_CHECKS (and other variables) is supposed to be closed after schema manipulation is done and a new connection should start up before manipulating the data. This unfortunately doesn't seem to be the case.
This is a pretty serious issue in my opinion, not because only a certain Patch inside core Magento can trigger it, but because many customisations can also cause something like this if they aren't very careful.
And also because this is an invisible problem. You won't notice errors anywhere, unless you dump the database and try to import it again. So there is a chance people have run against this problem and are not aware of this and now have a corrupt database.
An example
I have a specific case where I can actually debug this, it only happens on a remote server, not on my local, but at least I can reproduce it every single time by:
bin/magento setup:upgrade
In the 2.1.11 database, these records exist:
After
setup:upgrade
ran, it now contains:You see 2 duplicated lines in here, even though there is a unique key set on the combination of
attribute_id
,store_id
andentity_id
columns.There is only one website on this instance with one store and two storeviews.
I've added some debug code in some files, to demonstrate what is going on.
In OperationsExecutor::startSetupForAllConnections, added some var_dumps around disabling the unique_checks
This results in the following output:
In UpgradeWebsiteAttributes::apply:
Resulting in:
This demonstrates that the unique_checks are still disabled when this data patch executes, which is very bad!
In UpgradeWebsiteAttributes::executeInsertions, to search for insert statements which can introduce duplicated values:
Resulting in:
This demonstrates that two records get inserted with the same values.
I do wonder why the end result is then not 3 duplicated entries, because one already existed from before the upgrade was ran. Not exactly sure what's going on there ...
The end result is a corrupt database. You won't notice this at all, unless you mysqldump the database and try to import it again, only then you see the error happening:
So there is a chance that more people already ran into this problem but never saw an error about it but now have a corrupt database.
Workaround
The workaround is to restore the unique_checks variable again in: OperationsExecutor::endSetupForAllConnections.
A PR will follow soon.
For people having to deal with this corrupt database and a way to fix it (which isn't easy nor explained very well on the world wide web), you can:
mysqldump
with the--extended-insert=FALSE
flag but only for the tables with problemsmysql
with the-f
flag to import that dump again, it will output warnings about all the duplicated entries but continues to import the other ones.Be VERY CAREFUL HERE, triple check what you are doing here before executing this on a production environment.
Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.
The text was updated successfully, but these errors were encountered: