|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Functional\Ticket; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @see https://github.com/doctrine/orm/issues/7555 |
|
13
|
|
|
*/ |
|
14
|
|
|
final class GH7555Test extends OrmFunctionalTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
private static $tableCreated = false; |
|
17
|
|
|
|
|
18
|
|
|
/** @var EntityManagerInterface */ |
|
19
|
|
|
private $entityManager; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp() : void |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') { |
|
26
|
|
|
$this->markTestSkipped('Only databases supporting deferrable constraints are eligible for this test.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->entityManager = $this->_getEntityManager(clone self::$_sharedConn); |
|
30
|
|
|
|
|
31
|
|
|
if (self::$tableCreated) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->setUpEntitySchema([GH7555Entity::class]); |
|
36
|
|
|
$connection = $this->entityManager->getConnection(); |
|
37
|
|
|
$connection->exec('DROP INDEX "unique_field_constraint"'); |
|
38
|
|
|
$connection->exec( |
|
39
|
|
|
'ALTER TABLE "gh7555entity" ADD CONSTRAINT "unique_field_constraint" UNIQUE ("uniquefield") DEFERRABLE' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$this->entityManager->persist(new GH7555Entity()); |
|
43
|
|
|
$this->entityManager->flush(); |
|
44
|
|
|
$this->entityManager->clear(); |
|
45
|
|
|
|
|
46
|
|
|
self::$tableCreated = true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @group GH7555 |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testTransactionalWithDeferredConstraint() : void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(DBALException::class); |
|
55
|
|
|
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"'); |
|
56
|
|
|
|
|
57
|
|
|
$this->entityManager->transactional(static function (EntityManagerInterface $entityManager) : void { |
|
58
|
|
|
$entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED'); |
|
59
|
|
|
$entityManager->persist(new GH7555Entity()); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @group GH7555 |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testTransactionalWithDeferredConstraintAndTransactionNesting() : void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->expectException(DBALException::class); |
|
69
|
|
|
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"'); |
|
70
|
|
|
|
|
71
|
|
|
$this->entityManager->getConnection()->setNestTransactionsWithSavepoints(true); |
|
72
|
|
|
|
|
73
|
|
|
$this->entityManager->transactional(static function (EntityManagerInterface $entityManager) : void { |
|
74
|
|
|
$entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED'); |
|
75
|
|
|
$entityManager->persist(new GH7555Entity()); |
|
76
|
|
|
$entityManager->flush(); |
|
77
|
|
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @group GH7555 |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testFlushWithDeferredConstraint() : void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->expectException(DBALException::class); |
|
86
|
|
|
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"'); |
|
87
|
|
|
|
|
88
|
|
|
$this->entityManager->beginTransaction(); |
|
89
|
|
|
$this->entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED'); |
|
90
|
|
|
$this->entityManager->persist(new GH7555Entity()); |
|
91
|
|
|
$this->entityManager->flush(); |
|
92
|
|
|
$this->entityManager->commit(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @group GH7555 |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testFlushWithDeferredConstraintAndTransactionNesting() : void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->expectException(DBALException::class); |
|
101
|
|
|
$this->expectExceptionMessage('violates unique constraint "unique_field_constraint"'); |
|
102
|
|
|
|
|
103
|
|
|
$this->entityManager->getConnection()->setNestTransactionsWithSavepoints(true); |
|
104
|
|
|
|
|
105
|
|
|
$this->entityManager->beginTransaction(); |
|
106
|
|
|
$this->entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED'); |
|
107
|
|
|
$this->entityManager->persist(new GH7555Entity()); |
|
108
|
|
|
$this->entityManager->flush(); |
|
109
|
|
|
$this->entityManager->commit(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @Entity |
|
115
|
|
|
* @Table( |
|
116
|
|
|
* uniqueConstraints={ |
|
117
|
|
|
* @UniqueConstraint(columns={"uniqueField"}, name="unique_field_constraint") |
|
118
|
|
|
* } |
|
119
|
|
|
* ) |
|
120
|
|
|
*/ |
|
121
|
|
|
class GH7555Entity |
|
122
|
|
|
{ |
|
123
|
|
|
/** |
|
124
|
|
|
* @Id |
|
125
|
|
|
* @GeneratedValue |
|
126
|
|
|
* @Column(type="integer") |
|
127
|
|
|
* |
|
128
|
|
|
* @var int |
|
129
|
|
|
*/ |
|
130
|
|
|
public $id; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @Column(type="boolean") |
|
134
|
|
|
* |
|
135
|
|
|
* @var bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public $uniqueField = true; |
|
138
|
|
|
} |
|
139
|
|
|
|