Skip to content

Commit

Permalink
Fix support for URL to account for master-slave and pooling-shard con…
Browse files Browse the repository at this point in the history
…nections
  • Loading branch information
stof committed Jun 29, 2018
1 parent 402bbcf commit 09aadef
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/Doctrine/DBAL/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ public static function getConnection(

$params = self::parseDatabaseUrl($params);

// URL support for MasterSlaveConnection
if (isset($params['master'])) {
$params['master'] = self::parseDatabaseUrl($params['master']);
}

if (isset($params['slaves'])) {
foreach ($params['slaves'] as $key => $slaveParams) {
$params['slaves'][$key] = self::parseDatabaseUrl($slaveParams);
}
}

// URL support for PoolingShardConnection
if (isset($params['global'])) {
$params['global'] = self::parseDatabaseUrl($params['global']);
}

if (isset($params['shards'])) {
foreach ($params['shards'] as $key => $shardParams) {
$params['shards'][$key] = self::parseDatabaseUrl($shardParams);
}
}

// check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
Expand Down
71 changes: 71 additions & 0 deletions tests/Doctrine/Tests/DBAL/DriverManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver;
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Sharding\PoolingShardConnection;
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Mocks\ConnectionMock;
Expand Down Expand Up @@ -133,6 +136,74 @@ public function testValidDriverClass()
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver());
}

public function testDatabaseUrlMasterSlave()
{
$options = [
'driver' => 'pdo_mysql',
'master' => ['url' => 'mysql://foo:bar@localhost:11211/baz'],
'slaves' => [
'slave1' => ['url' => 'mysql://foo:bar@localhost:11211/baz_slave'],
],
'wrapperClass' => MasterSlaveConnection::class,
];

$conn = DriverManager::getConnection($options);

$params = $conn->getParams();
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver());

$expected = [
'user' => 'foo',
'password' => 'bar',
'host' => 'localhost',
'port' => 11211,
];

foreach ($expected as $key => $value) {
self::assertEquals($value, $params['master'][$key]);
self::assertEquals($value, $params['slaves']['slave1'][$key]);
}

self::assertEquals('baz', $params['master']['dbname']);
self::assertEquals('baz_slave', $params['slaves']['slave1']['dbname']);
}

public function testDatabaseUrlShard()
{
$options = [
'driver' => 'pdo_mysql',
'shardChoser' => MultiTenantShardChoser::class,
'global' => ['url' => 'mysql://foo:bar@localhost:11211/baz'],
'shards' => [
[
'id' => 1,
'url' => 'mysql://foo:bar@localhost:11211/baz_slave',
],
],
'wrapperClass' => PoolingShardConnection::class,
];

$conn = DriverManager::getConnection($options);

$params = $conn->getParams();
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver());

$expected = [
'user' => 'foo',
'password' => 'bar',
'host' => 'localhost',
'port' => 11211,
];

foreach ($expected as $key => $value) {
self::assertEquals($value, $params['global'][$key]);
self::assertEquals($value, $params['shards'][0][$key]);
}

self::assertEquals('baz', $params['global']['dbname']);
self::assertEquals('baz_slave', $params['shards'][0]['dbname']);
}

/**
* @dataProvider databaseUrls
*/
Expand Down

0 comments on commit 09aadef

Please sign in to comment.