@@ -77,7 +77,7 @@ class DbSchemaWriter implements DbSchemaWriterInterface
7777 */
7878 private DtoFactoriesTable $ columnConfig ;
7979
80- private const COLUMN_TYPE = ['varchar ' , 'char ' , 'text ' , 'mediumtext ' , 'longtext ' ];
80+ private const TEXTUAL_COLUMN_TYPES = ['varchar ' , 'char ' , 'text ' , 'mediumtext ' , 'longtext ' ];
8181
8282 /**
8383 * @param ResourceConnection $resourceConnection
@@ -110,7 +110,7 @@ public function createTable($tableName, $resource, array $definition, array $opt
110110 {
111111 if (count ($ definition )) {
112112 foreach ($ definition as $ index => $ value ) {
113- if ($ this ->isColumnExists ($ value , self ::COLUMN_TYPE )) {
113+ if ($ this ->isColumnTypes ($ value , self ::TEXTUAL_COLUMN_TYPES )) {
114114 if (str_contains ($ index , 'column ' )) {
115115 $ definition [$ index ] = $ this ->setDefaultCharsetAndCollation ($ value );
116116 }
@@ -192,6 +192,11 @@ private function getDropElementSQL($type, $name)
192192 public function addElement ($ elementName , $ resource , $ tableName , $ elementDefinition , $ elementType )
193193 {
194194 $ addElementSyntax = $ elementType === Column::TYPE ? 'ADD COLUMN %s ' : 'ADD %s ' ;
195+ if ($ elementType === Column::TYPE ) {
196+ if ($ this ->isColumnTypes ($ elementDefinition , self ::TEXTUAL_COLUMN_TYPES )) {
197+ $ elementDefinition = $ this ->setDefaultCharsetAndCollation ($ elementDefinition );
198+ }
199+ }
195200 $ sql = sprintf (
196201 $ addElementSyntax ,
197202 $ elementDefinition
@@ -237,7 +242,7 @@ public function modifyTableOption($tableName, $resource, $optionName, $optionVal
237242 */
238243 public function modifyColumn ($ columnName , $ resource , $ tableName , $ columnDefinition )
239244 {
240- if ($ this ->isColumnExists ($ columnDefinition , self ::COLUMN_TYPE )) {
245+ if ($ this ->isColumnTypes ($ columnDefinition , self ::TEXTUAL_COLUMN_TYPES )) {
241246 $ columnDefinition = $ this ->setDefaultCharsetAndCollation ($ columnDefinition );
242247 }
243248
@@ -499,16 +504,16 @@ private function setDefaultCharsetAndCollation(string $columnDefinition): string
499504 }
500505
501506 /**
502- * Checks if any column of type varchar,char or text (mediumtext/longtext)
507+ * Checks if any column is of the types passed in $columnTypes
503508 *
504509 * @param string $definition
505- * @param array $columntypes
510+ * @param array $columnTypes
506511 * @return bool
507512 */
508- private function isColumnExists (string $ definition , array $ columntypes ): bool
513+ private function isColumnTypes (string $ definition , array $ columnTypes ): bool
509514 {
510515 $ type = explode (' ' , $ definition );
511- $ pattern = '/\b( ' . implode ('| ' , array_map ('preg_quote ' , $ columntypes )) . ')\b/i ' ;
516+ $ pattern = '/\b( ' . implode ('| ' , array_map ('preg_quote ' , $ columnTypes )) . ')\b/i ' ;
512517 return preg_match ($ pattern , $ type [1 ]) === 1 ;
513518 }
514519}
0 commit comments