Skip to content

Commit

Permalink
refactor: move integer id to uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwannLeRoux committed Feb 21, 2023
1 parent 5c28591 commit bac390b
Show file tree
Hide file tree
Showing 29 changed files with 281 additions and 68 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/string": " 6.1.*",
"symfony/translation": " 6.1.*",
"symfony/twig-bundle": " 6.1.*",
"symfony/uid": " 6.1.*",
"symfony/validator": " 6.1.*",
"symfony/web-link": " 6.1.*",
"symfony/yaml": " 6.1.*",
Expand Down
158 changes: 157 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions config/doctrine/orm/Dinosaur.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
table="dinosaur"
>

<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<id name="id" type="uuid" />

<field name="name" column="name" type="string" length="255" nullable="false" unique="false" />
<field name="gender" column="gender" type="string" length="255" nullable="false" unique="false" />
Expand Down
4 changes: 1 addition & 3 deletions config/doctrine/orm/Species.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
table="species"
>

<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<id name="id" type="uuid" />

<field name="name" column="name" type="string" length="255" nullable="false" unique="true" />
<field name="habitats" column="habitats" type="simple_array" nullable="false" unique="false" />
Expand Down
37 changes: 37 additions & 0 deletions migrations/Version20230217084031.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230217084031 extends AbstractMigration
{
public function getDescription(): string
{
return 'Migrating integer ids to UUIDs';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE dinosaur DROP FOREIGN KEY FK_DAEDC56EB2A1D860');
$this->addSql('ALTER TABLE dinosaur CHANGE id id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', CHANGE species_id species_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE species CHANGE id id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE dinosaur ADD CONSTRAINT FK_DAEDC56EB2A1D860 FOREIGN KEY (species_id) REFERENCES species (id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE dinosaur DROP FOREIGN KEY FK_DAEDC56EB2A1D860');
$this->addSql('ALTER TABLE dinosaur CHANGE id id INT AUTO_INCREMENT NOT NULL, CHANGE species_id species_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE species CHANGE id id INT AUTO_INCREMENT NOT NULL');
$this->addSql('ALTER TABLE dinosaur ADD CONSTRAINT FK_DAEDC56EB2A1D860 FOREIGN KEY (species_id) REFERENCES species (id)');
}
}
5 changes: 4 additions & 1 deletion src/Command/ConsumeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$dinosaur = $dinosaurs[$randomDinoKey];

$consumeMessage = new Consume($dinosaur->getId(), $dinosaur->getName());
$consumeMessage = new Consume(
$dinosaur->getId()->toRfc4122(),
$dinosaur->getName()
);

$this->bus->dispatch($consumeMessage);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Controller/DinosaursController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function list(Request $request, ManagerRegistry $doctrine): Response
#[Route(
'/dinosaurs/{id}',
name: 'app_single_dinosaur',
requirements: ['id' => '\d+']
requirements: ['id' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$']
)]
public function single(string $id, ManagerRegistry $doctrine): Response
{
Expand All @@ -71,7 +71,7 @@ public function single(string $id, ManagerRegistry $doctrine): Response
}

#[Route('/dinosaurs/create', name: 'app_create_dinosaur')]
public function create(Request $request, ManagerRegistry $doctrine): Response
public function create(Request $request): Response
{
$form = $this->createForm(DinosaurType::class);

Expand Down Expand Up @@ -105,9 +105,9 @@ public function create(Request $request, ManagerRegistry $doctrine): Response
#[Route(
'/dinosaurs/{id}/edit',
name: 'app_edit_dinosaur',
requirements: ['id' => '\d+']
requirements: ['id' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$']
)]
public function edit(Request $request, int $id, ManagerRegistry $doctrine): Response
public function edit(Request $request, string $id, ManagerRegistry $doctrine): Response
{
$dinosaur = $doctrine
->getRepository(Dinosaur::class)
Expand Down Expand Up @@ -149,9 +149,9 @@ public function edit(Request $request, int $id, ManagerRegistry $doctrine): Resp
#[Route(
'/dinosaurs/{id}/remove',
name: 'app_remove_dinosaur',
requirements: ['id' => '\d+']
requirements: ['id' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$']
)]
public function remove(int $id): Response
public function remove(string $id): Response
{
$this->bus->dispatch(new Delete($id));

Expand Down
8 changes: 4 additions & 4 deletions src/Controller/SpeciesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function create(Request $request, ManagerRegistry $doctrine): Response
#[Route(
'/species/{id}/edit',
name: 'app_edit_species',
requirements: ['id' => '\d+']
requirements: ['id' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$']
)]
public function edit(Request $request, int $id, ManagerRegistry $doctrine): Response
public function edit(Request $request, string $id, ManagerRegistry $doctrine): Response
{
$species = $doctrine
->getRepository(Species::class)
Expand Down Expand Up @@ -108,9 +108,9 @@ public function edit(Request $request, int $id, ManagerRegistry $doctrine): Resp
#[Route(
'/species/{id}/remove',
name: 'app_remove_species',
requirements: ['id' => '\d+']
requirements: ['id' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$']
)]
public function remove(int $id): Response
public function remove(string $id): Response
{
$this->bus->dispatch(new Delete($id));

Expand Down
15 changes: 12 additions & 3 deletions src/Entity/Dinosaur.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Entity;

use Symfony\Component\Uid\Uuid;

class Dinosaur
{
private int $id;
private ?Uuid $id;
private string $name;
private string $gender;
private Species $species;
Expand All @@ -18,8 +20,10 @@ public function __construct(
Species $species,
int $age,
string $eyesColor,
Park $park
Park $park,
?Uuid $id = null
) {
$this->id = $id ?? Uuid::v4();
$this->name = $name;
$this->gender = $gender;
$this->species = $species;
Expand All @@ -28,11 +32,16 @@ public function __construct(
$this->park = $park;
}

public function getId(): int
public function getId(): ?Uuid
{
return $this->id;
}

public function setId(?Uuid $id): void
{
$this->id = $id;
}

public function getName(): string
{
return $this->name;
Expand Down
Loading

0 comments on commit bac390b

Please sign in to comment.