Skip to content

Commit

Permalink
[doctrineGH-4052] Add ensureConnectedToPrimary/Replica instead of con…
Browse files Browse the repository at this point in the history
…nect() with magic constant.
  • Loading branch information
beberlei committed Jun 7, 2020
1 parent 784a998 commit ea60246
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Before:
),
'keepSlave' => true,
));
$connection->connect('slave');
$connection->connect('master');
$connection->isConnectedToMaster();

After:

Expand All @@ -30,6 +33,9 @@ After:
)
'keepReplica' => true,
));
$connection->ensureConnectedToReplica();
$connection->ensureConnectedToPrimary();
$connection->isConnectedToPrimary();

## Deprecated `ArrayStatement` and `ResultCacheStatement` classes.

Expand Down
28 changes: 27 additions & 1 deletion lib/Doctrine/DBAL/Connections/PrimaryReplicaConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());
}

Expand Down Expand Up @@ -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]);
Expand All @@ -155,7 +155,7 @@ public function testKeepReplicaBeginTransactionStaysOnPrimary(): void
$conn->connect();
self::assertTrue($conn->isConnectedToPrimary());

$conn->connect('replica');
$conn->ensureConnectedToReplica();
self::assertFalse($conn->isConnectedToPrimary());
}

Expand All @@ -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]);

Expand All @@ -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());
}

Expand Down Expand Up @@ -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';

Expand Down

0 comments on commit ea60246

Please sign in to comment.