Skip to content

Commit 77c3d87

Browse files
committed
Fix migration table exist
1 parent 9a86386 commit 77c3d87

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/Migration/AllowNullMigration.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
use Doctrine\DBAL\Schema\Column;
3030
use 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,7 +127,7 @@ private function fetchNonNullableColumns(): array
121127

122128
$result = [];
123129
foreach ($langColumns as $tableName => $tableColumnNames) {
124-
if (!$schemaManager->tablesExist([$tableName])) {
130+
if (!$this->tablesExist([$tableName])) {
125131
continue;
126132
}
127133

@@ -209,4 +215,13 @@ private function fixColumn(string $tableName, Column $column): void
209215
->where('t.' . $column->getName() . ' = 0')
210216
->executeQuery();
211217
}
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+
}
212227
}

tests/Migration/AllowNullMigrationTest.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public function runConfiguration(): \Generator
5656
]
5757
];
5858

59-
yield 'attribute select not configured' => [
59+
yield 'attribute not configured' => [
6060
(object) [
6161
'requiredTablesExist' => true,
6262
'shouldRun' => false,
6363
'attributeConfigured' => false
6464
]
6565
];
6666

67-
yield 'attribute select is configured' => [
67+
yield 'attribute is configured' => [
6868
(object) [
6969
'requiredTablesExist' => true,
7070
'shouldRun' => false,
@@ -97,14 +97,17 @@ public function testRun(object $configuration): void
9797
$manager = $this
9898
->getMockBuilder(AbstractSchemaManager::class)
9999
->setConstructorArgs([$connection, $plattform])
100-
->onlyMethods(['tablesExist', 'introspectTable', 'alterTable'])
100+
->onlyMethods(['listTableNames', 'introspectTable', 'alterTable'])
101101
->getMockForAbstractClass();
102102

103103
$manager
104104
->expects(self::once())
105-
->method('tablesExist')
106-
->with(['tl_metamodel', 'tl_metamodel_attribute'])
107-
->willReturn($configuration->requiredTablesExist);
105+
->method('listTableNames')
106+
->willReturn(
107+
$configuration->requiredTablesExist
108+
? ['tl_metamodel', 'tl_metamodel_attribute', 'mm_table_1', 'mm_table_2']
109+
: []
110+
);
108111

109112
$connection
110113
->expects(

0 commit comments

Comments
 (0)