Skip to content

Commit 6d6a08d

Browse files
committed
AC-9755:Set default collation to utf8mb4 for MySQL
1 parent cc79a95 commit 6d6a08d

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class DbSchemaWriter implements DbSchemaWriterInterface
8080
*/
8181
private $columnConfig;
8282

83+
private const COLUMN_TYPE = ['varchar', 'char', 'text', 'mediumtext', 'longtext'];
84+
8385
/***
8486
* @param ResourceConnection $resourceConnection
8587
* @param StatementFactory $statementFactory
@@ -106,6 +108,15 @@ public function __construct(
106108
*/
107109
public function createTable($tableName, $resource, array $definition, array $options)
108110
{
111+
if (count($definition)) {
112+
foreach ($definition as $index => $value) {
113+
if ($this->isColumnExists($value, self::COLUMN_TYPE)) {
114+
if (str_contains($index, 'column')) {
115+
$definition[$index] = $this->applyCharsetAndCollation($value);
116+
}
117+
}
118+
}
119+
}
109120
$sql = sprintf(
110121
"(\n%s\n) ENGINE=%s DEFAULT CHARSET=%s DEFAULT COLLATE=%s %s",
111122
implode(", \n", $definition),
@@ -226,13 +237,10 @@ public function modifyTableOption($tableName, $resource, $optionName, $optionVal
226237
*/
227238
public function modifyColumn($columnName, $resource, $tableName, $columnDefinition)
228239
{
229-
$columnTypes = ['varchar', 'char', 'text'];
230-
foreach ($columnTypes as $type) {
231-
if (str_contains($columnDefinition, $type)) {
232-
$columnDefinition = $this->applyCharsetAndCollation($columnDefinition);
233-
break;
234-
}
240+
if ($this->isColumnExists($columnDefinition, self::COLUMN_TYPE)) {
241+
$columnDefinition = $this->applyCharsetAndCollation($columnDefinition);
235242
}
243+
236244
$sql = sprintf(
237245
'MODIFY COLUMN %s',
238246
$columnDefinition
@@ -404,4 +412,20 @@ private function applyCharsetAndCollation(string $columnDefinition): string
404412
return implode(" ", $columnsAttribute);
405413
}
406414
}
415+
416+
/***
417+
* Checks if any column of type varchar,char or text (mediumtext/longtext)
418+
*
419+
* @param string $definition
420+
* @param array $columntypes
421+
* @return bool
422+
*/
423+
private function isColumnExists(string $definition, array $columntypes): bool
424+
{
425+
if (!empty($definition)) {
426+
$type = explode(' ', $definition);
427+
$pattern = '/\b(' . implode('|', array_map('preg_quote', $columntypes)) . ')\b/i';
428+
return preg_match($pattern, $type[1]) === 1;
429+
}
430+
}
407431
}

0 commit comments

Comments
 (0)