Skip to content

Commit

Permalink
Merge pull request #9838 from greg0ire/remove-identity-columns-emulation
Browse files Browse the repository at this point in the history
Stop allowing identity columns emulation
  • Loading branch information
greg0ire authored Jun 11, 2022
2 parents 894dc4f + 2d230ce commit 78c9490
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 176 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 3.0

## BC BREAK: Removed identity columns emulation through sequences

If the platform you are using does not support identity columns, you should
switch to the `SEQUENCE` strategy.

## BC BREAK: Made setters parameters mandatory

The following methods require an argument when being called. Pass `null`
Expand Down
21 changes: 1 addition & 20 deletions lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ORM\Id;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -14,27 +13,9 @@
*/
class BigIntegerIdentityGenerator extends AbstractIdGenerator
{
/**
* @param string|null $sequenceName The name of the sequence to pass to lastInsertId()
* to obtain the last generated identifier within the current
* database session/connection, if any.
*/
public function __construct(
private ?string $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
);
}
}

public function generateId(EntityManagerInterface $em, ?object $entity): string
{
return (string) $em->getConnection()->lastInsertId($this->sequenceName);
return (string) $em->getConnection()->lastInsertId();
}

public function isPostInsertGenerator(): bool
Expand Down
21 changes: 1 addition & 20 deletions lib/Doctrine/ORM/Id/IdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ORM\Id;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -14,27 +13,9 @@
*/
class IdentityGenerator extends AbstractIdGenerator
{
/**
* @param string|null $sequenceName The name of the sequence to pass to lastInsertId()
* to obtain the last generated identifier within the current
* database session/connection, if any.
*/
public function __construct(
private ?string $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
);
}
}

public function generateId(EntityManagerInterface $em, ?object $entity): int
{
return (int) $em->getConnection()->lastInsertId($this->sequenceName);
return (int) $em->getConnection()->lastInsertId();
}

public function isPostInsertGenerator(): bool
Expand Down
38 changes: 2 additions & 36 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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;
Expand Down Expand Up @@ -491,42 +490,9 @@ private function completeIdGeneratorMapping(ClassMetadata $class): void
$sequenceName = null;
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;

// 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());
$sequenceName = $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix, $columnName);
$definition = [
'sequenceName' => $this->truncateSequenceName($sequenceName),
];

if ($quoted) {
$definition['quoted'] = true;
}

$sequenceName = $this
->em
->getConfiguration()
->getQuoteStrategy()
->getSequenceName($definition, $class, $this->getTargetPlatform());
}

$generator = $fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint'
? new BigIntegerIdentityGenerator($sequenceName)
: new IdentityGenerator($sequenceName);
? new BigIntegerIdentityGenerator()
: new IdentityGenerator();

$class->setIdGenerator($generator);

Expand Down
7 changes: 0 additions & 7 deletions phpstan-dbal4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ parameters:
message: '~^Variable \$e on left side of \?\? always exists and is not nullable\.$~'
path: lib/Doctrine/ORM/UnitOfWork.php

# See https://github.com/doctrine/orm/issues/8850
-
message: '~^Method Doctrine\\DBAL\\Connection::lastInsertId\(\) invoked with 1 parameter, 0 required\.$~'
paths:
- lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php
- lib/Doctrine/ORM/Id/IdentityGenerator.php

-
message: '~^Parameter #1 \$command of method Symfony\\Component\\Console\\Application::add\(\) expects Symfony\\Component\\Console\\Command\\Command, Doctrine\\DBAL\\Tools\\Console\\Command\\ReservedWordsCommand given\.$~'
path: lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
<code>$this-&gt;em</code>
<code>$this-&gt;em</code>
</PossiblyNullArgument>
<PossiblyNullReference occurrences="9">
<PossiblyNullReference occurrences="8">
<code>getAllClassNames</code>
<code>getConfiguration</code>
<code>getConfiguration</code>
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@ 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();
Expand Down

0 comments on commit 78c9490

Please sign in to comment.