Skip to content

Commit

Permalink
Recommend SEQUENCE until doctrine/dbal 4 is released
Browse files Browse the repository at this point in the history
Using IDENTITY with doctrine/dbal 3 results in SERIAL, which is not
recommended.
  • Loading branch information
greg0ire committed Nov 5, 2023
1 parent 16028e4 commit be1614a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 51 deletions.
13 changes: 6 additions & 7 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ Because of that, it is now deprecated to rely on the historical defaults when
they differ from what we recommend now.

Instead, you should pick a strategy for each database platform you use, and it
will be used when using `AUTO`. As of now, only PostgreSQL is affected by this.
It is recommended that PostgreSQL user configure their new applications to use
`IDENTITY`:
will be used when using `AUTO`. As of now, no platform is affected by this, but
PostgreSQL will be when `doctrine/dbal` 4 is released.

It is recommended that PostgreSQL users configure their existing and new
applications to use `SEQUENCE` until that happens:

```php
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Configuration;

assert($configuration instanceof Configuration);
$configuration->setIdentityGenerationPreferences([
PostgreSQLPlatform::CLASS => ClassMetadata::GENERATOR_TYPE_IDENTITY,
PostgreSQLPlatform::CLASS => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
]);
```

If migrating an existing application is too costly, the deprecation can be
addressed by configuring `SEQUENCE` as the default strategy.

## Deprecate `EntityManagerInterface::getPartialReference()`

This method does not have a replacement and will be removed in 3.0.
Expand Down
14 changes: 0 additions & 14 deletions docs/en/reference/basic-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,6 @@ defaults to the identifier generation mechanism your current database
vendor preferred at the time that strategy was introduced:
``AUTO_INCREMENT`` with MySQL, sequences with PostgreSQL and Oracle and
so on.
We now recommend using ``IDENTITY`` for PostgreSQL, and you can achieve
that while still using the ``AUTO`` strategy, by configuring what it
defaults to.

.. code-block:: php
<?php
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Configuration;
$config = new Configuration();
$config->setIdentityGenerationPreferences([
PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
]);

.. _identifier-generation-strategies:

Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory

private const RECOMMENDED_STRATEGY = [
'Doctrine\DBAL\Platforms\MySqlPlatform' => 'IDENTITY',
'Doctrine\DBAL\Platforms\PostgreSqlPlatform' => 'IDENTITY',
'Doctrine\DBAL\Platforms\PostgreSqlPlatform' => 'SEQUENCE',
Platforms\DB2Platform::class => 'IDENTITY',
Platforms\MySQLPlatform::class => 'IDENTITY',
Platforms\OraclePlatform::class => 'SEQUENCE',
Platforms\PostgreSQLPlatform::class => 'IDENTITY',
Platforms\PostgreSQLPlatform::class => 'SEQUENCE',
Platforms\SQLServerPlatform::class => 'IDENTITY',
Platforms\SqlitePlatform::class => 'IDENTITY',
];
Expand Down Expand Up @@ -657,7 +657,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
'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.
Problem: Using identity columns emulated with a sequence is deprecated and will not be possible in Doctrine ORM 3.0.
Solution: Use the SEQUENCE generator strategy instead.
DEPRECATION
,
Expand Down
27 changes: 0 additions & 27 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -170,32 +169,6 @@ private function setUpCmfForPlatform(AbstractPlatform $platform, array $preferen
return $cmf;
}

public function testRelyingOnLegacyIdGenerationDefaultsIsDeprecatedIfItResultsInASuboptimalDefault(): void
{
$cm = $this->createValidClassMetadata();
$cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);

$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform());
$cmf->setMetadataForClass($cm->name, $cm);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
$cmf->getMetadataFor($cm->name);
}

public function testSpecifyingIdGenerationStrategyThroughConfigurationFixesTheDeprecation(): void
{
$cm = $this->createValidClassMetadata();
$cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);

$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform(), [
PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
]);
$cmf->setMetadataForClass($cm->name, $cm);

$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
$cmf->getMetadataFor($cm->name);
}

public function testRelyingOnLegacyIdGenerationDefaultsIsOKIfItResultsInTheCurrentlyRecommendedStrategyBeingUsed(): void
{
$cm = $this->createValidClassMetadata();
Expand Down

0 comments on commit be1614a

Please sign in to comment.