Make WordPress Core

Opened 2 years ago

Closed 3 months ago

#59481 closed defect (bug) (fixed)

dbDelta do useless request when type case is not the same between query and describe result

Reported by: tristanleboss's profile tristanleboss Owned by: johnbillion's profile johnbillion
Milestone: 6.9 Priority: normal
Severity: normal Version: 6.3
Component: Database Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

While debugging why a call to dbDelta was not working, I discovered in the function output (an array of performed changes) that it was doing some useless changes: "Changed type of wp_table.field from varchar(255) to VARCHAR(255)".

Indeed, on my MySQL 8.0.31 Windows x64 version (installed by Wampserver), if I do "DESCRIBE wp_table", the "Type" column of the result is always lowercase. If somehow, you wrote your "CREATE TABLE" statements with a different case for varchar columns, dbDelta will detect a change. It does it only for varchar. If I change my varchar to be lowercase in the SQL I use for dbDelta, it doesn't try to change the type.

<?php

$sql = 'CREATE TABLE `'.$wpdb->prefix.'table` (
                        `object_id` INT(10) NOT NULL,
                        `group_term_slug` VARCHAR(255) NOT NULL
) '.$wpdb->get_charset_collate().';';

$result = dbDelta($sql); // Will contain both "Created table wp_table" and "Changed type of wp_table.field from varchar(255) to VARCHAR(255)"

$sql = 'CREATE TABLE `'.$wpdb->prefix.'table` (
                        `object_id` INT(10) NOT NULL,
                        `group_term_slug` varchar(255) NOT NULL
) '.$wpdb->get_charset_collate().';';

$result = dbDelta($sql); // Will NOT contain "Changed type of wp_table.field from varchar(255) to VARCHAR(255)", only "Created table wp_table"




Attachments (3)

59481.diff (838 bytes) - added by leewillis77 3 months ago.
patch
59481-2.diff (829 bytes) - added by leewillis77 3 months ago.
Previous patch is incorrect. Revised patch attached.
59481-3.diff (1.7 KB) - added by leewillis77 3 months ago.
Updated patch to add unit test

Download all attachments as: .zip

Change History (13)

This ticket was mentioned in PR #5521 on WordPress/wordpress-develop by r-martins.


2 years ago
#1

  • Keywords has-patch added

This will fix an issue (already reported on trac), where queries ran by calling dbDelta($sql) are not executed if they start in lowercase. For example: create table foo..., insert into ...

Trac ticket: https://core.trac.wordpress.org/ticket/59481

#2 @lordspace
16 months ago

yeah, this is annoying.

This also creates multiple indexes too!

The indexes license_plate_normalized and license_plate_normalized_5 seem to be equal and one of them could possibly be removed.
The indexes license_plate and license_plate_5 seem to be equal and one of them could possibly be removed.
....

#3 @leewillis77
3 months ago

  • Keywords has-patch removed

The PR mentioned is unrelated to this issue. The problem reported in this ticket appears to be down to an incorrect comparison in line 3204 of upgrade.php: https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/upgrade.php#L3204

In my specific case the comparison

$tablefield->Type !== $fieldtype

is comparing 'longtext' with 'LONGTEXT'. Clearly these are the same types and no table alteration is required, but this comparison fails and an alter statement is generated needlessly. I suspect that changing $fieldtype to $fieldtype_without_parentheses would resolve this. If that seems sensible then patch to follow.

@leewillis77
3 months ago

patch

#4 @leewillis77
3 months ago

  • Keywords has-patch added

@leewillis77
3 months ago

Previous patch is incorrect. Revised patch attached.

#5 @johnbillion
3 months ago

  • Keywords needs-unit-tests added

Thanks for the patch @leewillis77 ! Would you mind opening a pull request on https://github.com/WordPress/wordpress-develop so the test suite can run? It would be great to get test coverage of this change too.

This ticket was mentioned in PR #9868 on WordPress/wordpress-develop by @leewillis77.


3 months ago
#6

Fixes https://core.trac.wordpress.org/ticket/59481

Amends dbDelta so that differences of case-only in a field's type will not generate unnecessary alter table statements.

Trac ticket: https://core.trac.wordpress.org/ticket/59481

@leewillis77
3 months ago

Updated patch to add unit test

#7 @leewillis77
3 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

#8 @johnbillion
3 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Owner set to johnbillion
  • Status changed from new to reviewing

#9 @johnbillion
3 months ago

  • Milestone changed from Future Release to 6.9

#10 @SergeyBiryukov
3 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 60789:

Database: Do not unnecessarily alter table in dbDelta() for field type case differences.

This aims to avoid extra changes to database structure when type case is the only difference:

Changed type of wp_table.field from varchar(255) to VARCHAR(255)

Follow-up to [1575], [37532].

Props leewillis77, tristanleboss, lordspace, johnbillion, SergeyBiryukov.
Fixes #59481.

Note: See TracTickets for help on using tickets.