fix(eloquent): talk to MySQL the way WordPress does - #115
Merged
Merged
Conversation
The MySQL connection hard-coded utf8 / utf8_unicode_ci and passed DB_HOST
as the host. Measured on wpbones.test (MySQL 8.4, tables utf8mb4, DB_CHARSET
'utf8' that WordPress raises to utf8mb4):
- writing an emoji through Eloquent failed with error 3988, "Conversion
from collation utf8mb3_unicode_ci into utf8mb4_unicode_520_ci impossible";
- an emoji written by WordPress came back through Eloquent as "?";
- DB_HOST 'localhost:/tmp/mysql.sock' did not connect ("Can't assign
requested address"). host:port already did: PDO accepts it.
The charset and collation are now the ones WordPress settled on
($wpdb->charset, $wpdb->collate; an empty collation is left out rather than
sent as `collate ''`), and DB_HOST goes through $wpdb->parse_db_host() as in
wpdb::db_connect(): port, unix socket, and an IPv6 host bracketed for
mysqlnd. Without $wpdb the host is passed as it is and the charset is
utf8mb4.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Updates Eloquent’s MySQL connection to match WordPress’s charset, collation, and host parsing while preserving SQLite support.
Changes:
- Uses
$wpdbsettings with anutf8mb4fallback. - Supports ports, Unix sockets, and IPv6 hosts.
- Adds unit coverage for MySQL configuration variants.
| File | Description |
|---|---|
tests/Unit/EloquentTest.php |
Tests charset, collation, host parsing, and fallback behavior. |
src/Database/Eloquent.php |
Implements WordPress-compatible MySQL connection settings. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This was referenced Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #113, found while fixing #110.
The problem
The MySQL connection hard-coded
utf8/utf8_unicode_ciand passedDB_HOSTas the host. Measured on a local site: MySQL 8.4, tablesutf8mb4_unicode_520_ci,DB_CHARSET'utf8'(which WordPress raises toutf8mb4itself), and asql_modewithoutSTRICT_*.SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8mb3_unicode_ci into utf8mb4_unicode_520_ci impossible for parameter.emoji ? ok.DB_HOSTlocalhost:/tmp/mysql.sockdid not connect:SQLSTATE[HY000] [2002] Can't assign requested address.host:portalready worked, because PDO accepts it.The fix
The connection now follows WordPress's own:
$wpdb->charsetand$wpdb->collate, what WordPress actually uses on its connection. NotDB_CHARSET, which here saysutf8. An empty collation is left out instead of being sent ascollate ''.DB_HOSTgoes through$wpdb->parse_db_host(), as inwpdb::db_connect(). That gives Laravel aport, aunix_socket, and an IPv6 host in brackets when mysqlnd is loaded.$wpdb: the host is passed as it is and the charset isutf8mb4.SQLite (#113) is unchanged.
Tests
tests/Unit/EloquentTest.phphas 10 tests, 6 of them new. A stand-in$wpdbanswersparse_db_host()the way WordPress does, and the tests check how that answer becomes Laravel's connection: charset and collation, port, socket, IPv6 brackets, empty collation, an unparsable host, no$wpdb. The full suite passes: 158 tests.eloquent-live-smoke.shin the workspace, in the bench (MySQL) and in a local@wp-playground/cli(SQLite):utf8mb4,utf8mb4_unicode_520_ci)utf8/utf8_unicode_ciemoji ? okDB_HOSTlocalhost,localhost:3306,127.0.0.1:3306connectDB_HOSTlocalhost:/tmp/mysql.sockconnectsThe IPv6 form isn't in the live run: this MySQL listens on
127.0.0.1only, and mysqli refuses[::1]too. The brackets are covered by the unit test, which mirrorswpdb::db_connect().