29
29
use Doctrine \DBAL \Schema \Column ;
30
30
use Doctrine \DBAL \Schema \SchemaException ;
31
31
32
+ use function array_intersect ;
33
+ use function array_map ;
34
+ use function count ;
35
+ use function implode ;
36
+
32
37
/**
33
38
* This migration changes all database columns to allow null values.
34
39
*
@@ -43,6 +48,9 @@ class AllowNullMigration extends AbstractMigration
43
48
*/
44
49
private Connection $ connection ;
45
50
51
+ /** @var list<string> */
52
+ private array $ existsCache = [];
53
+
46
54
/**
47
55
* Create a new instance.
48
56
*
@@ -73,9 +81,7 @@ public function getName(): string
73
81
*/
74
82
public function shouldRun (): bool
75
83
{
76
- $ schemaManager = $ this ->connection ->createSchemaManager ();
77
-
78
- if (!$ schemaManager ->tablesExist (['tl_metamodel ' , 'tl_metamodel_attribute ' ])) {
84
+ if (!$ this ->tablesExist (['tl_metamodel ' , 'tl_metamodel_attribute ' ])) {
79
85
return false ;
80
86
}
81
87
@@ -103,7 +109,7 @@ public function run(): MigrationResult
103
109
}
104
110
}
105
111
106
- return new MigrationResult (true , 'Adjusted column(s): ' . \ implode (', ' , $ message ));
112
+ return new MigrationResult (true , 'Adjusted column(s): ' . implode (', ' , $ message ));
107
113
}
108
114
109
115
/**
@@ -121,7 +127,7 @@ private function fetchNonNullableColumns(): array
121
127
122
128
$ result = [];
123
129
foreach ($ langColumns as $ tableName => $ tableColumnNames ) {
124
- if (!$ schemaManager ->tablesExist ([$ tableName ])) {
130
+ if (!$ this ->tablesExist ([$ tableName ])) {
125
131
continue ;
126
132
}
127
133
@@ -209,4 +215,13 @@ private function fixColumn(string $tableName, Column $column): void
209
215
->where ('t. ' . $ column ->getName () . ' = 0 ' )
210
216
->executeQuery ();
211
217
}
218
+
219
+ private function tablesExist (array $ tableNames ): bool
220
+ {
221
+ if ([] === $ this ->existsCache ) {
222
+ $ this ->existsCache = \array_values ($ this ->connection ->createSchemaManager ()->listTableNames ());
223
+ }
224
+
225
+ return count ($ tableNames ) === count (array_intersect ($ tableNames , array_map ('strtolower ' , $ this ->existsCache )));
226
+ }
212
227
}
0 commit comments