fix: prevent duplicate events in database

This commit is contained in:
Maël Gangloff
2024-12-07 14:16:56 +01:00
parent 8870a7c75e
commit c3915556a5
4 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?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 Version20241207125955 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View File

@@ -6,6 +6,9 @@ use App\Repository\DomainEventRepository;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: DomainEventRepository::class)] #[ORM\Entity(repositoryClass: DomainEventRepository::class)]
#[ORM\UniqueConstraint(
columns: ['action', 'date', 'domain_id']
)]
class DomainEvent extends Event class DomainEvent extends Event
{ {
#[ORM\ManyToOne(targetEntity: Domain::class, inversedBy: 'events')] #[ORM\ManyToOne(targetEntity: Domain::class, inversedBy: 'events')]

View File

@@ -6,6 +6,9 @@ use App\Repository\EntityEventRepository;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EntityEventRepository::class)] #[ORM\Entity(repositoryClass: EntityEventRepository::class)]
#[ORM\UniqueConstraint(
columns: ['action', 'date', 'entity_id']
)]
class EntityEvent extends Event class EntityEvent extends Event
{ {
#[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')] #[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')]

View File

@@ -312,6 +312,7 @@ readonly class RDAPService
if (array_key_exists('nameservers', $res) && is_array($res['nameservers'])) { if (array_key_exists('nameservers', $res) && is_array($res['nameservers'])) {
$domain->getNameservers()->clear(); $domain->getNameservers()->clear();
$this->em->persist($domain);
foreach ($res['nameservers'] as $rdapNameserver) { foreach ($res['nameservers'] as $rdapNameserver) {
$nameserver = $this->nameserverRepository->findOneBy([ $nameserver = $this->nameserverRepository->findOneBy([