Skip to content

Commit

Permalink
Merge pull request #35 from mathroc/fix/34
Browse files Browse the repository at this point in the history
fix {Retry,MasterSlaves}Driver::get{Database,SchemaManager}
  • Loading branch information
mathroc committed Dec 15, 2015
2 parents 46d273f + 34c3836 commit 229a5a7
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 134 deletions.
16 changes: 13 additions & 3 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function aMasterSlavesConnectionWithSlaves($connectionName, $slaveCount)
$params = [
'master' => $master,
'slaves' => $slaves,
'driverClass' => 'Ez\DbLinker\Driver\MysqlMasterSlavesDriver',
'driverClass' => $this->masterSlaveDriverClass(),
];
$this->connections[$connectionName] = [
'params' => $params,
Expand Down Expand Up @@ -268,7 +268,7 @@ public function aRetryMasterSlavesConnectionWithSlavesLimitedToRetryWithDbAndUse
'connectionParams' => [
'master' => $master,
'slaves' => $slaves,
'driverClass' => 'Ez\DbLinker\Driver\MysqlMasterSlavesDriver',
'driverClass' => $this->masterSlaveDriverClass(),
],
'retryStrategy' => $this->retryStrategy($n),
];
Expand All @@ -280,6 +280,16 @@ public function aRetryMasterSlavesConnectionWithSlavesLimitedToRetryWithDbAndUse
];
}

/**
* @Then I can get the database name on :connectionName
*/
public function iCanGetTheDatabaseNameOn($connectionName)
{
$response = $this->getConnection($connectionName)->getDatabase();
$expectedResponse = $this->defaultDatabaseName();
assert($response === $expectedResponse, "'$response' matches '$expectedResponse'");
}

/**
* @Given a retry connection :connectionName limited to :n retry with db :db
* @Given a retry connection :connectionName limited to :n retry with db :db and username :username
Expand Down Expand Up @@ -387,7 +397,7 @@ public function shouldHaveSlave($connectionName, $n)
if ($connection instanceof \Ez\DbLinker\Driver\Connection\RetryConnection) {
$connection = $connection->wrappedConnection();
}
$slaveCount = $connection->slaves()->count();
$slaveCount = count($connection->slaves());
assert($slaveCount === (int)$n, "Slaves count is $slaveCount, $n expected.");
}

Expand Down
7 changes: 6 additions & 1 deletion features/bootstrap/MySQLContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private function masterParams($username = null, $password = '') {
'host' => getenv('DBLINKER_MYSQL_1_PORT_3306_TCP_ADDR'),
'user' => getenv('DBLINKER_MYSQL_1_ENV_MYSQL_USER'),
'password' => getenv('DBLINKER_MYSQL_1_ENV_MYSQL_PASSWORD'),
'dbname' => getenv('DBLINKER_MYSQL_1_ENV_MYSQL_DATABASE'),
'dbname' => $this->defaultDatabaseName(),
];
if ($username !== null) {
$params['user'] = $username;
Expand All @@ -26,6 +26,11 @@ private function masterParams($username = null, $password = '') {
return $this->params($params);
}

private function defaultDatabaseName()
{
return getenv('DBLINKER_MYSQL_1_ENV_MYSQL_DATABASE');
}

private function activeConnectionsCount()
{
$connection = $this->rootConnection();
Expand Down
7 changes: 6 additions & 1 deletion features/bootstrap/PostgreSQLContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private function masterParams($username = null, $password = '') {
'host' => getenv('DBLINKER_POSTGRESQL_1_PORT_5432_TCP_ADDR'),
'user' => getenv('DBLINKER_POSTGRESQL_1_ENV_POSTGRES_USER'),
'password' => getenv('DBLINKER_POSTGRESQL_1_ENV_POSTGRES_PASSWORD'),
'dbname' => getenv('DBLINKER_POSTGRESQL_1_ENV_POSTGRES_DATABASE'),
'dbname' => $this->defaultDatabaseName(),
];
if ($username !== null && $username !== 'root') {
if ($username === 'root') {
Expand All @@ -24,6 +24,11 @@ private function masterParams($username = null, $password = '') {
return $this->params($params);
}

private function defaultDatabaseName()
{
return getenv('DBLINKER_POSTGRESQL_1_ENV_POSTGRES_DATABASE');
}

private function activeConnectionsCount()
{
return 0;
Expand Down
3 changes: 3 additions & 0 deletions features/master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ Feature: Master / Slaves
When I query "SELECT 1" on "conn"
Then the last query succeeded on "conn"
And "conn" is on master

Scenario: Get database
Then I can get the database name on "conn"
4 changes: 4 additions & 0 deletions features/retry-master-slave.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@retry @master-slaves
Feature: Retry Master/Slaves

Scenario: Get database
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
Then I can get the database name on "conn"

Scenario: ACCESS_DENIED_ERROR restart on another slave
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry with username "nobody"
When I query "SELECT 1" on "conn"
Expand Down
3 changes: 3 additions & 0 deletions features/retry.feature
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Feature: Retry
And the last error should be "CON_COUNT" on "conn2"
And "conn2" retry limit should be 0

Scenario: Get database
Then I can get the database name on "conn"

Scenario: No such table
Given table "not_here_yet" can be created automatically on "conn"
When I prepare a statement "SELECT * FROM not_here_yet" on "conn"
Expand Down
16 changes: 16 additions & 0 deletions src/Driver/Connection/ConnectionWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Ez\DbLinker\Driver\Connection;

interface ConnectionWrapper
{
/**
* @return \Doctrine\DBAL\Driver\Connection
*/
public function wrappedConnection();

/**
* @return \Doctrine\DBAL\Driver
*/
public function wrappedDriver();
}
36 changes: 36 additions & 0 deletions src/Driver/Connection/ConnectionWrapperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Ez\DbLinker\Driver\Connection;

use Closure;
use Exception;
use SplObjectStorage;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Driver\Connection;

trait ConnectionWrapperTrait
{
private $wrappedConnection;
private $wrappedDriver;

/**
* @inherit
*/
public function wrappedConnection()
{
if ($this->wrappedConnection === null) {
$this->wrap();
}
return $this->wrappedConnection;
}

public function wrappedDriver()
{
if ($this->wrappedDriver === null) {
$this->wrap();
}
return $this->wrappedDriver;
}

abstract protected function wrap();
}
Loading

0 comments on commit 229a5a7

Please sign in to comment.