Skip to content

Commit ff8c27e

Browse files
committed
Fixed schema comparison attempting to remove double foreign keys.
1 parent d5a5a21 commit ff8c27e

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

src/Schema/Comparator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,17 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
275275
foreach ($newForeignKeys as $newKey => $newForeignKey) {
276276
if ($this->diffForeignKey($oldForeignKey, $newForeignKey) === false) {
277277
unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
278-
} else {
279-
if (strtolower($oldForeignKey->getName()) === strtolower($newForeignKey->getName())) {
280-
$droppedForeignKeys[$oldKey] = $oldForeignKey;
281-
$addedForeignKeys[$newKey] = $newForeignKey;
278+
continue 2;
279+
}
282280

283-
unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
284-
}
281+
if (strtolower($oldForeignKey->getName()) !== strtolower($newForeignKey->getName())) {
282+
continue;
285283
}
284+
285+
$droppedForeignKeys[$oldKey] = $oldForeignKey;
286+
$addedForeignKeys[$newKey] = $newForeignKey;
287+
288+
unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
286289
}
287290
}
288291

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Schema;
6+
7+
use Doctrine\DBAL\Platforms\DB2Platform;
8+
use Doctrine\DBAL\Platforms\OraclePlatform;
9+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
10+
use Doctrine\DBAL\Schema\Column;
11+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
12+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
13+
use Doctrine\DBAL\Schema\Table;
14+
use Doctrine\DBAL\Tests\FunctionalTestCase;
15+
use Doctrine\DBAL\Types\Types;
16+
17+
class DoubleForeignKeyConstraintTest extends FunctionalTestCase
18+
{
19+
private AbstractSchemaManager $schemaManager;
20+
21+
protected function setUp(): void
22+
{
23+
$this->schemaManager = $this->connection->createSchemaManager();
24+
}
25+
26+
public function testDoubleForeignKeyConstraint(): void
27+
{
28+
$platform = $this->connection->getDatabasePlatform();
29+
if ($platform instanceof DB2Platform || $platform instanceof OraclePlatform) {
30+
self::markTestSkipped('DB2 and Oracle do not allow multiple FKs with the same columns.');
31+
}
32+
33+
$articles = Table::editor()
34+
->setUnquotedName('articles')
35+
->setColumns(
36+
Column::editor()
37+
->setUnquotedName('id')
38+
->setTypeName(Types::INTEGER)
39+
->create(),
40+
)
41+
->setPrimaryKeyConstraint(
42+
PrimaryKeyConstraint::editor()
43+
->setUnquotedColumnNames('id')
44+
->create(),
45+
)
46+
->create();
47+
48+
$orders = Table::editor()
49+
->setUnquotedName('orders')
50+
->setColumns(
51+
Column::editor()
52+
->setUnquotedName('id')
53+
->setTypeName(Types::INTEGER)
54+
->create(),
55+
Column::editor()
56+
->setUnquotedName('article_id')
57+
->setTypeName(Types::INTEGER)
58+
->create(),
59+
)
60+
->setForeignKeyConstraints(
61+
ForeignKeyConstraint::editor()
62+
->setUnquotedName('articles_fk')
63+
->setUnquotedReferencingColumnNames('article_id')
64+
->setUnquotedReferencedTableName('articles')
65+
->setUnquotedReferencedColumnNames('id')
66+
->create(),
67+
ForeignKeyConstraint::editor()
68+
->setUnquotedName('articles_fk_2')
69+
->setUnquotedReferencingColumnNames('article_id')
70+
->setUnquotedReferencedTableName('articles')
71+
->setUnquotedReferencedColumnNames('id')
72+
->create(),
73+
)
74+
->create();
75+
76+
$this->dropTableIfExists('orders');
77+
$this->dropTableIfExists('articles');
78+
79+
$this->connection->createSchemaManager()
80+
->createTable($articles);
81+
$this->connection->createSchemaManager()
82+
->createTable($orders);
83+
84+
$onlineTable = $this->schemaManager->introspectTable('orders');
85+
86+
self::assertTrue(
87+
$this->schemaManager->createComparator()
88+
->compareTables($orders, $onlineTable)
89+
->isEmpty(),
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)