[5.x]: MySQL DSN in config array bypasses charset
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
Research direction
Start with DbConfig::init() in the craft config classes, which skips _updateDsn() when a DSN is supplied, so driver stays null; App::dbConfig() reads the driver from the DSN, so compare the two. getCharset() should return utf8mb4 for MySQL once the driver is resolved. Done when new DbConfig(['dsn' => 'mysql:host=localhost;dbname=example'])->getCharset() returns utf8mb4 and the driver is set, with a unit test covering this case.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 712
- Avg merge
- 13h 5m
- Merged PRs (30d)
- 328
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from craftcms/cms
-
bug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
craftcms/cms#20205 · 1 comment ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
craftcms/cms#20203 · 1 comment ·
Maintainers usually reply within 1 day
-
[5.x]: Moving an asset with “Replace it” throws TypeError: volumeUid must be of type string, null givenPossibly taken @i-just claimed this 5 days ago. Openbug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
craftcms/cms#20128 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 4/5 1-2 days Newbie friendliness 55/100
craftcms/cms#20226 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 3/5 1-2 days Newbie friendliness 65/100
craftcms/cms#20204 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
Recognise PHP_CodeSniffer (`phpcs`) as a linterPossibly taken @HarshRajSinghania claimed this today. Openenhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
MustacheCase/zanadir#507 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jwage/phpamqplib-messenger#163 ·
-
Newsletter subscription page logs Undefined array key "nw_error" on every GETPossibly taken @Codencode claimed this today. Open
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
PrestaShop/ps_emailsubscription#145 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100