Skip to content

Commit

Permalink
spliting uuid and timestamps to be first and last on the tables, assi…
Browse files Browse the repository at this point in the history
…gning AbstractEntity as MappedSuperclass for doctrine
  • Loading branch information
Claudiu Pintiuta committed Jun 13, 2024
1 parent 5fb5047 commit db46147
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240611131923 extends AbstractMigration
final class Version20240613153602 extends AbstractMigration
{
public function getDescription(): string
{
Expand All @@ -20,22 +20,22 @@ public function getDescription(): string
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE admin (identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_1614D53DD73087E9 (userUuid), INDEX IDX_1614D53D88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_role (name VARCHAR(30) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name VARCHAR(30) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_access_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, user_id VARCHAR(25) DEFAULT NULL, token VARCHAR(100) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_CA42527C19EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_access_token_scopes (access_token_id INT UNSIGNED NOT NULL, scope_id INT UNSIGNED NOT NULL, INDEX IDX_9FDF62E92CCB2688 (access_token_id), INDEX IDX_9FDF62E9682B5931 (scope_id), PRIMARY KEY(access_token_id, scope_id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_auth_codes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expiresDatetime DATETIME DEFAULT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_BB493F8319EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_auth_code_scopes (auth_code_id INT UNSIGNED NOT NULL, scope_id INT UNSIGNED NOT NULL, INDEX IDX_988BFFBF69FEDEE4 (auth_code_id), INDEX IDX_988BFFBF682B5931 (scope_id), PRIMARY KEY(auth_code_id, scope_id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_clients (id INT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, secret VARCHAR(100) DEFAULT NULL, redirect VARCHAR(191) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, isConfidential TINYINT(1) DEFAULT 0 NOT NULL, user_id BINARY(16) DEFAULT NULL, INDEX IDX_13CE8101A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_refresh_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, access_token_id INT UNSIGNED DEFAULT NULL, INDEX IDX_5AB6872CCB2688 (access_token_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_scopes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, scope VARCHAR(191) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user (identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_54FCD59FD73087E9 (userUuid), INDEX IDX_54FCD59F88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_avatar (name VARCHAR(191) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_detail (firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_reset_password (expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_role (name VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_avatar (uuid BINARY(16) NOT NULL, name VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_detail (uuid BINARY(16) NOT NULL, firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_role (uuid BINARY(16) NOT NULL, name VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53DD73087E9 FOREIGN KEY (userUuid) REFERENCES admin (uuid)');
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53D88446210 FOREIGN KEY (roleUuid) REFERENCES admin_role (uuid)');
$this->addSql('ALTER TABLE oauth_access_tokens ADD CONSTRAINT FK_CA42527C19EB6921 FOREIGN KEY (client_id) REFERENCES oauth_clients (id)');
Expand Down
2 changes: 2 additions & 0 deletions src/Admin/src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Api\App\Entity\AbstractEntity;
use Api\App\Entity\PasswordTrait;
use Api\App\Entity\RoleInterface;
use Api\App\Entity\TimestampsTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -19,6 +20,7 @@
class Admin extends AbstractEntity implements UserEntityInterface
{
use PasswordTrait;
use TimestampsTrait;

public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
Expand Down
3 changes: 3 additions & 0 deletions src/Admin/src/Entity/AdminRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
use Api\Admin\Repository\AdminRoleRepository;
use Api\App\Entity\AbstractEntity;
use Api\App\Entity\RoleInterface;
use Api\App\Entity\TimestampsTrait;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AdminRoleRepository::class)]
#[ORM\Table("admin_role")]
#[ORM\HasLifecycleCallbacks]
class AdminRole extends AbstractEntity implements RoleInterface
{
use TimestampsTrait;

public const ROLE_ADMIN = 'admin';
public const ROLE_SUPERUSER = 'superuser';
public const ROLES = [
Expand Down
42 changes: 2 additions & 40 deletions src/App/src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Api\App\Entity;

use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Laminas\Stdlib\ArraySerializableInterface;
use Ramsey\Uuid\Uuid;
Expand All @@ -14,60 +13,23 @@
use function method_exists;
use function ucfirst;

#[ORM\MappedSuperclass]

Check warning on line 16 in src/App/src/Entity/AbstractEntity.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MappedSuperclass'
abstract class AbstractEntity implements ArraySerializableInterface
{
#[ORM\Id]
#[ORM\Column(name: 'uuid', type: "uuid_binary", unique: true)]
protected UuidInterface $uuid;

#[ORM\Column(name: "created", type: "datetime_immutable")]
protected DateTimeImmutable $created;

#[ORM\Column(name: "updated", type: "datetime_immutable", nullable: true)]
protected ?DateTimeImmutable $updated = null;

public function __construct()
{
$this->uuid = Uuid::uuid4();
$this->created = new DateTimeImmutable();
$this->uuid = Uuid::uuid4();
}

public function getUuid(): UuidInterface
{
return $this->uuid;
}

public function getCreated(): ?DateTimeImmutable
{
return $this->created;
}

public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string
{
return $this->created->format($dateFormat);
}

public function getUpdated(): ?DateTimeImmutable
{
return $this->updated;
}

public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string
{
if ($this->updated instanceof DateTimeImmutable) {
return $this->updated->format($dateFormat);
}

return null;
}

#[ORM\PrePersist]
#[ORM\PreUpdate]
public function touch(): void
{
$this->updated = new DateTimeImmutable();
}

public function exchangeArray(array $array): void
{
foreach ($array as $property => $values) {
Expand Down
55 changes: 55 additions & 0 deletions src/App/src/Entity/TimestampsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Api\App\Entity;

use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;

Check warning on line 8 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Mapping'

Check warning on line 8 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'

#[ORM\HasLifecycleCallbacks]

Check warning on line 10 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'HasLifecycleCallbacks'

Check warning on line 10 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'
trait TimestampsTrait
{
#[ORM\Column(name: "created", type: "datetime_immutable")]

Check warning on line 13 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Column'

Check warning on line 13 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'
protected DateTimeImmutable $created;

#[ORM\Column(name: "updated", type: "datetime_immutable", nullable: true)]

Check warning on line 16 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Column'

Check warning on line 16 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'
protected ?DateTimeImmutable $updated = null;

public function getCreated(): ?DateTimeImmutable
{
return $this->created;
}

public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string
{
return $this->created->format($dateFormat);
}

public function getUpdated(): ?DateTimeImmutable
{
return $this->updated;
}

public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string
{
if ($this->updated instanceof DateTimeImmutable) {
return $this->updated->format($dateFormat);
}

return null;
}

#[ORM\PrePersist]

Check warning on line 43 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'PrePersist'

Check warning on line 43 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'
public function created(): void
{
$this->created = new DateTimeImmutable();
$this->updated = new DateTimeImmutable();
}

#[ORM\PreUpdate]

Check warning on line 50 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'PreUpdate'

Check warning on line 50 in src/App/src/Entity/TimestampsTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'ORM'
public function touch(): void
{
$this->updated = new DateTimeImmutable();
}
}
2 changes: 2 additions & 0 deletions src/User/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Api\App\Entity\AbstractEntity;
use Api\App\Entity\PasswordTrait;
use Api\App\Entity\RoleInterface;
use Api\App\Entity\TimestampsTrait;
use Api\User\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
Expand All @@ -23,6 +24,7 @@
class User extends AbstractEntity implements UserEntityInterface
{
use PasswordTrait;
use TimestampsTrait;

public const STATUS_PENDING = 'pending';
public const STATUS_ACTIVE = 'active';
Expand Down
3 changes: 3 additions & 0 deletions src/User/src/Entity/UserAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Api\User\Entity;

use Api\App\Entity\AbstractEntity;
use Api\App\Entity\TimestampsTrait;
use Api\User\EventListener\UserAvatarEventListener;
use Api\User\Repository\UserAvatarRepository;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -15,6 +16,8 @@
#[ORM\EntityListeners([UserAvatarEventListener::class])]
class UserAvatar extends AbstractEntity
{
use TimestampsTrait;

#[ORM\OneToOne(inversedBy: "avatar", targetEntity: User::class)]
#[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")]
protected User $user;
Expand Down
3 changes: 3 additions & 0 deletions src/User/src/Entity/UserDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Api\User\Entity;

use Api\App\Entity\AbstractEntity;
use Api\App\Entity\TimestampsTrait;
use Api\User\Repository\UserDetailRepository;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -13,6 +14,8 @@
#[ORM\HasLifecycleCallbacks]
class UserDetail extends AbstractEntity
{
use TimestampsTrait;

#[ORM\OneToOne(inversedBy: "detail", targetEntity: User::class)]
#[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")]
protected User $user;
Expand Down
3 changes: 3 additions & 0 deletions src/User/src/Entity/UserResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Api\User\Entity;

use Api\App\Entity\AbstractEntity;
use Api\App\Entity\TimestampsTrait;
use Api\User\Repository\UserResetPasswordRepository;
use DateInterval;
use DateTime;
Expand All @@ -17,6 +18,8 @@
#[ORM\HasLifecycleCallbacks]
class UserResetPassword extends AbstractEntity
{
use TimestampsTrait;

public const STATUS_COMPLETED = 'completed';
public const STATUS_REQUESTED = 'requested';
public const STATUSES = [
Expand Down
3 changes: 3 additions & 0 deletions src/User/src/Entity/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Api\App\Entity\AbstractEntity;
use Api\App\Entity\RoleInterface;
use Api\App\Entity\TimestampsTrait;
use Api\User\Repository\UserRoleRepository;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -14,6 +15,8 @@
#[ORM\HasLifecycleCallbacks]
class UserRole extends AbstractEntity implements RoleInterface
{
use TimestampsTrait;

public const ROLE_GUEST = 'guest';
public const ROLE_USER = 'user';
public const ROLES = [
Expand Down
5 changes: 4 additions & 1 deletion test/Functional/Traits/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function runSeeders(): void
$loader->loadFromDirectory($path);

$fixtures = $loader->getFixtures();
$executor->execute($fixtures, true);

foreach ($fixtures as $fixture) {
$executor->load($entityManager, $fixture);
}
}
}

0 comments on commit db46147

Please sign in to comment.