Skip to content

Commit

Permalink
fix(NodesTags): Added a single UUID field to prevent primary key over…
Browse files Browse the repository at this point in the history
…lap.
  • Loading branch information
ambroisemaupate committed Aug 4, 2023
1 parent e10c8a4 commit 68fff41
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
42 changes: 42 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20230804153629.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230804153629 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create a single UUID field primary key on nodes_tags table.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX `primary` ON nodes_tags');
$this->addSql('ALTER TABLE nodes_tags ADD id VARCHAR(36) DEFAULT NULL');
$this->addSql('UPDATE nodes_tags SET id = UUID() WHERE id IS NULL');
$this->addSql('ALTER TABLE nodes_tags CHANGE id id VARCHAR(36) NOT NULL');
$this->addSql('ALTER TABLE nodes_tags ADD PRIMARY KEY (id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX `PRIMARY` ON nodes_tags');
$this->addSql('ALTER TABLE nodes_tags DROP id');
$this->addSql('ALTER TABLE nodes_tags ADD PRIMARY KEY (node_id, tag_id, position)');
}

public function isTransactional(): bool
{
return false;
}
}
14 changes: 12 additions & 2 deletions lib/RoadizCoreBundle/src/Entity/NodesTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class NodesTags implements PositionedInterface, Comparable

#[
ORM\Id,
ORM\Column(type:"string", length:36),
SymfonySerializer\Ignore
]
/** @phpstan-ignore-next-line */
protected ?string $id = null;

#[
ORM\ManyToOne(targetEntity: Node::class, inversedBy: "nodesTags"),
ORM\JoinColumn(
name: "node_id",
Expand All @@ -40,7 +47,6 @@ class NodesTags implements PositionedInterface, Comparable
private Node $node;

#[
ORM\Id,
ORM\ManyToOne(targetEntity: Tag::class, inversedBy: "nodesTags"),
ORM\JoinColumn(
name: "tag_id",
Expand All @@ -55,13 +61,17 @@ class NodesTags implements PositionedInterface, Comparable
private Tag $tag;

#[
ORM\Id,
ORM\Column(type: "float", nullable: false, options: ['default' => 1]),
SymfonySerializer\Ignore,
Serializer\Exclude,
]
protected float $position = 0.0;

public function __construct(?string $uuid = null)
{
$this->id = $uuid ?? \Ramsey\Uuid\Uuid::uuid4()->toString();
}

/**
* @return Node
*/
Expand Down

0 comments on commit 68fff41

Please sign in to comment.