Skip to content

Commit e8d7b84

Browse files
authored
Remove hardcoded default collation for MySQL tables (#1011)
* Remove hardcoded default collation for MySQL tables Let MySQL/MariaDB use the database default collation instead of forcing utf8mb4_unicode_ci on every table. This avoids collation mismatches on servers with different defaults (e.g. MariaDB 11.8 uses utf8mb4_uca1400_ai_ci). Users who need a specific collation can still set it explicitly in their migrations. Fixes #1010 * Add Migrations.default_collation config option Allow configuring a global default collation for MySQL tables via Configure::read('Migrations.default_collation'). When null (default), tables inherit the database server collation. Per-table collation options still take precedence.
1 parent 428f653 commit e8d7b84

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

config/app.example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
'unsigned_primary_keys' => null, // Default false
1010
'unsigned_ints' => null, // Default false, make sure this is aligned with the above config
1111
'column_null_default' => null, // Default false
12+
'default_collation' => null, // Default null (uses database collation). Set to e.g. 'utf8mb4_unicode_ci' to override.
1213
],
1314
];

docs/en/writing-migrations.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,9 @@ In addition, the MySQL adapter supports following options:
546546
Option Platform Description
547547
========== ================ ===========
548548
comment MySQL, Postgres set a text comment on the table
549-
collation MySQL, SqlServer the default collation for a table if different than the database.
549+
collation MySQL, SqlServer set the table collation *(defaults to database collation)*
550550
row_format MySQL set the table row format
551551
engine MySQL define table engine *(defaults to ``InnoDB``)*
552-
collation MySQL define table collation *(defaults to ``utf8mb4_unicode_ci``)*
553552
signed MySQL whether the primary key is ``signed`` *(defaults to ``false``)*
554553
limit MySQL set the maximum length for the primary key
555554
========== ================ ===========

src/Db/Adapter/MysqlAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,13 @@ public function createTable(TableMetadata $table, array $columns = [], array $in
251251
// This method is based on the MySQL docs here: https://dev.mysql.com/doc/refman/5.1/en/create-index.html
252252
$defaultOptions = [
253253
'engine' => 'InnoDB',
254-
'collation' => 'utf8mb4_unicode_ci',
255254
];
256255

256+
$collation = Configure::read('Migrations.default_collation');
257+
if ($collation) {
258+
$defaultOptions['collation'] = $collation;
259+
}
260+
257261
$options = array_merge(
258262
$defaultOptions,
259263
array_intersect_key($this->getOptions(), $defaultOptions),

0 commit comments

Comments
 (0)