From c3915556a5ee0e96edb9ffbbe075019608549cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Sat, 7 Dec 2024 14:16:56 +0100 Subject: [PATCH] fix: prevent duplicate events in database --- migrations/Version20241207125955.php | 56 ++++++++++++++++++++++++++++ src/Entity/DomainEvent.php | 3 ++ src/Entity/EntityEvent.php | 3 ++ src/Service/RDAPService.php | 1 + 4 files changed, 63 insertions(+) create mode 100644 migrations/Version20241207125955.php diff --git a/migrations/Version20241207125955.php b/migrations/Version20241207125955.php new file mode 100644 index 0000000..6e16107 --- /dev/null +++ b/migrations/Version20241207125955.php @@ -0,0 +1,56 @@ +addSql('ALTER TABLE domain_entity ALTER deleted DROP DEFAULT'); + $this->addSql('ALTER TABLE domain_event ALTER deleted DROP DEFAULT'); + $this->addSql('ALTER TABLE entity_event ALTER deleted DROP DEFAULT'); + + $this->addSql(' + DELETE FROM domain_event + WHERE id NOT IN ( + SELECT MIN(id) + FROM domain_event + GROUP BY action, date, domain_id + )'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_E8D5227147CC8C92AA9E377A115F0EE5 ON domain_event (action, date, domain_id)'); + + $this->addSql(' + DELETE FROM entity_event + WHERE id NOT IN ( + SELECT MIN(id) + FROM entity_event + GROUP BY action, date, entity_id + )'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_975A3F5E47CC8C92AA9E377A81257D5D ON entity_event (action, date, entity_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP INDEX UNIQ_975A3F5E47CC8C92AA9E377A81257D5D'); + $this->addSql('DROP INDEX UNIQ_E8D5227147CC8C92AA9E377A115F0EE5'); + $this->addSql('ALTER TABLE entity_event ALTER deleted SET DEFAULT false'); + $this->addSql('ALTER TABLE domain_event ALTER deleted SET DEFAULT false'); + $this->addSql('ALTER TABLE domain_entity ALTER deleted SET DEFAULT false'); + } +} diff --git a/src/Entity/DomainEvent.php b/src/Entity/DomainEvent.php index 4648dff..40bb15a 100644 --- a/src/Entity/DomainEvent.php +++ b/src/Entity/DomainEvent.php @@ -6,6 +6,9 @@ use App\Repository\DomainEventRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: DomainEventRepository::class)] +#[ORM\UniqueConstraint( + columns: ['action', 'date', 'domain_id'] +)] class DomainEvent extends Event { #[ORM\ManyToOne(targetEntity: Domain::class, inversedBy: 'events')] diff --git a/src/Entity/EntityEvent.php b/src/Entity/EntityEvent.php index ed27d22..b4e9ed2 100644 --- a/src/Entity/EntityEvent.php +++ b/src/Entity/EntityEvent.php @@ -6,6 +6,9 @@ use App\Repository\EntityEventRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: EntityEventRepository::class)] +#[ORM\UniqueConstraint( + columns: ['action', 'date', 'entity_id'] +)] class EntityEvent extends Event { #[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')] diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index 8d9befe..c94848b 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -312,6 +312,7 @@ readonly class RDAPService if (array_key_exists('nameservers', $res) && is_array($res['nameservers'])) { $domain->getNameservers()->clear(); + $this->em->persist($domain); foreach ($res['nameservers'] as $rdapNameserver) { $nameserver = $this->nameserverRepository->findOneBy([