Skip to content

[5.4] Don't insert duplicate records in update SQL scripts when they are executed multiple times#45973

Merged
muhme merged 2 commits intojoomla:5.4-devfrom
richard67:5.4-dev-update-sql-no-duplicate-inserts
Aug 26, 2025
Merged

[5.4] Don't insert duplicate records in update SQL scripts when they are executed multiple times#45973
muhme merged 2 commits intojoomla:5.4-devfrom
richard67:5.4-dev-update-sql-no-duplicate-inserts

Conversation

@richard67
Copy link
Member

@richard67 richard67 commented Aug 24, 2025

Pull Request for Issue # .

Follow up to PRs #45371 and #45143 .

Summary of Changes

This pull request (PR) modifies all INSERT statements in any 5.4 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 (id column) in the insert, so that also cannot be violated, we have to use an INSERT INTO ... SELECT ... WHERE NOT EXISTS statement, checking the column combination type, element, folder and client_id, which should be unique for each extension even if we don't have a unique key for that.

The same applies to the guidedtours table.

In case of the mail templates table we have a primary key on the 2 columns template_id and language. Here we can use INSERT IGNORE for MySQL and MariaDB and ON CONFLICT DO NOTHING for 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 EXISTS and INSERT IGNORE/ON CONFLICT DO NOTHING we have already examples in update SQL scripts for 5.x versions.

As we are in beta phase we can modify the existing 5.4.0 scripts without the need for any extra comment about the modification.

Testing Instructions

Use phpMyAdmin on a Joomla 5.3 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 TABLE statement for the #__guidedtours table from the installation/extensions.sql file, the CREATE TABLE statement for the #__mail_templates table from the installation/supports.sql file and the CREATE TABLE statement for the #__extensions table from the installation/base.sql file 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 INSERT statement a new record is inserted.

Expected result AFTER applying this Pull Request

With the first execution of the particular INSERT statement 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

@muhme
Copy link
Contributor

muhme commented Aug 26, 2025

I have tested this item ✅ successfully on 57aab44

Tested with JBT and integrated phpMyAdmin, before this PR

  • select * from ajbt53_extensions where name = 'plg_behaviour_compat6';
  • select * from ajbt53_mail_templates where template_id = 'com_joomlaupdate.update.success'
  • select * from ajbt53_guidedtours where title = 'COM_GUIDEDTOURS_TOUR_WHATSNEW_5_4_TITLE'
  • MariaDB 10.6.23
    • in 5.3. before SQL-script 0 entries with each 5.4.0-2025-04-23.sql script running one more entry
      • for 5.4.0-2025-05-10.sql script duplicate key error

Tested this PR with JBT scripts/graft and integrated phpMyAdmin and pgAdmin

  • MariaDB 10.6.23
    • ✅ running 5.4.0-2025-04-23.sql script multiple times, still one entry
    • ✅ running 5.4.0-2025-05-10.sql script multiple times is possible, only warnings duplicate entry, additional entries only for #__postinstall_messages and #__guidedtour_steps
  • MySQL 8.1.0 new Joomla installation with JBT scripts/database
    • ✅ running 5.4.0-2025-04-23.sql script multiple times, still one entry
    • ✅ running 5.4.0-2025-05-10.sql script multiple times is possible, only warnings duplicate entry, additional entries only for #__postinstall_messages and #__guidedtour_steps
  • PostgreSQL 15.8 new Joomla installation with JBT scripts/database
    • ✅ running postgresql/5.4.0-2025-04-23.sql script multiple times, still one entry
    • ✅ running 5.4.0-2025-05-10.sql script multiple times, additional entries only for #__postinstall_messages and #__guidedtour_steps
      This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/45973.

@muhme muhme merged commit 5172a81 into joomla:5.4-dev Aug 26, 2025
40 of 41 checks passed
@muhme muhme added this to the Joomla! 5.4.0 milestone Aug 26, 2025
@muhme
Copy link
Contributor

muhme commented Aug 26, 2025

Thank you @richard67 for your contribution.

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.

3 participants