From f1bb1969aca1da041d63ec7e5fde7ea7347710a7 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 11 Sep 2021 21:00:38 +0200 Subject: [PATCH] Bump DBAL and remove compat code Signed-off-by: Alexander M. Turek --- CacheWarmer/DoctrineMetadataCacheWarmer.php | 10 +------- ConnectionFactory.php | 15 +++--------- DependencyInjection/DoctrineExtension.php | 24 ++++--------------- Tests/Command/DropDatabaseDoctrineTest.php | 10 ++------ Tests/ConnectionFactoryTest.php | 8 ++----- .../AbstractDoctrineExtensionTest.php | 9 +------ .../DoctrineExtensionTest.php | 4 ---- composer.json | 4 ++-- 8 files changed, 16 insertions(+), 68 deletions(-) diff --git a/CacheWarmer/DoctrineMetadataCacheWarmer.php b/CacheWarmer/DoctrineMetadataCacheWarmer.php index 140bdac91..af9571bfd 100644 --- a/CacheWarmer/DoctrineMetadataCacheWarmer.php +++ b/CacheWarmer/DoctrineMetadataCacheWarmer.php @@ -7,10 +7,8 @@ use LogicException; use Symfony\Bundle\FrameworkBundle\CacheWarmer\AbstractPhpFileCacheWarmer; use Symfony\Component\Cache\Adapter\ArrayAdapter; -use Symfony\Component\Cache\DoctrineProvider; use function is_file; -use function method_exists; class DoctrineMetadataCacheWarmer extends AbstractPhpFileCacheWarmer { @@ -49,13 +47,7 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter): bool throw new LogicException('DoctrineMetadataCacheWarmer must load metadata first, check priority of your warmers.'); } - if (method_exists($metadataFactory, 'setCache')) { - $metadataFactory->setCache($arrayAdapter); - } elseif ($metadataFactory instanceof AbstractClassMetadataFactory) { - // BC with doctrine/persistence < 2.2 - $metadataFactory->setCacheDriver(new DoctrineProvider($arrayAdapter)); - } - + $metadataFactory->setCache($arrayAdapter); $metadataFactory->getAllMetadata(); return true; diff --git a/ConnectionFactory.php b/ConnectionFactory.php index 28078b93f..fc3911aa1 100644 --- a/ConnectionFactory.php +++ b/ConnectionFactory.php @@ -5,16 +5,14 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\DriverManager; -use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; use function array_merge; -use function class_exists; use function is_subclass_of; use function trigger_deprecation; @@ -62,11 +60,7 @@ public function createConnection(array $params, ?Configuration $config = null, ? if (isset($params['wrapperClass'])) { if (! is_subclass_of($params['wrapperClass'], Connection::class)) { - if (class_exists(DBALException::class)) { - throw DBALException::invalidWrapperClass($params['wrapperClass']); - } - - throw Exception::invalidWrapperClass($params['wrapperClass']); + throw DBALException::invalidWrapperClass($params['wrapperClass']); } $wrapperClass = $params['wrapperClass']; @@ -122,16 +116,13 @@ public function createConnection(array $params, ?Configuration $config = null, ? * For details have a look at DoctrineBundle issue #673. * * @throws DBALException - * @throws Exception */ private function getDatabasePlatform(Connection $connection): AbstractPlatform { try { return $connection->getDatabasePlatform(); } catch (DriverException $driverException) { - $exceptionClass = class_exists(DBALException::class) ? DBALException::class : Exception::class; - - throw new $exceptionClass( + throw new DBALException( 'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL . "You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL . 'For further information have a look at:' . PHP_EOL . diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index 555387363..8934616bc 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -12,10 +12,8 @@ use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Connections\MasterSlaveConnection; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; use Doctrine\DBAL\Logging\LoggerChain; -use Doctrine\DBAL\SQLParserUtils; use Doctrine\DBAL\Tools\Console\Command\ImportCommand; use Doctrine\DBAL\Tools\Console\ConnectionProvider; use Doctrine\ORM\EntityManagerInterface; @@ -107,12 +105,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container) $loader->load('dbal.xml'); $chainLogger = $container->getDefinition('doctrine.dbal.logger.chain'); $logger = new Reference('doctrine.dbal.logger'); - if (! method_exists(SQLParserUtils::class, 'getPositionalPlaceholderPositions') && method_exists(LoggerChain::class, 'addLogger')) { - // doctrine/dbal < 2.10.0 - $chainLogger->addMethodCall('addLogger', [$logger]); - } else { - $chainLogger->addArgument([$logger]); - } + $chainLogger->addArgument([$logger]); if (class_exists(ImportCommand::class)) { $container->register('doctrine.database_import_command', ImportDoctrineCommand::class) @@ -181,12 +174,7 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder 'doctrine.dbal.logger.chain', LoggerChain::class ); - if (! method_exists(SQLParserUtils::class, 'getPositionalPlaceholderPositions') && method_exists(LoggerChain::class, 'addLogger')) { - // doctrine/dbal < 2.10.0 - $chainLogger->addMethodCall('addLogger', [$profilingLogger]); - } else { - $chainLogger->addArgument([$logger, $profilingLogger]); - } + $chainLogger->addArgument([$logger, $profilingLogger]); $loggerId = 'doctrine.dbal.logger.chain.' . $name; $container->setDefinition($loggerId, $chainLogger); @@ -315,7 +303,7 @@ protected function getConnectionOptions(array $connection): array 'options' => 'driverOptions', 'driver_class' => 'driverClass', 'wrapper_class' => 'wrapperClass', - 'keep_slave' => class_exists(PrimaryReadReplicaConnection::class) ? 'keepReplica' : 'keepSlave', + 'keep_slave' => 'keepReplica', 'keep_replica' => 'keepReplica', 'replicas' => 'replica', 'shard_choser' => 'shardChoser', @@ -364,15 +352,13 @@ protected function getConnectionOptions(array $connection): array continue; } - $options[class_exists(PrimaryReadReplicaConnection::class) ? 'primary' : 'master'][$key] = $value; + $options['primary'][$key] = $value; unset($options[$key]); } if (empty($options['wrapperClass'])) { // Change the wrapper class only if user did not configure custom one. - $options['wrapperClass'] = class_exists(PrimaryReadReplicaConnection::class) ? - PrimaryReadReplicaConnection::class : // dbal >= 2.11 - MasterSlaveConnection::class; // dbal < 2.11 + $options['wrapperClass'] = PrimaryReadReplicaConnection::class; } } else { unset($options['slaves'], $options['replica']); diff --git a/Tests/Command/DropDatabaseDoctrineTest.php b/Tests/Command/DropDatabaseDoctrineTest.php index cee8c84af..9c71cdafc 100644 --- a/Tests/Command/DropDatabaseDoctrineTest.php +++ b/Tests/Command/DropDatabaseDoctrineTest.php @@ -4,9 +4,8 @@ use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DriverManager; -use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\Persistence\ManagerRegistry; use Generator; use PHPUnit\Framework\MockObject\MockObject; @@ -16,7 +15,6 @@ use Symfony\Component\DependencyInjection\Container; use function array_merge; -use function class_exists; use function sprintf; use function sys_get_temp_dir; @@ -67,11 +65,7 @@ public function testExecute(array $options): void */ public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(array $options): void { - if (class_exists(DBALException::class)) { - $this->expectException(DBALException::class); - } else { - $this->expectException(Exception::class); - } + $this->expectException(DBALException::class); $this->testExecute($options); } diff --git a/Tests/ConnectionFactoryTest.php b/Tests/ConnectionFactoryTest.php index 979ca32dd..bf42ed32c 100644 --- a/Tests/ConnectionFactoryTest.php +++ b/Tests/ConnectionFactoryTest.php @@ -6,10 +6,10 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Driver\Exception as InternalDriverException; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; @@ -42,11 +42,7 @@ public function testContainer(): void // put the mock into the fake driver FakeDriver::$exception = $exception; - if (class_exists(DBALException::class)) { - $this->expectException(DBALException::class); - } else { - $this->expectException(\Doctrine\DBAL\Exception::class); - } + $this->expectException(DBALException::class); try { $factory->createConnection($params, $config, $eventManager, $mappingTypes); diff --git a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php index 674efeaa1..8adf8a7aa 100644 --- a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php +++ b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php @@ -11,7 +11,6 @@ use Doctrine\Common\Cache\CacheProvider; use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Doctrine\DBAL\Configuration; -use Doctrine\DBAL\Connections\MasterSlaveConnection; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; use Doctrine\DBAL\DriverManager; use Doctrine\ORM\EntityManager; @@ -35,7 +34,6 @@ use function array_keys; use function array_values; use function assert; -use function class_exists; use function interface_exists; use function method_exists; use function sprintf; @@ -185,12 +183,7 @@ public function testDbalLoadSingleMasterSlaveConnection(): void $container = $this->loadContainer('dbal_service_single_master_slave_connection'); $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); - $this->assertEquals( - class_exists(PrimaryReadReplicaConnection::class) ? - PrimaryReadReplicaConnection::class : // dbal >= 2.11 - MasterSlaveConnection::class, // dbal < 2.11,x - $param['wrapperClass'] - ); + $this->assertEquals(PrimaryReadReplicaConnection::class, $param['wrapperClass']); $this->assertTrue($param['keepReplica']); $this->assertEquals( [ diff --git a/Tests/DependencyInjection/DoctrineExtensionTest.php b/Tests/DependencyInjection/DoctrineExtensionTest.php index f0dece540..9829e3e83 100644 --- a/Tests/DependencyInjection/DoctrineExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -743,10 +743,6 @@ public function testAnnotationsBundleMappingDetection(): void public function testAttributesBundleMappingDetection(): void { - if (! class_exists(AttributeDriver::class)) { - self::markTestSkipped('This test requires ORM 2.9 with attribute driver.'); - } - if (PHP_VERSION_ID < 70400) { self::markTestSkipped('This test requires PHP 7.4.'); } diff --git a/composer.json b/composer.json index 57fcd900f..68ced3168 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,8 @@ "php": "^7.1 || ^8.0", "doctrine/annotations": "^1", "doctrine/cache": "^1.11 || ^2.0", - "doctrine/dbal": "^2.9.0|^3.0", - "doctrine/persistence": "^1.3.3|^2.0", + "doctrine/dbal": "^2.13.1|^3.1", + "doctrine/persistence": "^2.2", "doctrine/sql-formatter": "^1.0.1", "symfony/cache": "^4.3.3|^5.0|^6.0", "symfony/config": "^4.4.3|^5.0|^6.0",