Make sure all tables have created_at and last_modified_at columns
This commit is contained in:
parent
340257aa3f
commit
15a8d3b279
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version0002 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Make sure all tables have created_at and last_modified_at columns';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE "user" ADD last_modified_at TIMESTAMP(0) WITH TIME ZONE NOT NULL DEFAULT now()');
|
||||
$this->addSql('ALTER TABLE "user" ALTER last_modified_at DROP DEFAULT');
|
||||
$this->addSql("COMMENT ON COLUMN \"user\".last_modified_at IS '(DC2Type:datetimetz_immutable)'");
|
||||
|
||||
$this->addSql(
|
||||
'ALTER TABLE user_access_token ADD last_modified_at TIMESTAMP(0) WITH TIME ZONE NOT NULL DEFAULT now()'
|
||||
);
|
||||
$this->addSql('ALTER TABLE user_access_token ALTER last_modified_at DROP DEFAULT');
|
||||
$this->addSql("COMMENT ON COLUMN user_access_token.last_modified_at IS '(DC2Type:datetimetz_immutable)'");
|
||||
|
||||
$this->addSql('DROP INDEX nexus_raw_data_sorting_idx');
|
||||
$this->addSql('ALTER TABLE nexus_raw_data RENAME COLUMN submitted_at TO created_at');
|
||||
$this->addSql('CREATE INDEX nexus_raw_data_sorting_idx ON nexus_raw_data (created_at, request_started_at, id)');
|
||||
|
||||
$this->addSql(
|
||||
'ALTER TABLE nexus_raw_data ADD last_modified_at TIMESTAMP(0) WITH TIME ZONE NOT NULL DEFAULT now()'
|
||||
);
|
||||
$this->addSql('ALTER TABLE nexus_raw_data ALTER last_modified_at DROP DEFAULT');
|
||||
$this->addSql("COMMENT ON COLUMN nexus_raw_data.last_modified_at IS '(DC2Type:datetimetz_immutable)'");
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP INDEX nexus_raw_data_sorting_idx');
|
||||
$this->addSql('ALTER TABLE nexus_raw_data RENAME created_at TO submitted_at');
|
||||
$this->addSql(
|
||||
'CREATE INDEX nexus_raw_data_sorting_idx ON nexus_raw_data (submitted_at, request_started_at, id)'
|
||||
);
|
||||
|
||||
$this->addSql('ALTER TABLE nexus_raw_data DROP last_modified_at');
|
||||
$this->addSql('ALTER TABLE user_access_token DROP last_modified_at');
|
||||
$this->addSql('ALTER TABLE "user" DROP last_modified_at');
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Contract\Entity;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
interface BaseEntityInterface
|
||||
|
@ -11,4 +12,12 @@ interface BaseEntityInterface
|
|||
public function getId(): Uuid;
|
||||
|
||||
public function setId(Uuid $id): void;
|
||||
|
||||
public function getCreatedAt(): ?DateTimeImmutable;
|
||||
|
||||
public function setCreatedAt(DateTimeImmutable $createdAt): void;
|
||||
|
||||
public function getLastModifiedAt(): ?DateTimeImmutable;
|
||||
|
||||
public function setLastModifiedAt(DateTimeImmutable $lastModifiedAt): void;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ namespace App\Entity;
|
|||
|
||||
use App\Contract\Config\AppParameters;
|
||||
use App\Contract\Entity\BaseEntityInterface;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
|
@ -17,6 +19,12 @@ abstract class BaseEntity implements BaseEntityInterface
|
|||
]
|
||||
protected Uuid $id;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: Types::DATETIMETZ_IMMUTABLE, nullable: false)]
|
||||
protected ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
#[ORM\Column(name: 'last_modified_at', type: Types::DATETIMETZ_IMMUTABLE, nullable: false)]
|
||||
protected ?DateTimeImmutable $lastModifiedAt = null;
|
||||
|
||||
public function __construct(?Uuid $id = null)
|
||||
{
|
||||
$this->id = $id ?? Uuid::v4();
|
||||
|
@ -31,4 +39,24 @@ abstract class BaseEntity implements BaseEntityInterface
|
|||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
}
|
||||
|
||||
public function getLastModifiedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->lastModifiedAt;
|
||||
}
|
||||
|
||||
public function setLastModifiedAt(DateTimeImmutable $lastModifiedAt): void
|
||||
{
|
||||
$this->lastModifiedAt = $lastModifiedAt;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,14 +15,11 @@ use JsonSerializable;
|
|||
#[
|
||||
ORM\Entity(repositoryClass: NexusRawDataRepository::class),
|
||||
ORM\Table(name: 'nexus_raw_data'),
|
||||
ORM\Index(columns: ['submitted_at', 'request_started_at', 'id'], name: 'nexus_raw_data_sorting_idx'),
|
||||
ORM\Index(columns: ['created_at', 'request_started_at', 'id'], name: 'nexus_raw_data_sorting_idx'),
|
||||
ORM\Index(columns: ['submitter_id'], name: 'nexus_raw_data_submitter_idx'),
|
||||
]
|
||||
class NexusRawData extends BaseEntity implements BaseEntityInterface, JsonSerializable
|
||||
{
|
||||
#[ORM\Column(name: 'submitted_at', type: Types::DATETIMETZ_IMMUTABLE, nullable: false)]
|
||||
private ?DateTimeImmutable $submittedAt = null;
|
||||
|
||||
#[
|
||||
ORM\ManyToOne(targetEntity: User::class),
|
||||
ORM\JoinColumn(name: 'submitter_id', referencedColumnName: 'id', nullable: false),
|
||||
|
@ -51,7 +48,7 @@ class NexusRawData extends BaseEntity implements BaseEntityInterface, JsonSerial
|
|||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'submittedAt' => $this->getSubmittedAt()?->format(DateTimeInterface::ISO8601),
|
||||
'submittedAt' => $this->getCreatedAt()?->format(DateTimeInterface::ISO8601),
|
||||
'submitterId' => $this->getSubmitter()?->getId(),
|
||||
'requestStartedAt' => $this->getRequestStartedAt()?->format(DateTimeInterface::ISO8601),
|
||||
'responseCompletedAt' => $this->getResponseCompletedAt()?->format(DateTimeInterface::ISO8601),
|
||||
|
@ -62,16 +59,6 @@ class NexusRawData extends BaseEntity implements BaseEntityInterface, JsonSerial
|
|||
];
|
||||
}
|
||||
|
||||
public function getSubmittedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->submittedAt;
|
||||
}
|
||||
|
||||
public function setSubmittedAt(?DateTimeImmutable $submittedAt): void
|
||||
{
|
||||
$this->submittedAt = $submittedAt;
|
||||
}
|
||||
|
||||
public function getSubmitter(): ?User
|
||||
{
|
||||
return $this->submitter;
|
||||
|
|
|
@ -7,7 +7,6 @@ namespace App\Entity;
|
|||
use App\Contract\Config\AppParameters;
|
||||
use App\Contract\Entity\BaseEntityInterface;
|
||||
use App\Repository\UserRepository;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
@ -25,9 +24,6 @@ class User extends BaseEntity
|
|||
{
|
||||
public const USERNAME_MAX_LENGTH = 180;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: Types::DATETIMETZ_IMMUTABLE, nullable: false)]
|
||||
private ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
#[ORM\Column(name: 'username', type: Types::STRING, length: self::USERNAME_MAX_LENGTH, nullable: false)]
|
||||
private ?string $username = null;
|
||||
|
||||
|
@ -45,16 +41,6 @@ class User extends BaseEntity
|
|||
return $this->getUserIdentifier();
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since Symfony 5.3, use getUserIdentifier instead
|
||||
*/
|
||||
|
|
|
@ -22,9 +22,6 @@ class UserAccessToken extends BaseEntity implements BaseEntityInterface, JsonSer
|
|||
#[ORM\Column(name: 'value', type: Types::TEXT, unique: true, nullable: false)]
|
||||
private ?string $value = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: Types::DATETIMETZ_IMMUTABLE, nullable: false)]
|
||||
private ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
#[ORM\Column(name: 'valid_until', type: Types::DATETIMETZ_IMMUTABLE, nullable: true)]
|
||||
private ?DateTimeImmutable $validUntil = null;
|
||||
|
||||
|
@ -44,16 +41,6 @@ class UserAccessToken extends BaseEntity implements BaseEntityInterface, JsonSer
|
|||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
}
|
||||
|
||||
public function getValidUntil(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->validUntil;
|
||||
|
|
|
@ -41,7 +41,8 @@ final class NexusRawDataManager
|
|||
|
||||
$submitter = $userAccessToken->getOwner();
|
||||
|
||||
$nexusRawData->setSubmittedAt(submittedAt: $submittedAt);
|
||||
$nexusRawData->setCreatedAt(createdAt: $submittedAt);
|
||||
$nexusRawData->setLastModifiedAt(lastModifiedAt: $submittedAt);
|
||||
$nexusRawData->setSubmitter(submitter: $submitter);
|
||||
|
||||
$this->entityManager->persist($nexusRawData);
|
||||
|
|
|
@ -34,6 +34,7 @@ final class UserAccessTokenManager
|
|||
$token->setOwner(owner: $owner);
|
||||
$token->setValue(value: $this->generateValue());
|
||||
$token->setCreatedAt(createdAt: $createdAt);
|
||||
$token->setLastModifiedAt(lastModifiedAt: $createdAt);
|
||||
$token->setValidUntil(validUntil: $validUntil);
|
||||
|
||||
$this->entityManager->persist($token);
|
||||
|
|
|
@ -35,6 +35,7 @@ final class UserManager
|
|||
|
||||
$user = new User(id: $uuid);
|
||||
$user->setCreatedAt(createdAt: $createdAt);
|
||||
$user->setLastModifiedAt(lastModifiedAt: $createdAt);
|
||||
$user->setUsername(username: $username);
|
||||
$user->setRoles(roles: $roles);
|
||||
$user->setEnabled(enabled: true);
|
||||
|
|
Reference in New Issue