Skip to content

Commit db2d2bc

Browse files
authored
Remove UUID string generator strategy (#2892)
* Remove UUID generator strategy * Fix test to use a random string as ID #1525
1 parent c0a7e53 commit db2d2bc

File tree

12 files changed

+9
-328
lines changed

12 files changed

+9
-328
lines changed

docs/en/reference/basic-mapping.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,12 @@ object ID. The available strategies are:
286286
- ``ALNUM`` - Generates an alpha-numeric string (based on an incrementing value).
287287
- ``CUSTOM`` - Defers generation to an implementation of ``IdGenerator`` specified in the ``class`` option.
288288
- ``INCREMENT`` - Uses another collection to auto increment an integer identifier.
289-
- ``UUID`` - Generates a UUID identifier (deprecated).
290289
- ``NONE`` - Do not generate any identifier. ID must be manually set.
291290

292291
When using the ``AUTO`` strategy in combination with a UUID identifier, the generator can create UUIDs of type 1, type 4,
293292
and type 7 automatically. For all other UUID types, assign the identifier manually in combination with the ``NONE``
294293
strategy.
295294

296-
.. note::
297-
298-
The ``UUID`` generator is deprecated, as it stores UUIDs as strings. It is recommended to use the ``AUTO`` strategy
299-
with a ``uuid`` type identifier field instead. If you need to keep generating string UUIDs, you can use the
300-
``CUSTOM`` strategy with your own generator.
301-
302295
Here is an example how to manually set a string identifier for your documents:
303296

304297
.. configuration-block::
@@ -315,7 +308,7 @@ Here is an example how to manually set a string identifier for your documents:
315308
#[Document]
316309
class MyPersistentClass
317310
{
318-
#[Id(strategy: 'NONE', type: 'string')]
311+
#[Id(strategy: 'NONE')]
319312
public string $id;
320313
321314
//...

src/Id/AbstractIdGenerator.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Id/AutoGenerator.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Id/IncrementGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* collection. If not specified it defaults to the name of the collection for the
1919
* document.
2020
*/
21-
class IncrementGenerator extends AbstractIdGenerator
21+
class IncrementGenerator implements IdGenerator
2222
{
2323
/** @var string|null */
2424
protected $collection = null;

src/Id/ObjectIdGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use MongoDB\BSON\ObjectId;
99

1010
/** @internal */
11-
final class ObjectIdGenerator extends AbstractIdGenerator
11+
final class ObjectIdGenerator implements IdGenerator
1212
{
1313
public function generate(DocumentManager $dm, object $document): ObjectId
1414
{

src/Id/SymfonyUuidGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use function sprintf;
1818

1919
/** @internal */
20-
final class SymfonyUuidGenerator extends AbstractIdGenerator
20+
final class SymfonyUuidGenerator implements IdGenerator
2121
{
2222
private const SUPPORTED_TYPES = [
2323
1 => UuidV1::class,

src/Id/UuidGenerator.php

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/Mapping/ClassMetadata.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,6 @@
304304
*/
305305
public const GENERATOR_TYPE_INCREMENT = 2;
306306

307-
/**
308-
* UUID means Doctrine will generate a uuid for us.
309-
*
310-
* @deprecated without replacement. Use a custom generator or switch to binary UUIDs.
311-
*/
312-
public const GENERATOR_TYPE_UUID = 3;
313-
314307
/**
315308
* ALNUM means Doctrine will generate Alpha-numeric string identifiers, using the INCREMENT
316309
* generator to ensure identifier uniqueness
@@ -2166,14 +2159,6 @@ public function isIdGeneratorIncrement(): bool
21662159
return $this->generatorType === self::GENERATOR_TYPE_INCREMENT;
21672160
}
21682161

2169-
/**
2170-
* Checks whether the class will generate a uuid id.
2171-
*/
2172-
public function isIdGeneratorUuid(): bool
2173-
{
2174-
return $this->generatorType === self::GENERATOR_TYPE_UUID;
2175-
}
2176-
21772162
/**
21782163
* Checks whether the class uses no id generator.
21792164
*/

src/Mapping/ClassMetadataFactory.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Doctrine\ODM\MongoDB\Id\IncrementGenerator;
1717
use Doctrine\ODM\MongoDB\Id\ObjectIdGenerator;
1818
use Doctrine\ODM\MongoDB\Id\SymfonyUuidGenerator;
19-
use Doctrine\ODM\MongoDB\Id\UuidGenerator;
2019
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
2120
use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
2221
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
@@ -286,14 +285,6 @@ private function completeIdGeneratorMapping(ClassMetadata $class): void
286285

287286
$class->setIdGenerator($incrementGenerator);
288287
break;
289-
case ClassMetadata::GENERATOR_TYPE_UUID:
290-
$uuidGenerator = new UuidGenerator();
291-
if (isset($idGenOptions['salt'])) {
292-
$uuidGenerator->setSalt((string) $idGenOptions['salt']);
293-
}
294-
295-
$class->setIdGenerator($uuidGenerator);
296-
break;
297288
case ClassMetadata::GENERATOR_TYPE_ALNUM:
298289
$alnumGenerator = new AlnumGenerator();
299290
if (isset($idGenOptions['pad'])) {

tests/Tests/Functional/IdTest.php

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
use DateTime;
88
use Doctrine\Common\Collections\Collection;
9-
use Doctrine\ODM\MongoDB\Id\UuidGenerator;
109
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
11-
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1210
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
1311
use InvalidArgumentException;
1412
use MongoDB\BSON\Binary;
@@ -22,35 +20,11 @@
2220
use function gettype;
2321
use function is_object;
2422
use function md5;
25-
use function serialize;
2623
use function sprintf;
2724
use function ucfirst;
28-
use function unserialize;
2925

3026
class IdTest extends BaseTestCase
3127
{
32-
public function testUuidId(): void
33-
{
34-
$user = new UuidUser('Jonathan H. Wage');
35-
$this->dm->persist($user);
36-
$this->dm->flush();
37-
$id = $user->id;
38-
39-
$this->dm->clear();
40-
$check1 = $this->dm->getRepository(UuidUser::class)->findOneBy(['id' => $id]);
41-
self::assertNotNull($check1);
42-
43-
$check2 = $this->dm->createQueryBuilder(UuidUser::class)
44-
->field('id')->equals($id)->getQuery()->getSingleResult();
45-
self::assertNotNull($check2);
46-
self::assertSame($check1, $check2);
47-
48-
$check3 = $this->dm->createQueryBuilder(UuidUser::class)
49-
->field('name')->equals('Jonathan H. Wage')->getQuery()->getSingleResult();
50-
self::assertNotNull($check3);
51-
self::assertSame($check2, $check3);
52-
}
53-
5428
public function testAlnumIdChars(): void
5529
{
5630
$user = new AlnumCharsUser('Jonathan H. Wage');
@@ -146,23 +120,6 @@ public function testEmbeddedDocumentWithId(): void
146120
self::assertEquals(4, $user2->embedded[1]->id);
147121
}
148122

149-
public function testIdGeneratorInstance(): void
150-
{
151-
$class = $this->dm->getClassMetadata(UuidUser::class);
152-
self::assertEquals(ClassMetadata::GENERATOR_TYPE_UUID, $class->generatorType);
153-
self::assertEquals(['salt' => 'test'], $class->generatorOptions);
154-
self::assertInstanceOf(UuidGenerator::class, $class->idGenerator);
155-
self::assertEquals('test', $class->idGenerator->getSalt());
156-
157-
$serialized = serialize($class);
158-
$class = unserialize($serialized);
159-
160-
self::assertEquals(ClassMetadata::GENERATOR_TYPE_UUID, $class->generatorType);
161-
self::assertEquals(['salt' => 'test'], $class->generatorOptions);
162-
self::assertInstanceOf(UuidGenerator::class, $class->idGenerator);
163-
self::assertEquals('test', $class->idGenerator->getSalt());
164-
}
165-
166123
/** @param int|float $user2Id */
167124
#[DataProvider('provideEqualButNotIdenticalIds')]
168125
public function testEqualButNotIdenticalIds(string $user1Id, $user2Id): void
@@ -293,7 +250,6 @@ public static function getTestIdTypesAndStrategiesData(): array
293250

294251
// bin
295252
['bin', 'none', 'test-data', 'test-data', Binary::class],
296-
['bin', 'uuid', null, null, Binary::class],
297253
['bin_func', 'none', 'test-data', 'test-data', Binary::class],
298254
['bin_bytearray', 'none', 'test-data', 'test-data', Binary::class],
299255
['bin_uuid', 'none', 'TestTestTestTest', 'TestTestTestTest', Binary::class],
@@ -392,23 +348,6 @@ class %s
392348
}
393349
}
394350

395-
#[ODM\Document]
396-
class UuidUser
397-
{
398-
/** @var string|null */
399-
#[ODM\Id(strategy: 'uuid', options: ['salt' => 'test'])]
400-
public $id;
401-
402-
/** @var string */
403-
#[ODM\Field(name: 't', type: 'string')]
404-
public $name;
405-
406-
public function __construct(string $name)
407-
{
408-
$this->name = $name;
409-
}
410-
}
411-
412351
#[ODM\Document]
413352
class CollectionIdUser
414353
{

0 commit comments

Comments
 (0)