Skip to content

Commit

Permalink
#7841 SchemaTool generates extra diff for platforms without FK support
Browse files Browse the repository at this point in the history
  • Loading branch information
vpArth committed Oct 1, 2019
1 parent d528f70 commit 3e806f6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 25 deletions.
52 changes: 27 additions & 25 deletions lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,33 +737,35 @@ private function gatherRelationJoinColumns(

$compositeName = $theJoinTable->getName() . '.' . implode('', $localColumns);

if (isset($addedFks[$compositeName])
&& ($foreignTableName !== $addedFks[$compositeName]['foreignTableName']
|| 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))
) {
foreach ($theJoinTable->getForeignKeys() as $fkName => $key) {
if (count(array_diff($key->getLocalColumns(), $localColumns)) === 0
&& (($key->getForeignTableName() !== $foreignTableName)
|| 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))
) {
$theJoinTable->removeForeignKey($fkName);
break;
if ($this->platform->supportsForeignKeyConstraints()) {
if (isset($addedFks[$compositeName])
&& ($foreignTableName !== $addedFks[$compositeName]['foreignTableName']
|| 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))
) {
foreach ($theJoinTable->getForeignKeys() as $fkName => $key) {
if (count(array_diff($key->getLocalColumns(), $localColumns)) === 0
&& (($key->getForeignTableName() !== $foreignTableName)
|| 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))
) {
$theJoinTable->removeForeignKey($fkName);
break;
}
}
}

$blacklistedFks[$compositeName] = true;
} elseif (! isset($blacklistedFks[$compositeName])) {
$addedFks[$compositeName] = [
'foreignTableName' => $foreignTableName,
'foreignColumns' => $foreignColumns,
];

$theJoinTable->addForeignKeyConstraint(
$foreignTableName,
$localColumns,
$foreignColumns,
$fkOptions
);
$blacklistedFks[$compositeName] = true;
} elseif (! isset($blacklistedFks[$compositeName])) {
$addedFks[$compositeName] = [
'foreignTableName' => $foreignTableName,
'foreignColumns' => $foreignColumns,
];

$theJoinTable->addForeignKeyConstraint(
$foreignTableName,
$localColumns,
$foreignColumns,
$fkOptions
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Annotation as ORM;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmTestCase;
use Doctrine\Tests\TestUtil;

class SqliteSchemaToolTest extends OrmTestCase
{
/** @var EntityManagerInterface */
private $em;
/** @var SchemaTool */
private $schemaTool;

protected function setUp()
{
parent::setUp();
$this->em = $this->getTestEntityManager(TestUtil::getConnection());

$this->schemaTool = new SchemaTool($this->em);
}

public function testForeignKeysNotCompare() : void
{
if ($this->em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Test for platforms without foreign keys support');
}
$class = $this->em->getClassMetadata(SqliteSchemaToolChild::class);
$this->schemaTool->updateSchema([$class], true);
$diff = $this->schemaTool->getUpdateSchemaSql([$class]);

self::assertEmpty($diff);

$this->schemaTool->dropSchema([$class]);
}
}

/**
* @ORM\Entity
*/
class SqliteSchemaToolParent
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
public $id;

/** @ORM\OneToMany(targetEntity=SqliteSchemaToolChild::class, mappedBy="parent") */
public $children;

public function __construct()
{
$this->children = new ArrayCollection();
}
}

/**
* @ORM\Entity
*/
class SqliteSchemaToolChild
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
public $id;

/** @ORM\ManyToOne(targetEntity=SqliteSchemaToolParent::class) */
public $parent;
}
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2138Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class DDC2138Test extends OrmFunctionalTestCase
*/
public function testForeignKeyOnSTIWithMultipleMapping() : void
{
if (! $this->em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Platform does not support foreign keys.');
}
$schema = $this->schemaTool->getSchemaFromMetadata(
[
$this->em->getClassMetadata(DDC2138User::class),
Expand Down

0 comments on commit 3e806f6

Please sign in to comment.