Unverified Commit cf98cc9e authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3201 from stof/parse_url

Fix support for URL to account for master-slave and pooling-shard connections
parents 7bbed590 09aadef8
...@@ -155,6 +155,28 @@ final class DriverManager ...@@ -155,6 +155,28 @@ final class DriverManager
$params = self::parseDatabaseUrl($params); $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 // check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) { if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance(); throw DBALException::invalidPdoInstance();
......
...@@ -2,12 +2,15 @@ ...@@ -2,12 +2,15 @@
namespace Doctrine\Tests\DBAL; namespace Doctrine\Tests\DBAL;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver; use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver;
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver; use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Sharding\PoolingShardConnection;
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
use Doctrine\Tests\DBAL\Mocks\MockPlatform; use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use Doctrine\Tests\DbalTestCase; use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Mocks\ConnectionMock; use Doctrine\Tests\Mocks\ConnectionMock;
...@@ -133,6 +136,74 @@ class DriverManagerTest extends DbalTestCase ...@@ -133,6 +136,74 @@ class DriverManagerTest extends DbalTestCase
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver()); 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 * @dataProvider databaseUrls
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment