Skip to content

Commit

Permalink
Merge pull request doctrine#334 from stephen-lewis/bugfix/quote_reser…
Browse files Browse the repository at this point in the history
…ved_table_names_in_purger_delete_mode

Quote table names that also are reserved keywords
  • Loading branch information
greg0ire authored Jan 13, 2020
2 parents a933e60 + 7f309a2 commit 640ab13
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Doctrine\Common\DataFixtures\Sorter\TopologicalSorter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use function array_reverse;
use function array_search;
use function count;
use function implode;
use function is_callable;
use function method_exists;
use function preg_match;
Expand Down Expand Up @@ -246,11 +248,15 @@ private function getAssociationTables(array $classes, AbstractPlatform $platform
*/
private function getTableName($class, $platform)
{
if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) {
return $class->table['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
if (method_exists($class, 'getSchemaName')) {
$identifier[] = $class->getSchemaName();
} elseif (isset($class->table['schema'])) {
$identifier[] = $class->table['schema'];
}

return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
$identifier[] = $class->getTableName();

return (new Identifier(implode('.', $identifier)))->getQuotedName($platform);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/Purger/ORMPurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ORMPurgerTest extends BaseTest
public const TEST_ENTITY_USER = TestEntity\User::class;
public const TEST_ENTITY_USER_WITH_SCHEMA = TestEntity\UserWithSchema::class;
public const TEST_ENTITY_QUOTED = TestEntity\Quoted::class;
public const TEST_ENTITY_GROUP = TestEntity\Group::class;

public function testGetAssociationTables()
{
Expand Down Expand Up @@ -54,4 +55,18 @@ public function testTableNameWithSchema()
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('test_schema', $tableName);
}

public function testGetTableNameQuoted() : void
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_GROUP);
$platform = $em->getConnection()->getDatabasePlatform();
$purger = new ORMPurger($em);
$class = new ReflectionClass(ORMPurger::class);
$method = $class->getMethod('getTableName');
$method->setAccessible(true);
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('"', $tableName);
$this->assertStringEndsWith('"', $tableName);
}
}
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class Group
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
*
* @var int|null
*/
private $id;

/**
* @ORM\Column(length=32)
* @ORM\Id
*
* @var string|null
*/
private $code;

public function setId($id) : void
{
$this->id = $id;
}

public function getId() : ?int
{
return $this->id;
}

public function setCode($code) : void
{
$this->code = $code;
}

public function getCode() : ?string
{
return $this->code;
}
}

0 comments on commit 640ab13

Please sign in to comment.