@@ -80,6 +80,8 @@ class DbSchemaWriter implements DbSchemaWriterInterface
80
80
*/
81
81
private $ columnConfig ;
82
82
83
+ private const COLUMN_TYPE = ['varchar ' , 'char ' , 'text ' , 'mediumtext ' , 'longtext ' ];
84
+
83
85
/***
84
86
* @param ResourceConnection $resourceConnection
85
87
* @param StatementFactory $statementFactory
@@ -106,6 +108,15 @@ public function __construct(
106
108
*/
107
109
public function createTable ($ tableName , $ resource , array $ definition , array $ options )
108
110
{
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
+ }
109
120
$ sql = sprintf (
110
121
"( \n%s \n) ENGINE=%s DEFAULT CHARSET=%s DEFAULT COLLATE=%s %s " ,
111
122
implode (", \n" , $ definition ),
@@ -226,13 +237,10 @@ public function modifyTableOption($tableName, $resource, $optionName, $optionVal
226
237
*/
227
238
public function modifyColumn ($ columnName , $ resource , $ tableName , $ columnDefinition )
228
239
{
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 );
235
242
}
243
+
236
244
$ sql = sprintf (
237
245
'MODIFY COLUMN %s ' ,
238
246
$ columnDefinition
@@ -404,4 +412,20 @@ private function applyCharsetAndCollation(string $columnDefinition): string
404
412
return implode (" " , $ columnsAttribute );
405
413
}
406
414
}
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
+ }
407
431
}
0 commit comments