diff --git a/UPGRADE.md b/UPGRADE.md index e07c5279c56..b0d559e0a3f 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -17,6 +17,9 @@ Before: ), 'keepSlave' => true, )); + $connection->connect('slave'); + $connection->connect('master'); + $connection->isConnectedToMaster(); After: @@ -30,6 +33,9 @@ After: ) 'keepReplica' => true, )); + $connection->ensureConnectedToReplica(); + $connection->ensureConnectedToPrimary(); + $connection->isConnectedToPrimary(); ## Deprecated `ArrayStatement` and `ResultCacheStatement` classes. diff --git a/lib/Doctrine/DBAL/Connections/PrimaryReplicaConnection.php b/lib/Doctrine/DBAL/Connections/PrimaryReplicaConnection.php index 7b702095ccc..35ae867f4e4 100644 --- a/lib/Doctrine/DBAL/Connections/PrimaryReplicaConnection.php +++ b/lib/Doctrine/DBAL/Connections/PrimaryReplicaConnection.php @@ -126,11 +126,15 @@ public function isConnectedToPrimary() * * @return bool */ - public function connect($connectionName = null) + public function connect($connectionName = null, bool $triggerDeprecationWhenConnectionNameUsed = true) { $requestedConnectionChange = ($connectionName !== null); $connectionName = $connectionName ?: 'replica'; + if ($requestedConnectionChange && $triggerDeprecationWhenConnectionNameUsed) { + @trigger_error("PrimaryReplicaConnection::connect() with connection name argument is deprecated, use ensureConnectedToPrimary/Replica() instead", E_USER_DEPRECATED); + } + if ($connectionName !== 'replica' && $connectionName !== 'primary') { throw new InvalidArgumentException('Invalid option to connect(), only primary or replica allowed.'); } @@ -178,6 +182,28 @@ public function connect($connectionName = null) return true; } + /** + * Connects to the primary node of the database cluster. + * + * All following statements after this will be executed against the primary node. + */ + public function ensureConnectedToPrimary(): bool + { + return $this->connect('primary', false); + } + + /** + * Connects to a replica node of the database cluster. + * + * All following statements after this will be executed against the replica node, + * unless the keepReplica option is set to false and a primary connection + * was already opened. + */ + public function ensureConnectedToReplica(): bool + { + return $this->connect('replica', false); + } + /** * Connects to a specific connection. * diff --git a/tests/Doctrine/Tests/DBAL/Functional/PrimaryReplicaConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/PrimaryReplicaConnectionTest.php index 4ddabeb8336..e5175dcf61b 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/PrimaryReplicaConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/PrimaryReplicaConnectionTest.php @@ -87,7 +87,7 @@ public function testInheritCharsetFromPrimary(): void $conn = DriverManager::getConnection($params); self::assertInstanceOf(PrimaryReplicaConnection::class, $conn); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); self::assertFalse($conn->isConnectedToPrimary()); @@ -105,9 +105,9 @@ public function testPrimaryOnConnect(): void $conn = $this->createPrimaryReplicaConnection(); self::assertFalse($conn->isConnectedToPrimary()); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); self::assertFalse($conn->isConnectedToPrimary()); - $conn->connect('primary'); + $conn->ensureConnectedToPrimary(); self::assertTrue($conn->isConnectedToPrimary()); } @@ -144,7 +144,7 @@ public function testPrimaryOnWriteOperation(): void public function testKeepReplicaBeginTransactionStaysOnPrimary(): void { $conn = $this->createPrimaryReplicaConnection($keepReplica = true); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); $conn->beginTransaction(); $conn->insert('primary_replica_table', ['test_int' => 30]); @@ -155,7 +155,7 @@ public function testKeepReplicaBeginTransactionStaysOnPrimary(): void $conn->connect(); self::assertTrue($conn->isConnectedToPrimary()); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); self::assertFalse($conn->isConnectedToPrimary()); } @@ -165,7 +165,7 @@ public function testKeepReplicaBeginTransactionStaysOnPrimary(): void public function testKeepReplicaInsertStaysOnPrimary(): void { $conn = $this->createPrimaryReplicaConnection($keepReplica = true); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); $conn->insert('primary_replica_table', ['test_int' => 30]); @@ -174,20 +174,20 @@ public function testKeepReplicaInsertStaysOnPrimary(): void $conn->connect(); self::assertTrue($conn->isConnectedToPrimary()); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); self::assertFalse($conn->isConnectedToPrimary()); } public function testPrimaryReplicaConnectionCloseAndReconnect(): void { $conn = $this->createPrimaryReplicaConnection(); - $conn->connect('primary'); + $conn->ensureConnectedToPrimary(); self::assertTrue($conn->isConnectedToPrimary()); $conn->close(); self::assertFalse($conn->isConnectedToPrimary()); - $conn->connect('primary'); + $conn->ensureConnectedToPrimary(); self::assertTrue($conn->isConnectedToPrimary()); } @@ -218,7 +218,7 @@ public function testQueryOnPrimary(): void public function testQueryOnReplica(): void { $conn = $this->createPrimaryReplicaConnection(); - $conn->connect('replica'); + $conn->ensureConnectedToReplica(); $query = 'SELECT count(*) as num FROM primary_replica_table';