Opened 3 years ago
Last modified 2 months ago
#60002 new defect (bug)
maybe_convert_table_to_utf8mb4() not work with new version of MySQL/MariaDB.
| Reported by: | okvee | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
From [this document](https://mariadb.com/kb/en/unicode/). They said:
From MariaDB 10.6, utf8 is by default an alias for utf8mb3
And from [this document](https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8mb3.html) in MySQL. They said:
Historically, MySQL has used utf8 as an alias for utf8mb3; beginning with MySQL 8.0.28, utf8mb3 is used exclusively in the output of SHOW statements and in Information Schema tables when this character set is meant.
It can be refer to [this issue](https://core.trac.wordpress.org/changeset/53918).
So, from this simple MySQL code:
CREATE TABLE testing (
id bigint(20) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
If you run on MariaDB 10.6 or newer (for me it is 10.6.7) the utf8 column will be automatically changed to utf8mb3_general_ci.
But, WordPress function maybe_convert_table_to_utf8mb4() contain this condition:
<?php if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) { // Don't upgrade tables that have non-utf8 columns. return false; }
Which will be result in no change from utf8mb3_general_ci to utf8mb4_unicode_ci.
Change History (8)
#3
@
12 months ago
This cause problems when plugins are trying to create/alter their tables and cause wrong collation. Please fix.
#4
@
11 months ago
Found another possible bug.
File: wp-includes/class-wpdb.php
Line: 886
Method: determine_charset()
if ( 'utf8' === $charset ) {
$charset = 'utf8mb4';
}
Should be change to
if ( 'utf8' === $charset || 'utf8mb3' === $charset ) {
$charset = 'utf8mb4';
}
From same reason.
Historically, MySQL has used utf8 as an alias for utf8mb3; beginning with MySQL 8.0.28, utf8mb3 is used exclusively in the output of SHOW statements and in Information Schema tables when this character set is meant.
My hot fix function that improved to always use better collation (mentioned by WordPress wpdb.php file).
function rundizcomhotfix_maybe_convert_table_to_utf8mb4( $table ) {
global $wpdb;
$results = $wpdb->get_results("SHOW FULL COLUMNS FROM `$table`");
if (!$results) {
return false;
}
foreach ($results as $column) {
if ($column->Collation) {
list( $charset ) = explode('_', $column->Collation);
$charset = strtolower($charset);
if ('utf8' !== $charset && 'utf8mb3' !== $charset && 'utf8mb4' !== $charset) {
// Don't upgrade tables that have non-utf8 columns.
return false;
}
}
}
$table_details = $wpdb->get_row("SHOW TABLE STATUS LIKE '$table'");
if (!$table_details) {
return false;
}
list( $table_charset ) = explode('_', $table_details->Collation);
$table_charset = strtolower($table_charset);
if ('utf8mb4' === $table_charset) {
return true;
}
// the code above has been copied from original function.
// custom code that upgrade to best collate. ---------------------------------
$table_charset = 'utf8mb4';
$collate = 'utf8mb4_unicode_ci';
$charset_collate = $wpdb->determine_charset($table_charset, $collate);
$table_charset = $charset_collate['charset'];
$collate = $charset_collate['collate'];
unset($charset_collate);
// end custom code that upgrade to best collate. ---------------------------------
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange
return $wpdb->query("ALTER TABLE $table CONVERT TO CHARACTER SET $table_charset COLLATE $collate");
}
This ticket was mentioned in Slack in #core by andraganescu. View the logs.
2 months ago
#7
@
2 months ago
With mariaDB 13.1 we'll see potential mixed encoding tables. JOINs across old and new tables may hit collation coercion, which could result in degraded index use.
In some setups the "Illegal mix of collations" log spam (see #54836, where this will be a default). Additionally inconsistent user-visible behavior may appear, like an emoji surviving in a plugin table but getting stripped from post_content on the same site.
This ticket was mentioned in PR #13456 on WordPress/wordpress-develop by @faisalahammad.
2 months ago
#8
- Keywords has-patch has-unit-tests added
MySQL 8.0.30+ and MariaDB 10.6.1+ report the utf8 charset as utf8mb3 in SHOW statements and Information Schema output. Some code that inspects that output still only checks for utf8 and utf8mb4, so tables can end up unconverted or collations invalid on those servers. 5d78ecbe25 already fixed this inside wpdb for table charset lookups and text checks. This PR fixes the remaining two gaps:
### maybe_convert_table_to_utf8mb4()
Before: src/wp-admin/includes/upgrade.php listed each column charset with if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ). On MySQL 8.0.30+ or MariaDB 10.6.1+, every column of a utf8 table is reported as utf8mb3, so the check failed and the function returned false. Tables were left unconverted during upgrades.
After: utf8mb3 is accepted in the allowlist, matching the utf8 and utf8mb4 entries already handled by get_table_charset().
### wpdb::determine_charset()
Before: src/wp-includes/class-wpdb.php only matched an exact 'utf8' charset, and rewrote collations with str_replace( 'utf8_', 'utf8mb4_', ... ). On newer servers, 'utf8mb3' fell through untouched, and a reported collation like utf8mb3_general_ci would pair with the upgraded charset to produce the invalid combination utf8mb4 + utf8mb3_general_ci, causing "illegal mix of collations" errors.
After: utf8mb3 is treated like utf8 and normalized to utf8mb4, utf8mb3_general_ci is treated like utf8_general_ci, and utf8mb3_ collation prefixes are rewritten alongside utf8_. This matches the existing handling in get_table_charset() and check_safe_collation().
### Testing
New tests:
Tests_DB::test_utf8mb3_charset_switched_to_utf8mb4()andTests_DB::test_utf8mb3_non_unicode_collation_switched_to_utf8mb4()fordetermine_charset().Tests_Admin_IncludesUpgrade::test_maybe_convert_table_to_utf8mb4_converts_utf8_table()and::test_maybe_convert_table_to_utf8mb4_skips_non_utf8_columns()formaybe_convert_table_to_utf8mb4(). The tests use a real table and remove thequeryfilters that rewriteCREATE TABLEtoCREATE TEMPORARY TABLE, becauseSHOW TABLE STATUScannot see temporary tables. On servers that still reportutf8, the create usesutf8, so the tests pass on older setups too.
Verified that the new tests fail against unpatched trunk and pass with the fix. Focused suites pass on MySQL 9.7 (Tests_Admin_IncludesUpgrade, Tests_DB, Tests_DB_Charset, Tests_DB_dbDelta, IncludesSchema): 105 tests, 212 assertions. PHPCS clean on the touched files.
## Use of AI Tools
AI assistance: Yes
Tool(s): ZCode
Model(s): GLM
Used for: Ticket research, implementing the charset checks, writing the regression tests, and running the local test suite. The changes and this description were reviewed and submitted by me.
Tested with MySQL 8.2, and
SHOW FULL COLUMNS FROM$tableindeed returnsutf8mb3_...as the collation name. So I think you are right, that we need to change that line to something like this:if ( 'utf8' !== $charset && 'utf8mb3' !== $charset && 'utf8mb4' !== $charset ) {