Skip to content

Commit 68576ab

Browse files
committed
Clean up MySQL version detection logic
See: https://mariadb.com/kb/en/version/
1 parent 45a2bb4 commit 68576ab

File tree

3 files changed

+9
-70
lines changed

3 files changed

+9
-70
lines changed

src/Driver/AbstractMySQLDriver.php

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use Doctrine\Deprecations\Deprecation;
1818

1919
use function assert;
20-
use function preg_match;
2120
use function stripos;
21+
use function substr;
2222
use function version_compare;
2323

2424
/**
@@ -33,18 +33,17 @@ abstract class AbstractMySQLDriver implements VersionAwarePlatformDriver
3333
*/
3434
public function createDatabasePlatformForVersion($version)
3535
{
36-
$mariadb = stripos($version, 'mariadb') !== false;
36+
$mariadb = stripos($version, 'MariaDB') !== false;
3737
if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
3838
return new MariaDb1027Platform();
3939
}
4040

4141
if (! $mariadb) {
42-
$oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
43-
if (version_compare($oracleMysqlVersion, '8', '>=')) {
42+
if (version_compare($version, '8.0.0', '>=')) {
4443
return new MySQL80Platform();
4544
}
4645

47-
if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
46+
if (version_compare($version, '5.7.9', '>=')) {
4847
return new MySQL57Platform();
4948
}
5049
}
@@ -59,64 +58,19 @@ public function createDatabasePlatformForVersion($version)
5958
return $this->getDatabasePlatform();
6059
}
6160

62-
/**
63-
* Get a normalized 'version number' from the server string
64-
* returned by Oracle MySQL servers.
65-
*
66-
* @param string $versionString Version string returned by the driver, i.e. '5.7.10'
67-
*
68-
* @throws Exception
69-
*/
70-
private function getOracleMysqlVersionNumber(string $versionString): string
71-
{
72-
if (
73-
preg_match(
74-
'/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
75-
$versionString,
76-
$versionParts,
77-
) === 0
78-
) {
79-
throw Exception::invalidPlatformVersionSpecified(
80-
$versionString,
81-
'<major_version>.<minor_version>.<patch_version>',
82-
);
83-
}
84-
85-
$majorVersion = $versionParts['major'];
86-
$minorVersion = $versionParts['minor'] ?? 0;
87-
$patchVersion = $versionParts['patch'] ?? null;
88-
89-
if ($majorVersion === '5' && $minorVersion === '7') {
90-
$patchVersion ??= '9';
91-
}
92-
93-
return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
94-
}
95-
9661
/**
9762
* Detect MariaDB server version, including hack for some mariadb distributions
9863
* that starts with the prefix '5.5.5-'
9964
*
10065
* @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
101-
*
102-
* @throws Exception
10366
*/
10467
private function getMariaDbMysqlVersionNumber(string $versionString): string
10568
{
106-
if (
107-
preg_match(
108-
'/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
109-
$versionString,
110-
$versionParts,
111-
) === 0
112-
) {
113-
throw Exception::invalidPlatformVersionSpecified(
114-
$versionString,
115-
'^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>',
116-
);
69+
if (substr($versionString, 0, 6) === '5.5.5-') {
70+
return substr($versionString, 6);
11771
}
11872

119-
return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
73+
return $versionString;
12074
}
12175

12276
/**

tests/Driver/AbstractDriverTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Driver;
77
use Doctrine\DBAL\Driver\API\ExceptionConverter;
8-
use Doctrine\DBAL\Exception;
98
use Doctrine\DBAL\Platforms\AbstractPlatform;
109
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1110
use Doctrine\DBAL\VersionAwarePlatformDriver;
@@ -62,16 +61,6 @@ public function testCreatesDatabasePlatformForVersion(): void
6261
}
6362
}
6463

65-
public function testThrowsExceptionOnCreatingDatabasePlatformsForInvalidVersion(): void
66-
{
67-
if (! $this->driver instanceof VersionAwarePlatformDriver) {
68-
self::markTestSkipped('This test is only intended for version aware platform drivers.');
69-
}
70-
71-
$this->expectException(Exception::class);
72-
$this->driver->createDatabasePlatformForVersion('foo');
73-
}
74-
7564
public function testReturnsDatabasePlatform(): void
7665
{
7766
self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());

tests/Driver/AbstractMySQLDriverTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,17 @@ protected function getDatabasePlatformsForVersions(): array
4848
{
4949
return [
5050
['5.6.9', MySQLPlatform::class],
51-
['5.7', MySQL57Platform::class],
5251
['5.7.0', MySQLPlatform::class],
5352
['5.7.8', MySQLPlatform::class],
5453
['5.7.9', MySQL57Platform::class],
5554
['5.7.10', MySQL57Platform::class],
56-
['8', MySQL80Platform::class],
57-
['8.0', MySQL80Platform::class],
5855
['8.0.11', MySQL80Platform::class],
59-
['6', MySQL57Platform::class],
6056
['10.0.15-MariaDB-1~wheezy', MySQLPlatform::class],
6157
['5.5.5-10.1.25-MariaDB', MySQLPlatform::class],
6258
['10.1.2a-MariaDB-a1~lenny-log', MySQLPlatform::class],
6359
['5.5.40-MariaDB-1~wheezy', MySQLPlatform::class],
64-
['5.5.5-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
65-
['10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
60+
['5.5.5-10.2.8-MariaDB-1~xenial', MariaDb1027Platform::class],
61+
['5.5.5-10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
6662
['10.2.8-MariaDB-1~lenny-log', MariaDb1027Platform::class],
6763
];
6864
}

0 commit comments

Comments
 (0)