Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect handling of transactions using deferred constraints #7556

Open
wants to merge 1 commit into
base: 2.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,19 @@ public function transactional($func)
$return = call_user_func($func, $this);

$this->flush();
} catch (Throwable $e) {
$this->close();
$this->conn->rollBack();

throw $e;
}

try {
$this->conn->commit();

return $return ?: true;
} catch (Throwable $e) {
$this->close();
$this->conn->rollBack();

throw $e;
}
Expand Down
133 changes: 133 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7555Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Functional\Ticket;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Tests\OrmFunctionalTestCase;
use PDOException;

/**
* @see https://github.com/doctrine/orm/issues/7555
*
* @group GH7555
*/
final class GH7555Test extends OrmFunctionalTestCase
{
private static $tableCreated = false;

protected function setUp() : void
{
parent::setUp();

if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
$this->markTestSkipped('Only databases supporting deferrable constraints are eligible for this test.');
}

if (self::$tableCreated) {
return;
}

$this->setUpEntitySchema([GH7555Entity::class]);
$connection = $this->_em->getConnection();
$connection->exec('DROP INDEX "unique_field_constraint"');
$connection->exec('ALTER TABLE "gh7555entity" ADD CONSTRAINT "unique_field_constraint" UNIQUE ("uniquefield") DEFERRABLE');

$this->_em->persist(new GH7555Entity());
$this->_em->flush();
$this->_em->clear();

self::$tableCreated = true;
}

/**
* @group GH7555
*/
public function testTransactionalWithDeferredConstraint() : void
{
$this->expectException(PDOException::class);
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');

$this->_em->transactional(static function (EntityManagerInterface $entityManager) : void {
$entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
$entityManager->persist(new GH7555Entity());
});
}

/**
* @group GH7555
*/
public function testTransactionalWithDeferredConstraintAndTransactionNesting() : void
{
$this->expectException(PDOException::class);
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');

$this->_em->getConnection()->setNestTransactionsWithSavepoints(true);

$this->_em->transactional(static function (EntityManagerInterface $entityManager) : void {
$entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
$entityManager->persist(new GH7555Entity());
$entityManager->flush();
});
}

/**
* @group GH7555
*/
public function testFlushWithDeferredConstraint() : void
{
$this->expectException(PDOException::class);
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');

$this->_em->beginTransaction();
$this->_em->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
$this->_em->persist(new GH7555Entity());
$this->_em->flush();
$this->_em->commit();
}

/**
* @group GH7555
*/
public function testFlushWithDeferredConstraintAndTransactionNesting() : void
{
$this->expectException(PDOException::class);
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');

$this->_em->getConnection()->setNestTransactionsWithSavepoints(true);

$this->_em->beginTransaction();
$this->_em->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
$this->_em->persist(new GH7555Entity());
$this->_em->flush();
$this->_em->commit();
}
}

/**
* @Entity
* @Table(
* uniqueConstraints={
* @UniqueConstraint(columns={"uniqueField"}, name="unique_field_constraint")
* }
* )
*/
class GH7555Entity
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*
* @var int
*/
public $id;

/**
* @Column(type="boolean")
*
* @var bool
*/
public $uniqueField = true;
}