Skip to content

Commit

Permalink
Rename "connection_collation" config to "collation"
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed Sep 10, 2024
1 parent a5ebe36 commit 1d7429a
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion config/global.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
; If you encounter "Illegal mix of collation" errors, setting this config to the value matching
; your existing database tables can help.
; This setting will only be used if "charset" is also set.
connection_collation =
collation =

; Database error codes to ignore during updates
;
Expand Down
2 changes: 1 addition & 1 deletion core/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function createReaderDatabaseObject($dbConfig = null)
$dbConfig['type'] = $masterDbConfig['type'];
$dbConfig['tables_prefix'] = $masterDbConfig['tables_prefix'];
$dbConfig['charset'] = $masterDbConfig['charset'];
$dbConfig['connection_collation'] = $masterDbConfig['connection_collation'] ?? null;
$dbConfig['collation'] = $masterDbConfig['collation'] ?? null;

$db = @Adapter::factory($dbConfig['adapter'], $dbConfig);

Expand Down
8 changes: 4 additions & 4 deletions core/Tracker/Db/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mysqli extends Db
protected $username;
protected $password;
protected $charset;
protected $connectionCollation;
protected $collation;
protected $activeTransaction = false;

protected $enable_ssl;
Expand Down Expand Up @@ -63,7 +63,7 @@ public function __construct($dbInfo, $driverName = 'mysql')
$this->username = $dbInfo['username'];
$this->password = $dbInfo['password'];
$this->charset = $dbInfo['charset'] ?? null;
$this->connectionCollation = $dbInfo['connection_collation'] ?? null;
$this->collation = $dbInfo['collation'] ?? null;

if (!empty($dbInfo['enable_ssl'])) {
$this->enable_ssl = $dbInfo['enable_ssl'];
Expand Down Expand Up @@ -135,9 +135,9 @@ public function connect()
throw new DbException("Connect failed: " . mysqli_connect_error());
}

if ($this->charset && $this->connectionCollation) {
if ($this->charset && $this->collation) {
// mysqli_set_charset does not support setting a collation
$query = "SET NAMES '" . $this->charset . "' COLLATE '" . $this->connectionCollation . "'";
$query = "SET NAMES '" . $this->charset . "' COLLATE '" . $this->collation . "'";

if (!mysqli_query($this->connection, $query)) {
throw new DbException("Set charset/connection collation failed: " . mysqli_error($this->connection));
Expand Down
10 changes: 5 additions & 5 deletions core/Tracker/Db/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Mysql extends Db
/**
* @var string|null
*/
private $connectionCollation;
private $collation;

protected $mysqlOptions = [];

Expand Down Expand Up @@ -79,8 +79,8 @@ public function __construct($dbInfo, $driverName = 'mysql')
$this->charset = $dbInfo['charset'];
$this->dsn .= ';charset=' . $this->charset;

if (!empty($dbInfo['connection_collation'])) {
$this->connectionCollation = $dbInfo['connection_collation'];
if (!empty($dbInfo['collation'])) {
$this->collation = $dbInfo['collation'];
}
}

Expand Down Expand Up @@ -433,8 +433,8 @@ private function establishConnection(): void
if (!empty($this->charset)) {
$sql = "SET NAMES '" . $this->charset . "'";

if (!empty($this->connectionCollation)) {
$sql .= " COLLATE '" . $this->connectionCollation . "'";
if (!empty($this->collation)) {
$sql .= " COLLATE '" . $this->collation . "'";
}

$this->connection->exec($sql);
Expand Down
4 changes: 2 additions & 2 deletions libs/Zend/Db/Adapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ protected function _connect()
throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
}

if (!empty($this->_config['charset']) && !empty($this->_config['connection_collation'])) {
if (!empty($this->_config['charset']) && !empty($this->_config['collation'])) {
// mysqli_set_charset does not support setting a collation
$query = "SET NAMES '" . $this->_config['charset'] . "' COLLATE '" . $this->_config['connection_collation'] . "'";
$query = "SET NAMES '" . $this->_config['charset'] . "' COLLATE '" . $this->_config['collation'] . "'";

mysqli_query($this->_connection, $query);
} elseif (!empty($this->_config['charset'])) {
Expand Down
4 changes: 2 additions & 2 deletions libs/Zend/Db/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ protected function _connect()
if (!empty($this->_config['charset'])) {
$initCommand = "SET NAMES '" . $this->_config['charset'] . "'";

if (!empty($this->_config['connection_collation'])) {
$initCommand .= " COLLATE '" . $this->_config['connection_collation'] . "'";
if (!empty($this->_config['collation'])) {
$initCommand .= " COLLATE '" . $this->_config['collation'] . "'";
}

$this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPUnit/Integration/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function testConnectionCollationDefault(string $adapter, string $expected

$config = Config::getInstance();
$config->database['adapter'] = $adapter;
$config->database['connection_collation'] = null;
$config->database['collation'] = null;

$db = Db::get();
self::assertInstanceOf($expectedClass, $db);
Expand All @@ -285,13 +285,13 @@ public function testConnectionCollationSetInConfig(string $adapter, string $expe

$config = Config::getInstance();
$config->database['adapter'] = $adapter;
$config->database['connection_collation'] = $config->database['charset'] . '_swedish_ci';
$config->database['collation'] = $config->database['charset'] . '_swedish_ci';

$db = Db::get();
self::assertInstanceOf($expectedClass, $db);

$currentCollation = $db->fetchOne('SELECT @@collation_connection');
self::assertSame($config->database['connection_collation'], $currentCollation);
self::assertSame($config->database['collation'], $currentCollation);
}

public function getDbAdapter()
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/Integration/Tracker/Db/MysqliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testConnectionThrowsOnInvalidConnectionCollation(): void
self::expectExceptionMessageMatches('/Set charset\/connection collation failed/');

$config = Config::getInstance();
$config->database['connection_collation'] = 'something really invalid';
$config->database['collation'] = 'something really invalid';

Tracker\Db::connectPiwikTrackerDb();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPUnit/Integration/Tracker/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function testFetchAllNoMatch()
public function testConnectionCollationDefault(): void
{
$config = Config::getInstance();
$config->database['connection_collation'] = null;
$config->database['collation'] = null;
$db = Tracker\Db::connectPiwikTrackerDb();

// exact value depends on database used
Expand All @@ -206,11 +206,11 @@ public function testConnectionCollationDefault(): void
public function testConnectionCollationSetInConfig(): void
{
$config = Config::getInstance();
$config->database['connection_collation'] = $config->database['charset'] . '_swedish_ci';
$config->database['collation'] = $config->database['charset'] . '_swedish_ci';
$db = Tracker\Db::connectPiwikTrackerDb();

$currentCollation = $db->fetchOne('SELECT @@collation_connection');
self::assertSame($config->database['connection_collation'], $currentCollation);
self::assertSame($config->database['collation'], $currentCollation);
}

private function insertRowId($value = '1')
Expand Down

0 comments on commit 1d7429a

Please sign in to comment.