feat: add createdAt and updatedAt on Domain entity

This commit is contained in:
Maël Gangloff
2024-07-17 19:29:43 +02:00
parent 358cad6c21
commit 11c7207a1c
3 changed files with 77 additions and 1 deletions

View File

@@ -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 Version20240717172250 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 ADD COLUMN created_at DATE NOT NULL');
$this->addSql('ALTER TABLE domain ADD COLUMN updated_at DATE NOT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__domain AS SELECT ldh_name, handle, status FROM domain');
$this->addSql('DROP TABLE domain');
$this->addSql('CREATE TABLE domain (ldh_name VARCHAR(255) NOT NULL, handle VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array)
, PRIMARY KEY(ldh_name))');
$this->addSql('INSERT INTO domain (ldh_name, handle, status) SELECT ldh_name, handle, status FROM __temp__domain');
$this->addSql('DROP TABLE __temp__domain');
}
}

View File

@@ -8,6 +8,7 @@ use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Controller\DomainRefreshController;
use App\Repository\DomainRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
@@ -98,6 +99,12 @@ class Domain
#[Groups(['domain:item'])]
private Collection $nameservers;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?DateTimeImmutable $createdAt = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->events = new ArrayCollection();
@@ -252,4 +259,36 @@ class Domain
return $this;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->setUpdatedAt(new DateTimeImmutable('now'));
if ($this->getCreatedAt() === null) {
$this->setCreatedAt(new DateTimeImmutable('now'));
}
}
private function setUpdatedAt(?DateTimeImmutable $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
private function setCreatedAt(?DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}
}

View File

@@ -163,7 +163,7 @@ readonly class RDAPService
$domain->addNameserver($nameserver);
}
$domain->updateTimestamps();
$this->em->persist($domain);
$this->em->flush();