Skip to content

Commit 18138d8

Browse files
authored
Make PrimaryReadReplicaConnection enforcement explicit (#9239)
* Move primary replica connection logic into ORM explicitly. * Housekeeping: Use full named variables * Housekeeping: phpcs
1 parent fa2b52c commit 18138d8

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

lib/Doctrine/ORM/Id/SequenceGenerator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ORM\Id;
66

7+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
78
use Doctrine\ORM\EntityManager;
89
use Serializable;
910

@@ -54,11 +55,14 @@ public function generate(EntityManager $em, $entity)
5455
{
5556
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
5657
// Allocate new values
57-
$conn = $em->getConnection();
58-
$sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
58+
$connection = $em->getConnection();
59+
$sql = $connection->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
5960

60-
// Using `query` to force usage of the master server in MasterSlaveConnection
61-
$this->_nextValue = (int) $conn->executeQuery($sql)->fetchOne();
61+
if ($connection instanceof PrimaryReadReplicaConnection) {
62+
$connection->ensureConnectedToPrimary();
63+
}
64+
65+
$this->_nextValue = (int) $connection->executeQuery($sql)->fetchOne();
6266
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
6367
}
6468

lib/Doctrine/ORM/Id/UuidGenerator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ORM\Id;
66

7+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
78
use Doctrine\DBAL\Platforms\AbstractPlatform;
89
use Doctrine\Deprecations\Deprecation;
910
use Doctrine\ORM\EntityManager;
@@ -39,9 +40,13 @@ public function __construct()
3940
*/
4041
public function generate(EntityManager $em, $entity)
4142
{
42-
$conn = $em->getConnection();
43-
$sql = 'SELECT ' . $conn->getDatabasePlatform()->getGuidExpression();
43+
$connection = $em->getConnection();
44+
$sql = 'SELECT ' . $connection->getDatabasePlatform()->getGuidExpression();
4445

45-
return $conn->executeQuery($sql)->fetchOne();
46+
if ($connection instanceof PrimaryReadReplicaConnection) {
47+
$connection->ensureConnectedToPrimary();
48+
}
49+
50+
return $connection->executeQuery($sql)->fetchOne();
4651
}
4752
}

lib/Doctrine/ORM/Query/Exec/MultiTableDeleteExecutor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ORM\Query\Exec;
66

77
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
89
use Doctrine\DBAL\Types\Type;
910
use Doctrine\ORM\Query\AST;
1011
use Doctrine\ORM\Query\AST\DeleteStatement;
@@ -49,6 +50,10 @@ public function __construct(AST\Node $AST, $sqlWalker)
4950
$platform = $conn->getDatabasePlatform();
5051
$quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
5152

53+
if ($conn instanceof PrimaryReadReplicaConnection) {
54+
$conn->ensureConnectedToPrimary();
55+
}
56+
5257
$primaryClass = $em->getClassMetadata($AST->deleteClause->abstractSchemaName);
5358
$primaryDqlAlias = $AST->deleteClause->aliasIdentificationVariable;
5459
$rootClass = $em->getClassMetadata($primaryClass->rootEntityName);

lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ORM\Query\Exec;
66

77
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
89
use Doctrine\DBAL\Types\Type;
910
use Doctrine\ORM\Query\AST;
1011
use Doctrine\ORM\Query\AST\UpdateStatement;
@@ -55,6 +56,10 @@ public function __construct(AST\Node $AST, $sqlWalker)
5556
$platform = $conn->getDatabasePlatform();
5657
$quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
5758

59+
if ($conn instanceof PrimaryReadReplicaConnection) {
60+
$conn->ensureConnectedToPrimary();
61+
}
62+
5863
$updateClause = $AST->updateClause;
5964
$primaryClass = $sqlWalker->getEntityManager()->getClassMetadata($updateClause->abstractSchemaName);
6065
$rootClass = $em->getClassMetadata($primaryClass->rootEntityName);

lib/Doctrine/ORM/Query/Exec/SingleTableDeleteUpdateExecutor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ORM\Query\Exec;
66

77
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
89
use Doctrine\ORM\Query\AST;
910
use Doctrine\ORM\Query\SqlWalker;
1011

@@ -37,6 +38,10 @@ public function __construct(AST\Node $AST, $sqlWalker)
3738
*/
3839
public function execute(Connection $conn, array $params, array $types)
3940
{
41+
if ($conn instanceof PrimaryReadReplicaConnection) {
42+
$conn->ensureConnectedToPrimary();
43+
}
44+
4045
return $conn->executeStatement($this->_sqlStatements, $params, $types);
4146
}
4247
}

lib/Doctrine/ORM/UnitOfWork.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Common\Collections\Collection;
1010
use Doctrine\Common\EventManager;
1111
use Doctrine\Common\Proxy\Proxy;
12+
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
1213
use Doctrine\DBAL\LockMode;
1314
use Doctrine\Deprecations\Deprecation;
1415
use Doctrine\ORM\Cache\Persister\CachedPersister;
@@ -338,6 +339,12 @@ public function __construct(EntityManagerInterface $em)
338339
*/
339340
public function commit($entity = null)
340341
{
342+
$connection = $this->em->getConnection();
343+
344+
if ($connection instanceof PrimaryReadReplicaConnection) {
345+
$connection->ensureConnectedToPrimary();
346+
}
347+
341348
// Raise preFlush
342349
if ($this->evm->hasListeners(Events::preFlush)) {
343350
$this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em));

0 commit comments

Comments
 (0)