Keep collations at utf8mb4_general_ci going forward: functions.php now ends by calling databaseConnectionCollation, which runs SET collation_connection = 'utf8mb4_general_ci' on the connection config.php opened, so a string the server makes itself, CAST(x AS CHAR) for one, matches the tables where on MySQL 8 it came out utf8mb4_0900_ai_ci and failed beside a column with Illegal mix of collations, on a new install as much as an upgraded one; only the collation is set and not the character set, no SET NAMES and no mysqli_set_charset, because on a server that defaults to latin1 the connection has always been latin1 and changing it shows every accented letter already stored as two wrong ones, where setting collation_connection alone leaves what is stored and what is stored after the same; admin/database_updates.php calls databaseCollationRepair after a run that applied at least one database update and finished, so a table a migration creates without a COLLATE is given the collation db.sql has in the same run, with what it changed and what it could not going to App Logs and to the output of scripts/update_cli.php, a table it cannot change left as it was and never failing the update, and a run that stops at a failed migration leaving this for the run that finishes; run on MySQL 8.0 and MariaDB 10.11, on a latin1 server default and through update_cli.php --update_db with a migration written as 2.4.1 was; CONTRIBUTING.md and the 26.11.7 changelog section say so

This commit is contained in:
johnnyq
2026-11-11 20:52:46 -05:00
parent bce68aac6a
commit 69fd43051f
6 changed files with 84 additions and 1 deletions

View File

@@ -20,8 +20,51 @@
* CHARACTER SET, which takes every text column in the table - the binary ones
* with the rest - and turns a utf8mb3 TEXT into a MEDIUMTEXT. Only tables and
* columns db.sql has are looked at; anything else in the database is left.
*
* Two things keep it that way. The connection is given the tables' collation
* on every request (databaseConnectionCollation(), from functions.php), so a
* string the server makes itself matches them. And the database updater runs
* the repair after the updates it applies (admin/database_updates.php), so a
* table a future update creates without a COLLATE is put right in the same
* run, on the servers where that matters.
*/
/*
* Give the connection the collation the tables have.
*
* config.php connects without saying, so the connection takes the server's:
* utf8mb4_0900_ai_ci on MySQL 8. A quoted string in a query gives way to the
* column it meets, so nothing shows - until a query has the server make a
* string of its own. CAST(x AS CHAR) comes out in the connection's collation
* and does not give way: beside a column, in a comparison or a UNION, it is
* "Illegal mix of collations", on a new install as much as an upgraded one.
*
* Only the collation is set, never the character set (SET NAMES, or
* mysqli_set_charset()). On a server that defaults to latin1 - MySQL 5.7,
* MariaDB before 11.6 where the packager didn't change it - the connection
* has been latin1 since the install was made, and everything in it was
* written through that and reads back right through it. Changing the
* character set there turns every accented letter already stored into two
* wrong ones. Setting collation_connection changes what the server makes
* strings as, and leaves how text travels in and out exactly as it was -
* tried on a latin1 server: what was stored reads back the same, and what
* is written after is stored the same.
*
* Here and not in config.php, which each install has its own copy of. It is
* one short statement on a connection that is already open.
*/
function databaseConnectionCollation() {
global $mysqli;
if (!isset($mysqli) || !($mysqli instanceof mysqli)) {
return;
}
try {
mysqli_query($mysqli, "SET collation_connection = 'utf8mb4_general_ci'");
} catch (Throwable $e) {
// The page goes on with the server's own - as every page did before this
}
}
/** A text column's type as SHOW CREATE TABLE and db.sql write it, for a regex. An enum's values are quoted, with a quote inside one doubled. */
function databaseCollationTextType() {
return "(?:var)?char\\(\\d+\\)|(?:tiny|medium|long)?text|(?:enum|set)\\('(?:[^']|'')*'(?:,'(?:[^']|'')*')*\\)";