Unverified Commit 8c1e5136 authored by Luís Cobucci's avatar Luís Cobucci Committed by GitHub

Merge pull request #2588 from Deltachaos/patch-1

Inherit charset from master connection if not set explicitly
parents 63115676 c614747c
......@@ -224,7 +224,13 @@ class MasterSlaveConnection extends Connection
return $params['master'];
}
return $params['slaves'][array_rand($params['slaves'])];
$config = $params['slaves'][array_rand($params['slaves'])];
if ( ! isset($config['charset']) && isset($params['master']['charset'])) {
$config['charset'] = $params['master']['charset'];
}
return $config;
}
/**
......@@ -364,7 +370,7 @@ class MasterSlaveConnection extends Connection
if ($logger) {
$logger->startQuery($args[0]);
}
$statement = $this->_conn->query(...$args);
if ($logger) {
......
......@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\Tests\DbalFunctionalTestCase;
......@@ -38,15 +39,52 @@ class MasterSlaveConnectionTest extends DbalFunctionalTestCase
$this->_conn->insert('master_slave_table', array('test_int' => 1));
}
public function createMasterSlaveConnection($keepSlave = false)
private function createMasterSlaveConnection(bool $keepSlave = false) : MasterSlaveConnection
{
return DriverManager::getConnection($this->createMasterSlaveConnectionParams($keepSlave));
}
private function createMasterSlaveConnectionParams(bool $keepSlave = false) : array
{
$params = $this->_conn->getParams();
$params['master'] = $params;
$params['slaves'] = array($params, $params);
$params['keepSlave'] = $keepSlave;
$params['wrapperClass'] = 'Doctrine\DBAL\Connections\MasterSlaveConnection';
$params['wrapperClass'] = MasterSlaveConnection::class;
return $params;
}
public function testInheritCharsetFromMaster() : void
{
$charsets = [
'utf8',
'latin1'
];
return DriverManager::getConnection($params);
foreach ($charsets as $charset) {
$params = $this->createMasterSlaveConnectionParams();
$params['master']['charset'] = $charset;
foreach ($params['slaves'] as $index => $slaveParams) {
if (isset($slaveParams['charset'])) {
unset($params['slaves'][$index]['charset']);
}
}
/** @var MasterSlaveConnection $conn */
$conn = DriverManager::getConnection($params);
$conn->connect('slave');
self::assertFalse($conn->isConnectedToMaster());
$clientCharset = $conn->fetchColumn('select @@character_set_client as c');
self::assertSame(
$charset,
substr(strtolower($clientCharset), 0, strlen($charset))
);
}
}
public function testMasterOnConnect()
......
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