Skip to content

Commit 5eb298b

Browse files
authored
Merge pull request #11914 from xabbuh/dbal-6886
do not use deprecated index features
2 parents 28575f5 + 5eb0255 commit 5eb298b

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

phpstan-baseline.neon

+48
Original file line numberDiff line numberDiff line change
@@ -1368,12 +1368,24 @@ parameters:
13681368
count: 1
13691369
path: src/Mapping/Driver/DatabaseDriver.php
13701370

1371+
-
1372+
message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\Index\:\:getIndexedColumns\(\)\.$#'
1373+
identifier: method.notFound
1374+
count: 1
1375+
path: src/Mapping/Driver/DatabaseDriver.php
1376+
13711377
-
13721378
message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\Table\:\:getPrimaryKeyConstraint\(\)\.$#'
13731379
identifier: method.notFound
13741380
count: 1
13751381
path: src/Mapping/Driver/DatabaseDriver.php
13761382

1383+
-
1384+
message: '#^Call to method getColumnName\(\) on an unknown class Doctrine\\DBAL\\Schema\\Index\\IndexedColumn\.$#'
1385+
identifier: class.notFound
1386+
count: 1
1387+
path: src/Mapping/Driver/DatabaseDriver.php
1388+
13771389
-
13781390
message: '#^Call to method getColumnNames\(\) on an unknown class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint\.$#'
13791391
identifier: class.notFound
@@ -1392,6 +1404,12 @@ parameters:
13921404
count: 1
13931405
path: src/Mapping/Driver/DatabaseDriver.php
13941406

1407+
-
1408+
message: '#^Class Doctrine\\DBAL\\Schema\\Index\\IndexType not found\.$#'
1409+
identifier: class.notFound
1410+
count: 1
1411+
path: src/Mapping/Driver/DatabaseDriver.php
1412+
13951413
-
13961414
message: '#^Class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint not found\.$#'
13971415
identifier: class.notFound
@@ -1434,12 +1452,24 @@ parameters:
14341452
count: 2
14351453
path: src/Mapping/Driver/DatabaseDriver.php
14361454

1455+
-
1456+
message: '#^Parameter \#1 \$array of function sort contains unresolvable type\.$#'
1457+
identifier: argument.unresolvableType
1458+
count: 1
1459+
path: src/Mapping/Driver/DatabaseDriver.php
1460+
14371461
-
14381462
message: '#^Parameter \#2 \$columnName of method Doctrine\\ORM\\Mapping\\Driver\\DatabaseDriver\:\:getFieldNameForColumn\(\) expects string, string\|false given\.$#'
14391463
identifier: argument.type
14401464
count: 4
14411465
path: src/Mapping/Driver/DatabaseDriver.php
14421466

1467+
-
1468+
message: '#^Parameter \$indexedColumn of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Index\\IndexedColumn\.$#'
1469+
identifier: class.notFound
1470+
count: 1
1471+
path: src/Mapping/Driver/DatabaseDriver.php
1472+
14431473
-
14441474
message: '#^Parameter \$name of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
14451475
identifier: class.notFound
@@ -3114,6 +3144,12 @@ parameters:
31143144
count: 1
31153145
path: src/Tools/SchemaTool.php
31163146

3147+
-
3148+
message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\Index\:\:getIndexedColumns\(\)\.$#'
3149+
identifier: method.notFound
3150+
count: 1
3151+
path: src/Tools/SchemaTool.php
3152+
31173153
-
31183154
message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\Table\:\:dropForeignKey\(\)\.$#'
31193155
identifier: method.notFound
@@ -3126,6 +3162,12 @@ parameters:
31263162
count: 1
31273163
path: src/Tools/SchemaTool.php
31283164

3165+
-
3166+
message: '#^Call to method getColumnName\(\) on an unknown class Doctrine\\DBAL\\Schema\\Index\\IndexedColumn\.$#'
3167+
identifier: class.notFound
3168+
count: 1
3169+
path: src/Tools/SchemaTool.php
3170+
31293171
-
31303172
message: '#^Call to method getColumnNames\(\) on an unknown class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint\.$#'
31313173
identifier: class.notFound
@@ -3246,6 +3288,12 @@ parameters:
32463288
count: 1
32473289
path: src/Tools/SchemaTool.php
32483290

3291+
-
3292+
message: '#^Parameter \$indexedColumn of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Index\\IndexedColumn\.$#'
3293+
identifier: class.notFound
3294+
count: 1
3295+
path: src/Tools/SchemaTool.php
3296+
32493297
-
32503298
message: '#^Parameter \$name of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
32513299
identifier: class.notFound

src/Mapping/Driver/DatabaseDriver.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Doctrine\DBAL\Schema\AbstractSchemaManager;
88
use Doctrine\DBAL\Schema\Column;
99
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
10+
use Doctrine\DBAL\Schema\Index;
11+
use Doctrine\DBAL\Schema\Index\IndexedColumn;
12+
use Doctrine\DBAL\Schema\Index\IndexType;
1013
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1114
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1215
use Doctrine\DBAL\Schema\SchemaException;
@@ -29,6 +32,7 @@
2932
use function assert;
3033
use function count;
3134
use function current;
35+
use function enum_exists;
3236
use function get_debug_type;
3337
use function in_array;
3438
use function method_exists;
@@ -291,7 +295,7 @@ private function reverseEngineerMappingFromDatabase(): void
291295
if ($primaryKey instanceof PrimaryKeyConstraint) {
292296
$pkColumns = array_map(static fn (UnqualifiedName $name) => $name->toString(), $primaryKey->getColumnNames());
293297
} else {
294-
$pkColumns = $primaryKey->getColumns();
298+
$pkColumns = self::getIndexedColumns($primaryKey);
295299
}
296300

297301
sort($pkColumns);
@@ -323,9 +327,15 @@ private function buildIndexes(ClassMetadata $metadata): void
323327
continue;
324328
}
325329

330+
if (enum_exists(IndexType::class) && method_exists($index, 'getType')) {
331+
$isUnique = $index->getType() === IndexType::UNIQUE;
332+
} else {
333+
$isUnique = $index->isUnique();
334+
}
335+
326336
$indexName = $index->getName();
327-
$indexColumns = $index->getColumns();
328-
$constraintType = $index->isUnique()
337+
$indexColumns = self::getIndexedColumns($index);
338+
$constraintType = $isUnique
329339
? 'uniqueConstraints'
330340
: 'indexes';
331341

@@ -500,7 +510,7 @@ private function getTablePrimaryKeys(Table $table): array
500510
return array_map(static fn (UnqualifiedName $name) => $name->toString(), $table->getPrimaryKeyConstraint()->getColumnNames());
501511
}
502512

503-
return $table->getPrimaryKey()->getColumns();
513+
return self::getIndexedColumns($table->getPrimaryKey());
504514
} catch (SchemaException) {
505515
// Do nothing
506516
}
@@ -574,4 +584,14 @@ private static function getReferencedColumnNames(ForeignKeyConstraint $foreignKe
574584

575585
return $foreignKey->getForeignColumns();
576586
}
587+
588+
/** @return string[] */
589+
private static function getIndexedColumns(Index $index): array
590+
{
591+
if (method_exists(Index::class, 'getIndexedColumns')) {
592+
return array_map(static fn (IndexedColumn $indexedColumn) => $indexedColumn->getColumnName()->toString(), $index->getIndexedColumns());
593+
}
594+
595+
return $index->getColumns();
596+
}
577597
}

src/Tools/SchemaTool.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1111
use Doctrine\DBAL\Schema\ForeignKeyConstraintEditor;
1212
use Doctrine\DBAL\Schema\Index;
13+
use Doctrine\DBAL\Schema\Index\IndexedColumn;
1314
use Doctrine\DBAL\Schema\Name\Identifier;
1415
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1516
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
@@ -322,7 +323,7 @@ public function getSchemaFromMetadata(array $classes): Schema
322323
$primaryKey = $table->getIndex('primary');
323324

324325
foreach ($table->getIndexes() as $idxKey => $existingIndex) {
325-
if ($existingIndex !== $primaryKey && $primaryKey->spansColumns($existingIndex->getColumns())) {
326+
if ($existingIndex !== $primaryKey && $primaryKey->spansColumns(self::getIndexedColumns($existingIndex))) {
326327
$table->dropIndex($idxKey);
327328
}
328329
}
@@ -353,7 +354,7 @@ public function getSchemaFromMetadata(array $classes): Schema
353354
}
354355
}
355356

356-
$table->addUniqueIndex($uniqIndex->getColumns(), is_numeric($indexName) ? null : $indexName, $indexData['options'] ?? []);
357+
$table->addUniqueIndex(self::getIndexedColumns($uniqIndex), is_numeric($indexName) ? null : $indexName, $indexData['options'] ?? []);
357358
}
358359
}
359360

@@ -874,7 +875,7 @@ public function getDropSchemaSQL(array $classes): array
874875
if ($primaryKey instanceof PrimaryKeyConstraint) {
875876
$columns = array_map(static fn (UnqualifiedName $name) => $name->toString(), $primaryKey->getColumnNames());
876877
} else {
877-
$columns = $primaryKey->getColumns();
878+
$columns = self::getIndexedColumns($primaryKey);
878879
}
879880

880881
if (count($columns) === 1) {
@@ -967,4 +968,14 @@ private function addPrimaryKeyConstraint(Table $table, array $primaryKeyColumns)
967968
$table->setPrimaryKey($primaryKeyColumns);
968969
}
969970
}
971+
972+
/** @return string[] */
973+
private static function getIndexedColumns(Index $index): array
974+
{
975+
if (method_exists(Index::class, 'getIndexedColumns')) {
976+
return array_map(static fn (IndexedColumn $indexedColumn) => $indexedColumn->getColumnName()->toString(), $index->getIndexedColumns());
977+
}
978+
979+
return $index->getColumns();
980+
}
970981
}

tests/Tests/ORM/Tools/SchemaToolTest.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Doctrine\Common\Collections\Collection;
88
use Doctrine\DBAL\Schema\ForeignKeyConstraintEditor;
9+
use Doctrine\DBAL\Schema\Index as DbalIndex;
10+
use Doctrine\DBAL\Schema\Index\IndexedColumn;
11+
use Doctrine\DBAL\Schema\Index\IndexType;
912
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1013
use Doctrine\DBAL\Schema\PrimaryKeyConstraintEditor;
1114
use Doctrine\DBAL\Schema\Table as DbalTable;
@@ -50,6 +53,8 @@
5053
use function class_exists;
5154
use function count;
5255
use function current;
56+
use function enum_exists;
57+
use function method_exists;
5358

5459
class SchemaToolTest extends OrmTestCase
5560
{
@@ -294,11 +299,11 @@ public function testDerivedCompositeKey(): void
294299
} else {
295300
$rootTable = $schema->getTable('joined_derived_root');
296301
self::assertNotNull($rootTable->getPrimaryKey());
297-
self::assertSame(['keyPart1_id', 'keyPart2'], $rootTable->getPrimaryKey()->getColumns());
302+
self::assertSame(['keyPart1_id', 'keyPart2'], self::getIndexedColumns($rootTable->getPrimaryKey()));
298303

299304
$childTable = $schema->getTable('joined_derived_child');
300305
self::assertNotNull($childTable->getPrimaryKey());
301-
self::assertSame(['keyPart1_id', 'keyPart2'], $childTable->getPrimaryKey()->getColumns());
306+
self::assertSame(['keyPart1_id', 'keyPart2'], self::getIndexedColumns($childTable->getPrimaryKey()));
302307
}
303308

304309
$childTableForeignKeys = $childTable->getForeignKeys();
@@ -339,8 +344,8 @@ public function testIndexesBasedOnFields(): void
339344
$schema = $schemaTool->getSchemaFromMetadata([$metadata]);
340345
$table = $schema->getTable('field_index');
341346

342-
self::assertEquals(['index', 'field_name'], $table->getIndex('index')->getColumns());
343-
self::assertEquals(['index', 'table'], $table->getIndex('uniq')->getColumns());
347+
self::assertEquals(['index', 'field_name'], self::getIndexedColumns($table->getIndex('index')));
348+
self::assertEquals(['index', 'table'], self::getIndexedColumns($table->getIndex('uniq')));
344349
}
345350

346351
public function testIncorrectIndexesBasedOnFields(): void
@@ -418,8 +423,23 @@ public function testLoadUniqueConstraintWithoutName(): void
418423

419424
$tableIndex = $tableEntity->getIndex('uniq_2d81a3ed5bf54558875f7fd5');
420425

421-
self::assertTrue($tableIndex->isUnique());
422-
self::assertSame(['field', 'anotherField'], $tableIndex->getColumns());
426+
if (enum_exists(IndexType::class)) {
427+
self::assertSame(IndexType::UNIQUE, $tableIndex->getType());
428+
} else {
429+
self::assertTrue($tableIndex->isUnique());
430+
}
431+
432+
self::assertSame(['field', 'anotherField'], self::getIndexedColumns($tableIndex));
433+
}
434+
435+
/** @return string[] */
436+
private static function getIndexedColumns(DbalIndex $index): array
437+
{
438+
if (method_exists(DbalIndex::class, 'getIndexedColumns')) {
439+
return array_map(static fn (IndexedColumn $indexedColumn) => $indexedColumn->getColumnName()->toString(), $index->getIndexedColumns());
440+
}
441+
442+
return $index->getColumns();
423443
}
424444

425445
private static function columnIsIndexed(DbalTable $table, string $column): bool

0 commit comments

Comments
 (0)