2929use Doctrine \DBAL \Schema \Column ;
3030use Doctrine \DBAL \Schema \SchemaException ;
3131
32+ use function array_intersect ;
33+ use function array_map ;
34+ use function count ;
35+ use function implode ;
36+
3237/**
3338 * This migration changes all database columns to allow null values.
3439 *
@@ -43,6 +48,9 @@ class AllowNullMigration extends AbstractMigration
4348 */
4449 private Connection $ connection ;
4550
51+ /** @var list<string> */
52+ private array $ existsCache = [];
53+
4654 /**
4755 * Create a new instance.
4856 *
@@ -73,9 +81,7 @@ public function getName(): string
7381 */
7482 public function shouldRun (): bool
7583 {
76- $ schemaManager = $ this ->connection ->createSchemaManager ();
77-
78- if (!$ schemaManager ->tablesExist (['tl_metamodel ' , 'tl_metamodel_attribute ' ])) {
84+ if (!$ this ->tablesExist (['tl_metamodel ' , 'tl_metamodel_attribute ' ])) {
7985 return false ;
8086 }
8187
@@ -103,7 +109,7 @@ public function run(): MigrationResult
103109 }
104110 }
105111
106- return new MigrationResult (true , 'Adjusted column(s): ' . \ implode (', ' , $ message ));
112+ return new MigrationResult (true , 'Adjusted column(s): ' . implode (', ' , $ message ));
107113 }
108114
109115 /**
@@ -121,6 +127,10 @@ private function fetchNonNullableColumns(): array
121127
122128 $ result = [];
123129 foreach ($ langColumns as $ tableName => $ tableColumnNames ) {
130+ if (!$ this ->tablesExist ([$ tableName ])) {
131+ continue ;
132+ }
133+
124134 /** @var Column[] $columns */
125135 $ columns = [];
126136 // The schema manager return the column list with lowercase keys, wo got to use the real names.
@@ -205,4 +215,13 @@ private function fixColumn(string $tableName, Column $column): void
205215 ->where ('t. ' . $ column ->getName () . ' = 0 ' )
206216 ->executeQuery ();
207217 }
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+ }
208227}
0 commit comments