Skip to content

Commit f1423b2

Browse files
greg0ireostrolucky
authored andcommitted
Remap collate to collation when appropriate
collate was never supposed to be supported and used in the first place, and DBAL 3.3.2 fixed that. Here, we avoid the situation where a user willing to use the correct option ends up with both collate and collation defined, and we remap collate to collation when DBAL 3.3 is detected. DBAL 3.3.0 and 3.3.1 are avoided thanks to a composer version constraint.
1 parent 0b47e6e commit f1423b2

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

ConnectionFactory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\DBAL\Types\Type;
1414

1515
use function array_merge;
16+
use function defined;
1617
use function is_subclass_of;
1718
use function trigger_deprecation;
1819

@@ -79,8 +80,19 @@ public function createConnection(array $params, ?Configuration $config = null, ?
7980
if ($driver instanceof AbstractMySQLDriver) {
8081
$params['charset'] = 'utf8mb4';
8182

82-
if (! isset($params['defaultTableOptions']['collate'])) {
83-
$params['defaultTableOptions']['collate'] = 'utf8mb4_unicode_ci';
83+
/* PARAM_ASCII_STR_ARRAY is defined since doctrine/dbal 3.3
84+
doctrine/dbal 3.3.2 adds support for the option "collation"
85+
Checking for that constant will no longer be necessary
86+
after dropping support for doctrine/dbal 2, since this
87+
package requires doctrine/dbal 3.3.2 or higher. */
88+
if (isset($params['defaultTableOptions']['collate']) && defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
89+
$params['defaultTableOptions']['collation'] = $params['defaultTableOptions']['collate'];
90+
unset($params['defaultTableOptions']['collate']);
91+
}
92+
93+
$collationOption = defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY') ? 'collation' : 'collate';
94+
if (! isset($params['defaultTableOptions'][$collationOption])) {
95+
$params['defaultTableOptions'][$collationOption] = 'utf8mb4_unicode_ci';
8496
}
8597
} else {
8698
$params['charset'] = 'utf8';

Tests/ConnectionFactoryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use function array_intersect_key;
2121
use function class_exists;
22+
use function defined;
2223
use function strpos;
2324

2425
// Compatibility with DBAL < 3
@@ -81,6 +82,56 @@ public function testDefaultCharsetMySql(): void
8182
$this->assertSame('utf8mb4', $connection->getParams()['charset']);
8283
}
8384

85+
public function testDefaultCollateMySql(): void
86+
{
87+
if (defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
88+
self::markTestSkipped('This test is only relevant for DBAL < 3.3');
89+
}
90+
91+
$factory = new ConnectionFactory([]);
92+
$connection = $factory->createConnection(['driver' => 'pdo_mysql']);
93+
94+
$this->assertSame(
95+
'utf8mb4_unicode_ci',
96+
$connection->getParams()['defaultTableOptions']['collate']
97+
);
98+
}
99+
100+
public function testDefaultCollationMySql(): void
101+
{
102+
if (! defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
103+
self::markTestSkipped('This test is only relevant for DBAL >= 3.3');
104+
}
105+
106+
$factory = new ConnectionFactory([]);
107+
$connection = $factory->createConnection(['driver' => 'pdo_mysql']);
108+
109+
$this->assertSame(
110+
'utf8mb4_unicode_ci',
111+
$connection->getParams()['defaultTableOptions']['collation']
112+
);
113+
}
114+
115+
public function testCollateMapsToCollationForMySql(): void
116+
{
117+
if (! defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
118+
self::markTestSkipped('This test is only relevant for DBAL >= 3.3');
119+
}
120+
121+
$factory = new ConnectionFactory([]);
122+
$connection = $factory->createConnection([
123+
'driver' => 'pdo_mysql',
124+
'defaultTableOptions' => ['collate' => 'my_collation'],
125+
]);
126+
127+
$tableOptions = $connection->getParams()['defaultTableOptions'];
128+
$this->assertArrayNotHasKey('collate', $tableOptions);
129+
$this->assertSame(
130+
'my_collation',
131+
$tableOptions['collation']
132+
);
133+
}
134+
84135
/** @group legacy */
85136
public function testConnectionOverrideOptions(): void
86137
{

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"php": "^7.1 || ^8.0",
2929
"doctrine/annotations": "^1",
3030
"doctrine/cache": "^1.11 || ^2.0",
31-
"doctrine/dbal": "^2.13.1|^3.1",
31+
"doctrine/dbal": "^2.13.1|^3.3.2",
3232
"doctrine/persistence": "^2.2",
3333
"doctrine/sql-formatter": "^1.0.1",
3434
"symfony/cache": "^4.3.3|^5.0|^6.0",

psalm.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@
3939
<file name="Repository/ServiceEntityRepository.php"/>
4040
</errorLevel>
4141
</TooManyTemplateParams>
42+
<InvalidArrayOffset>
43+
<errorLevel type="suppress">
44+
<!-- requires a release of https://github.com/doctrine/dbal/pull/5261 -->
45+
<file name="Tests/ConnectionFactoryTest.php"/>
46+
</errorLevel>
47+
</InvalidArrayOffset>
4248
</issueHandlers>
4349
</psalm>

0 commit comments

Comments
 (0)