From 3f8430459cdad5293fb7cb7df83f1ad76bcb0b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 9 Jun 2022 23:46:50 +0200 Subject: [PATCH] Deprecate reliance on sequence-emulated identity columns Sequence-based identity values have been deprecated. --- UPGRADE.md | 5 +++++ .../ORM/Id/BigIntegerIdentityGenerator.php | 10 +++++++++ lib/Doctrine/ORM/Id/IdentityGenerator.php | 10 +++++++++ .../ORM/Mapping/ClassMetadataFactory.php | 13 ++++++++++++ .../ORM/Mapping/ClassMetadataFactoryTest.php | 21 +++++++++++++++++++ 5 files changed, 59 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index 73a04ca0e53..1a33d5b85af 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,10 @@ # Upgrade to 2.13 +## Deprecated using the `IDENTITY` identifier strategy on platform that do not support identity columns + +If identity columns are emulated with sequences on the platform you are using, +you should switch to the `SEQUENCE` strategy. + ## Deprecated passing `null` to `Doctrine\ORM\Query::setFirstResult()` `$query->setFirstResult(null);` is equivalent to `$query->setFirstResult(0)`. diff --git a/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php b/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php index 4c5408ecc9c..a6c30cf8840 100644 --- a/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php +++ b/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Id; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManagerInterface; /** @@ -27,6 +28,15 @@ class BigIntegerIdentityGenerator extends AbstractIdGenerator */ public function __construct($sequenceName = null) { + if ($sequenceName !== null) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + 'Passing a sequence name to the IdentityGenerator is deprecated in favor of using %s. $sequenceName will be removed in ORM 3.0', + SequenceGenerator::class + ); + } + $this->sequenceName = $sequenceName; } diff --git a/lib/Doctrine/ORM/Id/IdentityGenerator.php b/lib/Doctrine/ORM/Id/IdentityGenerator.php index 1a003fc49ec..9777b7fbc5d 100644 --- a/lib/Doctrine/ORM/Id/IdentityGenerator.php +++ b/lib/Doctrine/ORM/Id/IdentityGenerator.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Id; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManagerInterface; /** @@ -27,6 +28,15 @@ class IdentityGenerator extends AbstractIdGenerator */ public function __construct($sequenceName = null) { + if ($sequenceName !== null) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + 'Passing a sequence name to the IdentityGenerator is deprecated in favor of using %s. $sequenceName will be removed in ORM 3.0', + SequenceGenerator::class + ); + } + $this->sequenceName = $sequenceName; } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 408ac1f6862..860fdd5b8c7 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -33,6 +33,7 @@ use function count; use function end; use function explode; +use function get_class; use function in_array; use function is_subclass_of; use function str_contains; @@ -558,6 +559,18 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour. if ($this->getTargetPlatform()->usesSequenceEmulatedIdentityColumns()) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + <<<'DEPRECATION' +Context: Loading metadata for class %s +Problem: Using the IDENTITY generator strategy with platform "%s" is deprecated and will not be possible in Doctrine ORM 3.0. +Solution: Use the SEQUENCE generator strategy instead. +DEPRECATION + , + $class->name, + get_class($this->getTargetPlatform()) + ); $columnName = $class->getSingleIdentifierColumnName(); $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $sequencePrefix = $class->getSequencePrefix($this->getTargetPlatform()); diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 7193638a58d..dd34430fe33 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -97,6 +97,27 @@ public function testGetMetadataForSingleClass(): void self::assertTrue($cmMap1->hasField('name')); } + public function testUsingIdentityWithAPlatformThatDoesNotSupportIdentityColumnsIsDeprecated(): void + { + $cm = $this->createValidClassMetadata(); + $cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); + $cmf = new ClassMetadataFactoryTestSubject(); + $driver = $this->createMock(Driver::class); + $platform = $this->createStub(AbstractPlatform::class); + $platform->method('usesSequenceEmulatedIdentityColumns')->willReturn(true); + $platform->method('getIdentitySequenceName')->willReturn('whatever'); + $driver->method('getDatabasePlatform')->willReturn($platform); + $entityManager = $this->createEntityManager( + new MetadataDriverMock(), + new Connection([], $driver) + ); + $cmf->setEntityManager($entityManager); + $cmf->setMetadataForClass($cm->name, $cm); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8850'); + $cmf->getMetadataFor($cm->name); + } + public function testItThrowsWhenUsingAutoWithIncompatiblePlatform(): void { $cm1 = $this->createValidClassMetadata();