Skip to content

Commit 4121b85

Browse files
committed
AC-9755:Set default collation to utf8mb4 for MySQL
1 parent 06f8814 commit 4121b85

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface, Rese
149149
*/
150150
private const COLUMN_TYPE = ['varchar', 'char', 'text', 'mediumtext', 'longtext'];
151151

152+
/**
153+
* const for charset
154+
*/
155+
private const CHARSET = "utf8mb4";
156+
157+
/**
158+
* const for collation
159+
*/
160+
private const COLLATION = "utf8mb4_general_ci";
161+
152162
/**
153163
* MySQL column - Table DDL type pairs
154164
*
@@ -1258,7 +1268,11 @@ public function modifyColumn($tableName, $columnName, $definition, $flushData =
12581268
if (is_array($definition)) {
12591269
$definition = $this->_getColumnDefinition($definition);
12601270
}
1261-
1271+
// Add charset and collation for DBC failures
1272+
if(!empty($definition)) {
1273+
$type = explode(' ', trim($definition));
1274+
$definition = $this->applyCharsetAndCollation($type[0], $definition, 1);
1275+
}
12621276
$sql = sprintf(
12631277
'ALTER TABLE %s MODIFY COLUMN %s %s',
12641278
$this->quoteIdentifier($tableName),
@@ -2433,20 +2447,13 @@ protected function _getColumnsDefinition(Table $table)
24332447
$columnDefinition
24342448
);
24352449
}
2436-
2450+
// Adding charset and collation for DBC failures
24372451
if(count($definition)) {
24382452
foreach ($definition as $index => $columnDefinition) {
24392453
$type = explode(' ', trim($columnDefinition));
2440-
$pattern = '/\b(' . implode('|', array_map('preg_quote', self::COLUMN_TYPE)) . ')\b/i';
2441-
if (preg_match($pattern, $type[1]) === 1) {
2442-
$charsets = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci';
2443-
$columnsAttribute = explode(' ', trim($columnDefinition));
2444-
array_splice($columnsAttribute, 2, 0, $charsets);
2445-
$definition[$index] = implode(' ', $columnsAttribute);
2446-
}
2454+
$definition[$index] = $this->applyCharsetAndCollation($type[1], $columnDefinition, 2);
24472455
}
24482456
}
2449-
24502457
// PRIMARY KEY
24512458
if (!empty($primary)) {
24522459
asort($primary, SORT_NUMERIC);
@@ -4301,4 +4308,24 @@ public function __debugInfo()
43014308
{
43024309
return [];
43034310
}
4311+
4312+
/***
4313+
* Adding charset and collation for DBC failures
4314+
*
4315+
* @param $columnType
4316+
* @param $definition
4317+
* @param $position
4318+
* @return string
4319+
*/
4320+
private function applyCharsetAndCollation($columnType, $definition, $position) : string
4321+
{
4322+
$pattern = '/\b(' . implode('|', array_map('preg_quote', self::COLUMN_TYPE)) . ')\b/i';
4323+
if (preg_match($pattern, $columnType) === 1) {
4324+
$charsets = 'CHARACTER SET ' . self::CHARSET. ' COLLATE ' . self::COLLATION;
4325+
$columnsAttribute = explode(' ', trim($definition));
4326+
array_splice($columnsAttribute, $position, 0, $charsets);
4327+
return implode(' ', $columnsAttribute);
4328+
}
4329+
return $definition;
4330+
}
43044331
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/Db/MySQL/DbSchemaWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private function removeConstraint(array $statementsSql): array
396396
}
397397

398398
/***
399-
* Adding charset and collation at column level after column name and column type
399+
* Adding charset and collation for DBC failures
400400
*
401401
* @param string $columnDefinition
402402
* @return string

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ class Installer
275275
private const OLDCHARSET = 'utf8mb3';
276276

277277
/***
278-
* charset and collation for column level
278+
* Charset and collation for column level
279+
* Adding charset and collation for DBC failures
279280
*/
280-
private const COLUMN_ENCODING = " CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
281+
private const COLUMN_ENCODING = " CHARACTER SET ".self::CHARSET." COLLATE ".self::COLLATION;
281282

282283
/**
283284
* Constructor

0 commit comments

Comments
 (0)