Skip to content

Conversation

@richard67
Copy link
Member

@richard67 richard67 commented Aug 24, 2025

Pull Request for Issue # .

Follow up to PRs #45211 , #45233 and #43974 .

Summary of Changes

This pull request (PR) modifies all INSERT statements 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 (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.

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 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 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

@richard67 richard67 changed the title 6.0 dev update sql no duplicate inserts [6.0] Don't insert duplicate records in update SQL scripts when the script runs multiple times Aug 24, 2025
@richard67 richard67 changed the title [6.0] Don't insert duplicate records in update SQL scripts when the script runs multiple times [6.0] Don't insert duplicate records in update SQL scripts when they are executed multiple times Aug 24, 2025
@brianteeman
Copy link
Contributor

Thanks for the detailed explanation. From reading the code I had some questions but i see you answered all of them already

@muhme
Copy link
Contributor

muhme commented Aug 29, 2025

I have tested this item ✅ successfully on 34a605b

Tested with JBT and integrated phpMyAdmin, some used SQL statements:

  • select * from ajbt54_mail_templates where template_id = 'plg_content_joomla.newarticle'
  • select * from ajbt54_extensions where name = 'plg_fields_note';
  • select * from ajbt54_extensions where name = 'plg_fields_number'
  • Before applying this PR in 5.4 and with MariaDB 10.6.23
    • for 6.0.0-2025-03-25.sql script duplicate key error
    • for 6.0.0-2025-08-16.sql before 0 ajbt54_extensions where name = 'plg_fields_note' and with each script execution one more entry
    • for 6.0.0-2025-08-17.sql before 0 ajbt54_extensions where name = 'plg_fields_number' and with each script execution one more entry

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

  • MariaDB 10.6.23
    • ✅ running 6.0.0-2025-03-25.sql script multiple times, for second and following runs only a warning
    • ✅ running 6.0.0-2025-08-16.sql script multiple times, running mutliple times is possible, only one entry
    • ✅ running 6.0.0-2025-08-17.sql script multiple times, running mutliple times is possible, only one entry
  • MySQL 8.1.0 new Joomla installation with JBT scripts/database
    • ✅ running 6.0.0-2025-03-25.sql script multiple times, for second and following runs only a warning
    • ✅ running 6.0.0-2025-08-16.sql script multiple times, running mutliple times is possible, only one entry
    • ✅ running 6.0.0-2025-08-17.sql script multiple times, running mutliple times is possible, only one entry
  • PostgreSQL 15.8 new Joomla installation with JBT scripts/database
    • ✅ running 6.0.0-2025-03-25.sql script multiple times, no problem with second and following runs, still one entry
    • ✅ running 6.0.0-2025-08-16.sql script multiple times, running mutliple times is possible, only one entry
    • ✅ running 6.0.0-2025-08-17.sql script multiple times, running mutliple times is possible, only one entry
  • Checked all SQL scripts in administrator/components/com_admin/sql/updates/mysql/*
    ✅ All insert statements are save with INSERT IGNORE or INSERT INTO ... WHERE NOT EXISTS
    This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/45972.

@softforge softforge added this to the Joomla! 6.0.0 milestone Aug 29, 2025
@softforge softforge merged commit 533c8d4 into joomla:6.0-dev Aug 29, 2025
30 checks passed
@richard67 richard67 deleted the 6.0-dev-update-sql-no-duplicate-inserts branch August 29, 2025 16:22
rdeutz pushed a commit to rdeutz/joomla-cms that referenced this pull request Sep 2, 2025
…are executed multiple times (joomla#45972)

* Insert extensions only if record doesn't exist for the same extension

* Insert mail templates only if PK is not violated
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.

5 participants