[6.0] Don't insert duplicate records in update SQL scripts when they are executed multiple times #45972
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request for Issue # .
Follow up to PRs #45211 , #45233 and #43974 .
Summary of Changes
This pull request (PR) modifies all
INSERTstatements in any 6.0 update SQL scripts so that the new records are only inserted if they do not already exist.This makes sure that we do not get duplicate records when the update SQL script runs multiple time, e.g. a 2nd time after a failed update attempt is resumed instead of starting again with a restored backup from before the update.
In case of the extensions table where we do not have a unique key which could be violated by the inserted data and where we do not use the primary key (
idcolumn) in the insert, so that also cannot be violated, we have to use anINSERT INTO ... SELECT ... WHERE NOT EXISTSstatement, checking the column combinationtype,element,folderandclient_id, which should be unique for each extension even if we don't have a unique key for that.In case of the mail templates table we have a primary key on the 2 columns
template_idandlanguage. Here we can useINSERT IGNOREfor MySQL and MariaDB andON CONFLICT DO NOTHINGfor PostgreSQL to avoid duplicates.All this increases the resilience of the update SQL scripts for multiple executions.
For both ways
INSERT INTO ... SELECT ... WHERE NOT EXISTSandINSERT IGNORE/ON CONFLICT DO NOTHINGwe have already examples in update SQL scripts for 5.x versions.As we are in beta phase we can modify the existing 6.0.0 scripts without the need for any extra comment about the modification.
Testing Instructions
Use phpMyAdmin on a Joomla 5.4 database where the records which shall be inserted by the update SQL statements which are modified by this PR do not exist yet.
Alternatively, on any Joomla version, copy the
CREATE TABLEstatement for the#__mail_templatestable from theinstallation/supports.sqlfile and theCREATE TABLEstatement for the#__extensionstable from theinstallation/base.sqlfile into phpMyadmin and execute them so you have these tables without your database prefix and can play with them without messing your installation.Now execute multiple times the original SQL statement from each of the scripts touched by this PR. Original means how it is without this PR applied. You have to replace the
#__by the actual prefix when not using separate tables with the original#__prefix.Result: See section "Actual result BEFORE applying this Pull Request" below.
Now delete the previously inserted records from the mentioned tables, or if working on separate tables beside your Joomla installation, just clear these tables so you have the initial conditions.
Now execute multiple times the modified SQL statement from each of the scripts touched by this PR. You have to replace the
#__by the actual prefix when not using separate tables with the original#__prefix.Result: See section "Expected result AFTER applying this Pull Request" below.
Actual result BEFORE applying this Pull Request
With every execution of the particular
INSERTstatement a new record is inserted.Expected result AFTER applying this Pull Request
With the first execution of the particular
INSERTstatement a new record is inserted, every further execution does not insert a new record.Link to documentations
Please select:
Documentation link for docs.joomla.org:
No documentation changes for docs.joomla.org needed
Pull Request link for manual.joomla.org:
No documentation changes for manual.joomla.org needed