From 80855fca1ff007e1c3fae587f38d4ed07e99407e Mon Sep 17 00:00:00 2001 From: Damien Harper Date: Tue, 10 Sep 2024 15:11:09 +0200 Subject: [PATCH] Enhanced database version checking (#219) --- .../Persistence/Helper/PlatformHelper.php | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php b/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php index f72e5bd..f1f8bbd 100644 --- a/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php +++ b/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php @@ -5,6 +5,8 @@ namespace DH\Auditor\Provider\Doctrine\Persistence\Helper; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\MariaDBPlatform; +use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Types\Types; abstract class PlatformHelper @@ -25,45 +27,56 @@ public static function isIndexLengthLimited(string $name, Connection $connection return false; } + $platform = $connection->getDatabasePlatform(); $version = self::getServerVersion($connection); if (null === $version) { - // Assume no index length limitation return false; } - $mariadb = false !== mb_stripos($version, 'mariadb'); - if ($mariadb && version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.2', '<')) { - return true; + // JSON wasn't supported on MariaDB before 10.2.7 + // @see https://mariadb.com/kb/en/json-data-type/ + if ($platform instanceof MariaDBPlatform) { + return version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.2', '<'); + } + + if ($platform instanceof MySQLPlatform) { + return version_compare(self::getOracleMysqlVersionNumber($version), '5.7.7', '<'); } - return !$mariadb && version_compare(self::getOracleMysqlVersionNumber($version), '5.7.7', '<'); + return false; } public static function getServerVersion(Connection $connection): ?string { + $reflected = new \ReflectionObject($connection); + if ($reflected->hasMethod('getServerVersion') && $reflected->getMethod('getServerVersion')->isPublic()) { + return $connection->getServerVersion(); + } if (method_exists($connection, 'getWrappedConnection')) { return $connection->getWrappedConnection()->getServerVersion(); } - if (method_exists($connection, 'getNativeConnection')) { - return $connection->getServerVersion(); - } return null; } public static function isJsonSupported(Connection $connection): bool { - $version = self::getServerVersion($connection); - - if (null === $version || false !== mb_stripos($version, 'mariadb')) { - // Assume JSON is supported - return true; - } + $platform = $connection->getDatabasePlatform(); // JSON wasn't supported on MariaDB before 10.2.7 // @see https://mariadb.com/kb/en/json-data-type/ - return version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.7', '<'); + if ($platform instanceof MariaDBPlatform) { + $version = self::getServerVersion($connection); + + if (null === $version) { + return true; + } + + return version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.7', '<'); + } + + return true; } /**