[5.x]: MySQL DSN in config array bypasses charset

Aperta Adatta ai principianti
#20,248 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
mariadb, mysql, php
Ambito
databases

Direzione di ricerca

Inizia da DbConfig::init() nelle classi di configurazione di Craft, che salta _updateDsn() quando viene fornito un DSN, quindi driver resta null. App::dbConfig() legge il driver dal DSN, quindi confronta i due. getCharset() dovrebbe restituire utf8mb4 per MySQL una volta risolto il driver. È completato quando new DbConfig(['dsn' => 'mysql:host=localhost;dbname=example'])->getCharset() restituisce utf8mb4 e il driver è impostato, con un test unitario che copra questo caso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug repo:cms
What happened?

When a MySQL DSN is supplied through a database config array, DbConfig::$driver remains null. This causes getCharset() to return utf8 instead of normalizing it to utf8mb4.

On MariaDB, creating a table with Craft’s default collation then produces an incompatible combination:

DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8mb4_unicode_ci

MariaDB rejects this with:

COLLATION 'utf8mb4_unicode_ci' is not valid for CHARACTER SET 'utf8mb3'
Steps to reproduce

This can be reproduced without connecting to a database:

$config = new \craft\config\DbConfig([
    'dsn' => 'mysql:host=localhost;dbname=example',
]);

var_dump($config->driver);       // null
var_dump($config->getCharset()); // "utf8"

For comparison, explicitly supplying the driver returns the expected charset:

$config = new \craft\config\DbConfig([
    'dsn' => 'mysql:host=localhost;dbname=example',
    'driver' => 'mysql',
]);

var_dump($config->getCharset()); // "utf8mb4"
Expected behavior

Craft should recognize the MySQL driver from the DSN and normalize the default charset to utf8mb4, consistently with the fluent dsn() method.

Additional information

DbConfig::init() skips _updateDsn() when a DSN is already supplied, leaving the driver unset. Meanwhile, App::dbConfig() reads the driver directly from the DSN, so the connection can work despite the missing configured driver.

Explicitly setting charset to utf8mb4 works around the issue.

Found while investigating Blitz #929.

Tested on Craft 5.11.5.1, but may affect other versions as well.

Craft CMS version

5.11.5.1

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions
Lingua principale
PHP
Stelle
3.6k
Fork
712
Merge medio
12h 40m
PR unite (30g)
344

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di craftcms/cms

Tutte le issue di craftcms/cms

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.