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

Abierto Apto para principiantes
#20,248 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
62/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
mariadb, mysql, php
Área
databases

Línea de trabajo

Empieza con DbConfig::init() en las clases de configuración de Craft, que omite _updateDsn() cuando se proporciona un DSN, por lo que driver se queda en null. App::dbConfig() lee el driver del DSN, así que compara ambos. getCharset() debería devolver utf8mb4 para MySQL una vez resuelto el driver. Está terminado cuando new DbConfig(['dsn' => 'mysql:host=localhost;dbname=example'])->getCharset() devuelve utf8mb4 y el driver está establecido, con una prueba unitaria que cubra este caso.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
PHP
Estrellas
3.6k
Forks
712
Merge medio
13 h 5 min
PR fusionados (30 d)
328

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de craftcms/cms

Todos los issues de craftcms/cms

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.