Skip to content

Commit 457d2d2

Browse files
committed
do not use deprecated primary key constraint features
1 parent a2516b6 commit 457d2d2

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

phpstan-baseline.neon

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,10 +1368,22 @@ parameters:
13681368
count: 1
13691369
path: src/Mapping/Driver/DatabaseDriver.php
13701370

1371+
-
1372+
message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\Table\:\:getPrimaryKeyConstraint\(\)\.$#'
1373+
identifier: method.notFound
1374+
count: 1
1375+
path: src/Mapping/Driver/DatabaseDriver.php
1376+
1377+
-
1378+
message: '#^Call to method getColumnNames\(\) on an unknown class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint\.$#'
1379+
identifier: class.notFound
1380+
count: 1
1381+
path: src/Mapping/Driver/DatabaseDriver.php
1382+
13711383
-
13721384
message: '#^Call to method toString\(\) on an unknown class Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
13731385
identifier: class.notFound
1374-
count: 2
1386+
count: 4
13751387
path: src/Mapping/Driver/DatabaseDriver.php
13761388

13771389
-
@@ -1380,6 +1392,12 @@ parameters:
13801392
count: 1
13811393
path: src/Mapping/Driver/DatabaseDriver.php
13821394

1395+
-
1396+
message: '#^Class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint not found\.$#'
1397+
identifier: class.notFound
1398+
count: 1
1399+
path: src/Mapping/Driver/DatabaseDriver.php
1400+
13831401
-
13841402
message: '#^Instanceof between Doctrine\\ORM\\Mapping\\ClassMetadata\<T of object\> and Doctrine\\ORM\\Mapping\\ClassMetadata will always evaluate to true\.$#'
13851403
identifier: instanceof.alwaysTrue
@@ -1425,7 +1443,7 @@ parameters:
14251443
-
14261444
message: '#^Parameter \$name of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
14271445
identifier: class.notFound
1428-
count: 2
1446+
count: 4
14291447
path: src/Mapping/Driver/DatabaseDriver.php
14301448

14311449
-
@@ -3108,10 +3126,22 @@ parameters:
31083126
count: 1
31093127
path: src/Tools/SchemaTool.php
31103128

3129+
-
3130+
message: '#^Call to method getColumnNames\(\) on an unknown class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint\.$#'
3131+
identifier: class.notFound
3132+
count: 1
3133+
path: src/Tools/SchemaTool.php
3134+
31113135
-
31123136
message: '#^Call to method toString\(\) on an unknown class Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
31133137
identifier: class.notFound
3114-
count: 2
3138+
count: 3
3139+
path: src/Tools/SchemaTool.php
3140+
3141+
-
3142+
message: '#^Class Doctrine\\DBAL\\Schema\\PrimaryKeyConstraint not found\.$#'
3143+
identifier: class.notFound
3144+
count: 1
31153145
path: src/Tools/SchemaTool.php
31163146

31173147
-
@@ -3219,7 +3249,7 @@ parameters:
32193249
-
32203250
message: '#^Parameter \$name of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
32213251
identifier: class.notFound
3222-
count: 2
3252+
count: 3
32233253
path: src/Tools/SchemaTool.php
32243254

32253255
-

src/Mapping/Driver/DatabaseDriver.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Schema\Column;
99
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1010
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
11+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1112
use Doctrine\DBAL\Schema\SchemaException;
1213
use Doctrine\DBAL\Schema\Table;
1314
use Doctrine\DBAL\Types\Type;
@@ -274,15 +275,24 @@ private function reverseEngineerMappingFromDatabase(): void
274275
$allForeignKeyColumns = array_merge($allForeignKeyColumns, self::getReferencingColumnNames($foreignKey));
275276
}
276277

277-
$primaryKey = $table->getPrimaryKey();
278+
if (method_exists($table, 'getPrimaryKeyConstraint')) {
279+
$primaryKey = $table->getPrimaryKeyConstraint();
280+
} else {
281+
$primaryKey = $table->getPrimaryKey();
282+
}
283+
278284
if ($primaryKey === null) {
279285
throw new MappingException(
280286
'Table ' . $tableName . ' has no primary key. Doctrine does not ' .
281287
"support reverse engineering from tables that don't have a primary key.",
282288
);
283289
}
284290

285-
$pkColumns = $primaryKey->getColumns();
291+
if ($primaryKey instanceof PrimaryKeyConstraint) {
292+
$pkColumns = array_map(static fn (UnqualifiedName $name) => $name->toString(), $primaryKey->getColumnNames());
293+
} else {
294+
$pkColumns = $primaryKey->getColumns();
295+
}
286296

287297
sort($pkColumns);
288298
sort($allForeignKeyColumns);
@@ -486,6 +496,10 @@ private function buildToOneAssociationMappings(ClassMetadata $metadata): void
486496
private function getTablePrimaryKeys(Table $table): array
487497
{
488498
try {
499+
if (method_exists($table, 'getPrimaryKeyConstraints')) {
500+
return array_map(static fn (UnqualifiedName $name) => $name->toString(), $table->getPrimaryKeyConstraint()->getColumnNames());
501+
}
502+
489503
return $table->getPrimaryKey()->getColumns();
490504
} catch (SchemaException) {
491505
// Do nothing

src/Tools/SchemaTool.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use function implode;
4444
use function in_array;
4545
use function is_numeric;
46+
use function method_exists;
4647
use function strtolower;
4748

4849
/**
@@ -321,7 +322,7 @@ public function getSchemaFromMetadata(array $classes): Schema
321322
$primaryKey = $table->getIndex('primary');
322323

323324
foreach ($table->getIndexes() as $idxKey => $existingIndex) {
324-
if (! $existingIndex->isPrimary() && $primaryKey->spansColumns($existingIndex->getColumns())) {
325+
if ($existingIndex !== $primaryKey && $primaryKey->spansColumns($existingIndex->getColumns())) {
325326
$table->dropIndex($idxKey);
326327
}
327328
}
@@ -860,12 +861,22 @@ public function getDropSchemaSQL(array $classes): array
860861
}
861862

862863
foreach ($schema->getTables() as $table) {
863-
$primaryKey = $table->getPrimaryKey();
864+
if (method_exists($table, 'getPrimaryKeyConstraint')) {
865+
$primaryKey = $table->getPrimaryKeyConstraint();
866+
} else {
867+
$primaryKey = $table->getPrimaryKey();
868+
}
869+
864870
if ($primaryKey === null) {
865871
continue;
866872
}
867873

868-
$columns = $primaryKey->getColumns();
874+
if ($primaryKey instanceof PrimaryKeyConstraint) {
875+
$columns = array_map(static fn (UnqualifiedName $name) => $name->toString(), $primaryKey->getColumnNames());
876+
} else {
877+
$columns = $primaryKey->getColumns();
878+
}
879+
869880
if (count($columns) === 1) {
870881
$checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
871882
if ($deployedSchema->hasSequence($checkSequence) && ! $schema->hasSequence($checkSequence)) {

tests/Tests/ORM/Tools/SchemaToolTest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\Common\Collections\Collection;
88
use Doctrine\DBAL\Schema\ForeignKeyConstraintEditor;
99
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
10+
use Doctrine\DBAL\Schema\PrimaryKeyConstraintEditor;
1011
use Doctrine\DBAL\Schema\Table as DbalTable;
1112
use Doctrine\ORM\Mapping\ClassMetadata;
1213
use Doctrine\ORM\Mapping\Column;
@@ -234,10 +235,15 @@ public function testRemoveUniqueIndexOverruledByPrimaryKey(): void
234235

235236
self::assertTrue($schema->hasTable('first_entity'), 'Table first_entity should exist.');
236237

237-
$indexes = $schema->getTable('first_entity')->getIndexes();
238+
$table = $schema->getTable('first_entity');
239+
240+
self::assertTrue($table->hasIndex('primary'), 'Table should have a primary key.');
241+
242+
$primaryKey = $table->getIndex('primary');
243+
$indexes = $table->getIndexes();
238244

239245
self::assertCount(1, $indexes, 'there should be only one index');
240-
self::assertTrue(current($indexes)->isPrimary(), 'index should be primary');
246+
self::assertSame($primaryKey, current($indexes), 'index should be primary');
241247
}
242248

243249
public function testSetDiscriminatorColumnWithoutLength(): void
@@ -277,13 +283,23 @@ public function testDerivedCompositeKey(): void
277283
self::assertTrue($schema->hasTable('joined_derived_root'));
278284
self::assertTrue($schema->hasTable('joined_derived_child'));
279285

280-
$rootTable = $schema->getTable('joined_derived_root');
281-
self::assertNotNull($rootTable->getPrimaryKey());
282-
self::assertSame(['keyPart1_id', 'keyPart2'], $rootTable->getPrimaryKey()->getColumns());
283-
284-
$childTable = $schema->getTable('joined_derived_child');
285-
self::assertNotNull($childTable->getPrimaryKey());
286-
self::assertSame(['keyPart1_id', 'keyPart2'], $childTable->getPrimaryKey()->getColumns());
286+
if (class_exists(PrimaryKeyConstraintEditor::class)) {
287+
$rootTable = $schema->getTable('joined_derived_root');
288+
self::assertNotNull($rootTable->getPrimaryKeyConstraint());
289+
self::assertSame(['keyPart1_id', 'keyPart2'], array_map(static fn (UnqualifiedName $name) => $name->toString(), $rootTable->getPrimaryKeyConstraint()->getColumnNames()));
290+
291+
$childTable = $schema->getTable('joined_derived_child');
292+
self::assertNotNull($childTable->getPrimaryKeyConstraint());
293+
self::assertSame(['keyPart1_id', 'keyPart2'], array_map(static fn (UnqualifiedName $name) => $name->toString(), $childTable->getPrimaryKeyConstraint()->getColumnNames()));
294+
} else {
295+
$rootTable = $schema->getTable('joined_derived_root');
296+
self::assertNotNull($rootTable->getPrimaryKey());
297+
self::assertSame(['keyPart1_id', 'keyPart2'], $rootTable->getPrimaryKey()->getColumns());
298+
299+
$childTable = $schema->getTable('joined_derived_child');
300+
self::assertNotNull($childTable->getPrimaryKey());
301+
self::assertSame(['keyPart1_id', 'keyPart2'], $childTable->getPrimaryKey()->getColumns());
302+
}
287303

288304
$childTableForeignKeys = $childTable->getForeignKeys();
289305

0 commit comments

Comments
 (0)