Skip to content

Commit 300cffb

Browse files
committed
Recommend SEQUENCE until doctrine/dbal 4 is released
Using IDENTITY with doctrine/dbal 3 results in SERIAL, which is not recommended.
1 parent 16028e4 commit 300cffb

File tree

5 files changed

+30
-61
lines changed

5 files changed

+30
-61
lines changed

UPGRADE.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@ It will be a full-fledged class, no longer extending
99

1010
When the `AUTO` identifier generation strategy was introduced, the best
1111
strategy at the time was selected for each database platform.
12-
A lot of time has passed since then, and support for better strategies has been
13-
added.
12+
A lot of time has passed since then, and with ORM 3.0.0 and DBAL 4.0.0, support
13+
for better strategies will be added.
1414

1515
Because of that, it is now deprecated to rely on the historical defaults when
16-
they differ from what we recommend now.
16+
they differ from what we will be recommended in the future.
1717

1818
Instead, you should pick a strategy for each database platform you use, and it
1919
will be used when using `AUTO`. As of now, only PostgreSQL is affected by this.
20-
It is recommended that PostgreSQL user configure their new applications to use
21-
`IDENTITY`:
20+
21+
It is recommended that PostgreSQL users configure their existing and new
22+
applications to use `SEQUENCE` until `doctrine/dbal` 4.0.0 is released:
2223

2324
```php
2425
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
2526
use Doctrine\ORM\Configuration;
2627

2728
assert($configuration instanceof Configuration);
2829
$configuration->setIdentityGenerationPreferences([
29-
PostgreSQLPlatform::CLASS => ClassMetadata::GENERATOR_TYPE_IDENTITY,
30+
PostgreSQLPlatform::CLASS => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
3031
]);
3132
```
3233

33-
If migrating an existing application is too costly, the deprecation can be
34-
addressed by configuring `SEQUENCE` as the default strategy.
34+
When DBAL 4 is released, `AUTO` will result in `IDENTITY`, and the above
35+
configuration should be removed to migrate to it.
3536

3637
## Deprecate `EntityManagerInterface::getPartialReference()`
3738

docs/en/reference/basic-mapping.rst

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,6 @@ defaults to the identifier generation mechanism your current database
427427
vendor preferred at the time that strategy was introduced:
428428
``AUTO_INCREMENT`` with MySQL, sequences with PostgreSQL and Oracle and
429429
so on.
430-
We now recommend using ``IDENTITY`` for PostgreSQL, and you can achieve
431-
that while still using the ``AUTO`` strategy, by configuring what it
432-
defaults to.
433-
434-
.. code-block:: php
435-
436-
<?php
437-
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
438-
use Doctrine\ORM\Configuration;
439-
440-
$config = new Configuration();
441-
$config->setIdentityGenerationPreferences([
442-
PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
443-
]);
444430

445431
.. _identifier-generation-strategies:
446432

lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
use function assert;
3636
use function class_exists;
37-
use function constant;
3837
use function count;
3938
use function end;
4039
use function explode;
@@ -74,26 +73,10 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
7473
/** @var mixed[] */
7574
private $embeddablesActiveNesting = [];
7675

77-
private const LEGACY_DEFAULTS_FOR_ID_GENERATION = [
78-
'Doctrine\DBAL\Platforms\MySqlPlatform' => ClassMetadata::GENERATOR_TYPE_IDENTITY,
76+
private const NON_IDENTITY_DEFAULT_STRATEGY = [
7977
'Doctrine\DBAL\Platforms\PostgreSqlPlatform' => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
80-
Platforms\DB2Platform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
81-
Platforms\MySQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
8278
Platforms\OraclePlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
8379
Platforms\PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
84-
Platforms\SQLServerPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
85-
Platforms\SqlitePlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
86-
];
87-
88-
private const RECOMMENDED_STRATEGY = [
89-
'Doctrine\DBAL\Platforms\MySqlPlatform' => 'IDENTITY',
90-
'Doctrine\DBAL\Platforms\PostgreSqlPlatform' => 'IDENTITY',
91-
Platforms\DB2Platform::class => 'IDENTITY',
92-
Platforms\MySQLPlatform::class => 'IDENTITY',
93-
Platforms\OraclePlatform::class => 'SEQUENCE',
94-
Platforms\PostgreSQLPlatform::class => 'IDENTITY',
95-
Platforms\SQLServerPlatform::class => 'IDENTITY',
96-
Platforms\SqlitePlatform::class => 'IDENTITY',
9780
];
9881

9982
/** @return void */
@@ -657,7 +640,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
657640
'https://github.com/doctrine/orm/issues/8850',
658641
<<<'DEPRECATION'
659642
Context: Loading metadata for class %s
660-
Problem: Using the IDENTITY generator strategy with platform "%s" is deprecated and will not be possible in Doctrine ORM 3.0.
643+
Problem: Using identity columns emulated with a sequence is deprecated and will not be possible in Doctrine ORM 3.0.
661644
Solution: Use the SEQUENCE generator strategy instead.
662645
DEPRECATION
663646
,
@@ -762,27 +745,25 @@ private function determineIdGeneratorStrategy(AbstractPlatform $platform): int
762745
}
763746
}
764747

765-
foreach (self::LEGACY_DEFAULTS_FOR_ID_GENERATION as $platformFamily => $strategy) {
748+
foreach (self::NON_IDENTITY_DEFAULT_STRATEGY as $platformFamily => $strategy) {
766749
if (is_a($platform, $platformFamily)) {
767-
$recommendedStrategyName = self::RECOMMENDED_STRATEGY[$platformFamily];
768-
if ($strategy !== constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $recommendedStrategyName)) {
750+
if ($platform instanceof Platforms\PostgreSQLPlatform || is_a($platform, 'Doctrine\DBAL\Platforms\PostgreSqlPlatform')) {
769751
Deprecation::trigger(
770752
'doctrine/orm',
771753
'https://github.com/doctrine/orm/issues/8893',
772754
<<<'DEPRECATION'
773-
Relying on non-optimal defaults for ID generation is deprecated.
755+
Relying on non-optimal defaults for ID generation is deprecated, and IDENTITY
756+
results in SERIAL, which is not recommended.
774757
Instead, configure identifier generation strategies explicitly through
775758
configuration.
776-
We currently recommend "%s" for "%s", so you should use
759+
We currently recommend "SEQUENCE" for "%s", so you should use
777760
$configuration->setIdentityGenerationPreferences([
778-
"%s" => ClassMetadata::GENERATOR_TYPE_%s,
761+
"%s" => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
779762
]);
780763
DEPRECATION
781764
,
782-
$recommendedStrategyName,
783-
$platformFamily,
784765
$platformFamily,
785-
$recommendedStrategyName
766+
$platformFamily
786767
);
787768
}
788769

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@
537537
<code>$class</code>
538538
<code>$class</code>
539539
<code>$platformFamily</code>
540+
<code><![CDATA['Doctrine\DBAL\Platforms\PostgreSqlPlatform']]></code>
540541
<code><![CDATA[new $definition['class']()]]></code>
541542
</ArgumentTypeCoercion>
542543
<DeprecatedClass>

tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,37 @@ private function setUpCmfForPlatform(AbstractPlatform $platform, array $preferen
170170
return $cmf;
171171
}
172172

173-
public function testRelyingOnLegacyIdGenerationDefaultsIsDeprecatedIfItResultsInASuboptimalDefault(): void
173+
public function testRelyingOnLegacyIdGenerationDefaultsIsOKIfItResultsInTheCurrentlyRecommendedStrategyBeingUsed(): void
174174
{
175175
$cm = $this->createValidClassMetadata();
176176
$cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
177-
178-
$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform());
177+
$cmf = $this->setUpCmfForPlatform(new OraclePlatform());
179178
$cmf->setMetadataForClass($cm->name, $cm);
180179

181-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
180+
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
182181
$cmf->getMetadataFor($cm->name);
183182
}
184183

185-
public function testSpecifyingIdGenerationStrategyThroughConfigurationFixesTheDeprecation(): void
184+
public function testRelyingOnLegacyIdGenerationDefaultsIsDeprecatedIfItResultsInADefaultThatWillChange(): void
186185
{
187186
$cm = $this->createValidClassMetadata();
188187
$cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
189188

190-
$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform(), [
191-
PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY,
192-
]);
189+
$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform());
193190
$cmf->setMetadataForClass($cm->name, $cm);
194191

195-
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
192+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');
196193
$cmf->getMetadataFor($cm->name);
197194
}
198195

199-
public function testRelyingOnLegacyIdGenerationDefaultsIsOKIfItResultsInTheCurrentlyRecommendedStrategyBeingUsed(): void
196+
public function testSpecifyingIdGenerationStrategyThroughConfigurationFixesTheDeprecation(): void
200197
{
201198
$cm = $this->createValidClassMetadata();
202199
$cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
203-
$cmf = $this->setUpCmfForPlatform(new OraclePlatform());
200+
201+
$cmf = $this->setUpCmfForPlatform(new PostgreSQLPlatform(), [
202+
PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
203+
]);
204204
$cmf->setMetadataForClass($cm->name, $cm);
205205

206206
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8893');

0 commit comments

Comments
 (0)