Skip to content

Commit

Permalink
Create tables using utf8mb4 by default for MySQL
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
  • Loading branch information
Sesquipedalian committed Jan 30, 2025
1 parent 9cce008 commit d7c25ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Sources/Db/APIs/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1691,8 +1691,12 @@ public function create_table(string $table_name, array $columns, array $indexes

$table_query .= ') ENGINE=' . $parameters['engine'];

if (!empty($this->character_set) && $this->character_set == 'utf8') {
$table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
if (!empty($this->character_set) && str_starts_with($this->character_set, 'utf8')) {
if ($this->mb4) {
$table_query .= ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci';
} else {
$table_query .= ' DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci';
}
}

// Create the table!
Expand Down
12 changes: 8 additions & 4 deletions other/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,12 @@ function DatabaseSettings()
if (Db::$db->name != '' && !$needsDB) {
Db::$db->query(
'',
'CREATE DATABASE IF NOT EXISTS `' . Db::$db->name . '`',
'CREATE DATABASE IF NOT EXISTS {identifier:db_name} {raw:extra}',
[
'security_override' => true,
'db_error_skip' => true,
'db_name' => Db::$db->name,
'extra' => Config::$db_type === 'mysql' ? ' CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci' : '',
],
Db::$db->connection,
);
Expand All @@ -947,10 +949,12 @@ function DatabaseSettings()
if (!Db::$db->select(Db::$db->name, Db::$db->connection) && Db::$db->name != '') {
Db::$db->query(
'',
'CREATE DATABASE IF NOT EXISTS `' . Db::$db->prefix . Db::$db->name . '`',
'CREATE DATABASE IF NOT EXISTS {identifier:db_name} {raw:extra}',
[
'security_override' => true,
'db_error_skip' => true,
'db_name' => Db::$db->prefix . Db::$db->name,
'extra' => Config::$db_type === 'mysql' ? ' CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci' : '',
],
Db::$db->connection,
);
Expand Down Expand Up @@ -1235,8 +1239,8 @@ function DatabasePopulation()
$replaces['{$memory}'] = (!$has_innodb && in_array('MEMORY', $engines)) ? 'MEMORY' : $replaces['{$engine}'];

// UTF-8 is required.
$replaces['{$engine}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
$replaces['{$memory}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
$replaces['{$engine}'] .= ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci';
$replaces['{$memory}'] .= ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci';

// One last thing - if we don't have InnoDB, we can't do transactions...
if (!$has_innodb) {
Expand Down
9 changes: 4 additions & 5 deletions other/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2392,10 +2392,10 @@ function ($errno, $errstr, $errfile, $errline) use ($support_js) {
},
);

// If we're on MySQL, set {db_collation}; this approach is used throughout upgrade_2-0_mysql.php to set new tables to utf8
// If we're on MySQL, set {db_collation}; this approach is used throughout upgrade_2-0_mysql.php to set new tables to utf8mb4
// Note it is expected to be in the format: ENGINE=InnoDB{$db_collation};
if (Config::$db_type == 'mysql') {
$db_collation = ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
$db_collation = ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci';
} else {
$db_collation = '';
}
Expand All @@ -2410,7 +2410,7 @@ function ($errno, $errstr, $errfile, $errline) use ($support_js) {
$last_step = '';

// Make sure all newly created tables will have the proper characters set; this approach is used throughout upgrade_2-1_mysql.php
$lines = preg_replace('/\) ENGINE=(InnoDB|MyISAM);/', ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;', $lines);
$lines = preg_replace('/\) ENGINE=(InnoDB|MyISAM);/', ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;', $lines);

// Count the total number of steps within this file - for progress.
$file_steps = substr_count(implode('', $lines), '---#');
Expand Down Expand Up @@ -3301,8 +3301,7 @@ function ConvertUtf8()
'',
'SHOW INDEX
FROM {db_prefix}messages',
[
],
[],
);

$upcontext['dropping_index'] = false;
Expand Down

0 comments on commit d7c25ff

Please # to comment.