Skip to content

Commit

Permalink
Merge pull request #5795 from greg0ire/revert-5779
Browse files Browse the repository at this point in the history
Revert "Clean up MySQL version detection logic"
  • Loading branch information
derrabus committed Oct 24, 2022
2 parents 0b4e730 + d670002 commit f38ee8a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
60 changes: 53 additions & 7 deletions src/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Doctrine\Deprecations\Deprecation;

use function assert;
use function preg_match;
use function stripos;
use function substr;
use function version_compare;

/**
Expand All @@ -33,17 +33,18 @@ abstract class AbstractMySQLDriver implements VersionAwarePlatformDriver
*/
public function createDatabasePlatformForVersion($version)
{
$mariadb = stripos($version, 'MariaDB') !== false;
$mariadb = stripos($version, 'mariadb') !== false;
if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
return new MariaDb1027Platform();
}

if (! $mariadb) {
if (version_compare($version, '8.0.0', '>=')) {
$oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
if (version_compare($oracleMysqlVersion, '8', '>=')) {
return new MySQL80Platform();
}

if (version_compare($version, '5.7.9', '>=')) {
if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
return new MySQL57Platform();
}
}
Expand All @@ -58,19 +59,64 @@ public function createDatabasePlatformForVersion($version)
return $this->getDatabasePlatform();
}

/**
* Get a normalized 'version number' from the server string
* returned by Oracle MySQL servers.
*
* @param string $versionString Version string returned by the driver, i.e. '5.7.10'
*
* @throws Exception
*/
private function getOracleMysqlVersionNumber(string $versionString): string
{
if (
preg_match(
'/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
$versionString,
$versionParts,
) === 0
) {
throw Exception::invalidPlatformVersionSpecified(
$versionString,
'<major_version>.<minor_version>.<patch_version>',
);
}

$majorVersion = $versionParts['major'];
$minorVersion = $versionParts['minor'] ?? 0;
$patchVersion = $versionParts['patch'] ?? null;

if ($majorVersion === '5' && $minorVersion === '7') {
$patchVersion ??= '9';
}

return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
}

/**
* Detect MariaDB server version, including hack for some mariadb distributions
* that starts with the prefix '5.5.5-'
*
* @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
*
* @throws Exception
*/
private function getMariaDbMysqlVersionNumber(string $versionString): string
{
if (substr($versionString, 0, 6) === '5.5.5-') {
return substr($versionString, 6);
if (
preg_match(
'/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
$versionString,
$versionParts,
) === 0
) {
throw Exception::invalidPlatformVersionSpecified(
$versionString,
'^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>',
);
}

return $versionString;
return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
Expand Down Expand Up @@ -61,6 +62,16 @@ public function testCreatesDatabasePlatformForVersion(): void
}
}

public function testThrowsExceptionOnCreatingDatabasePlatformsForInvalidVersion(): void
{
if (! $this->driver instanceof VersionAwarePlatformDriver) {
self::markTestSkipped('This test is only intended for version aware platform drivers.');
}

$this->expectException(Exception::class);
$this->driver->createDatabasePlatformForVersion('foo');
}

public function testReturnsDatabasePlatform(): void
{
self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());
Expand Down
8 changes: 6 additions & 2 deletions tests/Driver/AbstractMySQLDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ protected function getDatabasePlatformsForVersions(): array
{
return [
['5.6.9', MySQLPlatform::class],
['5.7', MySQL57Platform::class],
['5.7.0', MySQLPlatform::class],
['5.7.8', MySQLPlatform::class],
['5.7.9', MySQL57Platform::class],
['5.7.10', MySQL57Platform::class],
['8', MySQL80Platform::class],
['8.0', MySQL80Platform::class],
['8.0.11', MySQL80Platform::class],
['6', MySQL57Platform::class],
['10.0.15-MariaDB-1~wheezy', MySQLPlatform::class],
['5.5.5-10.1.25-MariaDB', MySQLPlatform::class],
['10.1.2a-MariaDB-a1~lenny-log', MySQLPlatform::class],
['5.5.40-MariaDB-1~wheezy', MySQLPlatform::class],
['5.5.5-10.2.8-MariaDB-1~xenial', MariaDb1027Platform::class],
['5.5.5-10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
['5.5.5-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
['10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class],
['10.2.8-MariaDB-1~lenny-log', MariaDb1027Platform::class],
];
}
Expand Down

0 comments on commit f38ee8a

Please sign in to comment.