Skip to content
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

[5.1] Fix SQL error 1242 "Subquery returns more than 1 row" on update when having multiple update sites for the core #42988

Merged
merged 3 commits into from
Mar 10, 2024

Conversation

richard67
Copy link
Member

@richard67 richard67 commented Mar 9, 2024

Pull Request for Issue #42971 (part).

Summary of Changes

When updating from 5.1.0-alpha4 or any older 5.1.0-alpha or 5.0.x or 4.4.x, the update SQL script "5.1.0-2024-02-24.sql" is run, which migrates the Joomla core update site to TUF, see pull request (PR) #42799 .

The update SQL script creates a new table and does an INSERT statement on that table, then it does an UPDATE statement on the #__update_sites table.

Both statements INSERT and UPDATE contain the same subquery to get the update_site_id.

If that subquery returns more than 1 row, which can happen if there is more than 1 update site for the Joomla core, the INSERT will insert one record for each of these update sites.

But the UPDATE statement will fail with an SQL error no. 1242 "Subquery returns more than 1 row" on MySQL/MariaDB or "more than one row returned by a subquery used as an expression" on PostgreSQL.

This PR here fixes that by using the IN instead of the = operator so the UPDATE statement will update all rows returned by the subquery.

Testing Instructions

Pre-conditions

The test can be executed in a tool like e.g. phpMyAdmin for MySQL or MariaDB or phpPgAdmin for PostgreSQL databases.

It just needs a database with a recent Joomla 5.1 or 5.0.x or 4.4.x installation.

In all SQL statements in the following steps, replace the #__ in all table names by your actual database prefix including the underscore.

Step 1: Prepare a 2nd update site for the Joomla core.

You should of course not do that on a production site, and if you want to continue to use the site you should clean up the new records created by this step in the #__update_sites and the #__update_sites_extensions table after the test.

With MySQL or MariaDB:

INSERT INTO `#__update_sites` (`name`, `type`, `location`, `enabled`, `last_check_timestamp`) VALUES
('Test duplicate', 'collection', 'https://update.joomla.org/core/list.xml', 0, 0);

INSERT INTO `#__update_sites_extensions` (`update_site_id`, `extension_id`)
SELECT u.`update_site_id`, e.`extension_id`
  FROM (SELECT MAX(`update_site_id`) AS `update_site_id` FROM `#__update_sites`) AS u
  JOIN `#__extensions` AS e
 WHERE e.`type`='file' AND e.`element`='joomla';

With PostgreSQL:

INSERT INTO "#__update_sites" ("name", "type", "location", "enabled", "last_check_timestamp") VALUES
('Test duplicate', 'collection', 'https://update.joomla.org/core/list.xml', 0, 0);

INSERT INTO "#__update_sites_extensions" ("update_site_id", "extension_id")
SELECT u."update_site_id", e."extension_id"
  FROM (SELECT MAX("update_site_id") AS "update_site_id" FROM "#__update_sites") AS u
  JOIN "#__extensions" AS e ON 1=1
 WHERE e."type"='file' AND e."element"='joomla';

Step 2: Run the UPDATE statement of the unmodified update SQL script "5.1.0-2024-02-24.sql"

With MySQL or MariaDB see https://github.com/joomla/joomla-cms/blob/5.1-dev/administrator/components/com_admin/sql/updates/mysql/5.1.0-2024-02-24.sql#L21-L23

With PostgreSQL see https://github.com/joomla/joomla-cms/blob/5.1-dev/administrator/components/com_admin/sql/updates/postgresql/5.1.0-2024-02-24.sql#L21-L23

Result: See section "Actual result BEFORE applying this Pull Request" below.

Step 3: Run the UPDATE statement of the update SQL script "5.1.0-2024-02-24.sql" from this PR

With MySQL or MariaDB see

UPDATE `#__update_sites`
SET `type` = 'tuf', `location` = 'https://update.joomla.org/cms/'
WHERE `update_site_id` IN (SELECT ue.`update_site_id` FROM `#__update_sites_extensions` AS ue JOIN `#__extensions` AS e ON (e.`extension_id` = ue.`extension_id`) WHERE e.`type`='file' AND e.`element`='joomla');

With PostgreSQL see

UPDATE "#__update_sites"
SET "type" = 'tuf', "location" = 'https://update.joomla.org/cms/'
WHERE "update_site_id" IN (SELECT ue."update_site_id" FROM "#__update_sites_extensions" AS ue JOIN "#__extensions" AS e ON (e."extension_id" = ue."extension_id") WHERE e."type"='file' AND e."element"='joomla');

... or just replace the first = by IN in the WHEREclause of the UPDATE statement from the previous step 2.

Result: See section "Expected result AFTER applying this Pull Request" below.

Actual result BEFORE applying this Pull Request

With MySQL or MariaDB SQL error #1242 Subquery returns more than 1 row, with PostgreSQL ERROR: more than one row returned by a subquery used as an expression.

Expected result AFTER applying this Pull Request

Both update sites are updated by the UPDATE statement.

Link to documentations

Please select:

  • No documentation changes for docs.joomla.org needed

  • No documentation changes for manual.joomla.org needed

@richard67 richard67 marked this pull request as ready for review March 9, 2024 12:24
@richard67 richard67 added this to the Joomla! 5.1.0 milestone Mar 9, 2024
@alikon
Copy link
Contributor

alikon commented Mar 9, 2024

I have tested this item ✅ successfully on 6988fcd


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42988.

1 similar comment
@viocassel
Copy link
Contributor

I have tested this item ✅ successfully on 6988fcd


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42988.

@richard67
Copy link
Member Author

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42988.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Mar 9, 2024
@richard67 richard67 added this to the Joomla! 5.1.0 milestone Mar 9, 2024
@bembelimen bembelimen merged commit af08b9c into joomla:5.1-dev Mar 10, 2024
4 checks passed
@bembelimen
Copy link
Contributor

Thx

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.

None yet

6 participants