1
0
Fork 0

Fix structure of leaderboard tables to match leaderboard DTOs

This commit is contained in:
Krzysztof Sikorski 2022-04-21 22:23:28 +02:00
parent 6cc6038dad
commit 05fa4a6bb6
Signed by: krzysztof-sikorski
GPG Key ID: 4EB564BD08FE8476
5 changed files with 86 additions and 29 deletions

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version0009 extends AbstractMigration
{
public function getDescription(): string
{
return 'Fix structure of leaderboard tables to match leaderboard DTOs';
}
private function checkLeaderboardCount(): void
{
$sql = 'SELECT COUNT(*) FROM nexus_leaderboard_category';
$categoryCount = $this->connection->executeQuery(sql: $sql)->fetchOne();
$sql = 'SELECT COUNT(*) FROM nexus_leaderboard';
$leaderboardCount = $this->connection->executeQuery(sql: $sql)->fetchOne();
$sql = 'SELECT COUNT(*) FROM nexus_leaderboard_entry';
$entryCount = $this->connection->executeQuery(sql: $sql)->fetchOne();
$this->abortIf(
condition: ($categoryCount > 0) || ($leaderboardCount > 0) || ($entryCount > 0),
message: 'This migration can only be executed on empty leaderboard tables!',
);
}
public function preUp(Schema $schema): void
{
$this->checkLeaderboardCount();
}
public function up(Schema $schema): void
{
$this->addSql('DROP INDEX nexus_leaderboard_category_uniq');
$this->addSql('ALTER TABLE nexus_leaderboard_category DROP career');
$this->addSql('ALTER TABLE nexus_leaderboard_category ADD type TEXT NOT NULL');
$this->addSql(
'CREATE UNIQUE INDEX nexus_leaderboard_category_uniq ON nexus_leaderboard_category (name, type)'
);
$this->addSql(
'CREATE UNIQUE INDEX nexus_leaderboard_entry_uniq ON nexus_leaderboard_entry (leaderboard_id, position)'
);
}
public function preDown(Schema $schema): void
{
$this->checkLeaderboardCount();
}
public function down(Schema $schema): void
{
$this->addSql('DROP INDEX nexus_leaderboard_entry_uniq');
$this->addSql('DROP INDEX nexus_leaderboard_category_uniq');
$this->addSql('ALTER TABLE nexus_leaderboard_category DROP type');
$this->addSql('ALTER TABLE nexus_leaderboard_category ADD career BOOLEAN NOT NULL');
$this->addSql(
'CREATE UNIQUE INDEX nexus_leaderboard_category_uniq ON nexus_leaderboard_category (name, career)'
);
}
}

View File

@ -18,7 +18,7 @@ class Leaderboard implements LeaderboardInterface
private EntryListInterface $entries;
public function __construct(
private ?string $title = null,
private ?string $name = null,
private ?string $type = null,
private ?string $scoreLabel = null,
) {
@ -27,12 +27,12 @@ class Leaderboard implements LeaderboardInterface
public function getName(): ?string
{
return $this->title;
return $this->name;
}
public function setName(string $name): void
{
$this->title = $name;
$this->name = $name;
}
public function getType(): ?string

View File

@ -13,7 +13,7 @@ use App\Doctrine\Entity\UuidPrimaryKeyTrait;
use Doctrine\ORM\Mapping as ORM;
#[
ORM\Entity(),
ORM\Entity,
ORM\Table(name: 'nexus_leaderboard'),
ORM\UniqueConstraint(name: 'nexus_leaderboard_uniq', fields: ['category', 'gamePeriod']),
ORM\Index(fields: ['category'], name: 'nexus_leaderboard_category_idx'),

View File

@ -5,27 +5,27 @@ declare(strict_types=1);
namespace App\Doctrine\Entity\Nexus;
use App\Contract\Doctrine\Entity\UuidPrimaryKeyInterface;
use App\Contract\Entity\LeaderboardTypes;
use App\Doctrine\Entity\UuidPrimaryKeyTrait;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[
ORM\Entity(),
ORM\Entity,
ORM\Table(name: 'nexus_leaderboard_category'),
ORM\UniqueConstraint(name: 'nexus_leaderboard_category_uniq', fields: ['name', 'career']),
ORM\UniqueConstraint(name: 'nexus_leaderboard_category_uniq', fields: ['name', 'type']),
]
class LeaderboardCategory implements UuidPrimaryKeyInterface
{
use UuidPrimaryKeyTrait;
#[ORM\Column(name: 'name', type: 'text', nullable: false)]
#[ORM\Column(name: 'name', type: Types::TEXT, nullable: false)]
private ?string $name = null;
#[ORM\Column(name: 'score_label', type: 'text', nullable: false)]
#[ORM\Column(name: 'score_label', type: Types::TEXT, nullable: false)]
private ?string $scoreLabel = null;
#[ORM\Column(name: 'career', type: 'boolean', nullable: false)]
private bool $career = false;
#[ORM\Column(name: 'type', type: Types::TEXT, nullable: false)]
private ?string $type = null;
public function __construct()
{
@ -54,23 +54,11 @@ class LeaderboardCategory implements UuidPrimaryKeyInterface
public function getType(): ?string
{
return $this->career ? LeaderboardTypes::CAREER : LeaderboardTypes::BREATH;
return $this->type;
}
public function setType(string $type): void
{
$this->career = LeaderboardTypes::CAREER === $type;
}
public function getCareer(): bool
{
return $this->career;
}
public function setCareer(bool $career): self
{
$this->career = $career;
return $this;
$this->type = $type;
}
}

View File

@ -4,11 +4,13 @@ declare(strict_types=1);
namespace App\Doctrine\Entity\Nexus;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[
ORM\Entity(),
ORM\Entity,
ORM\Table(name: 'nexus_leaderboard_entry'),
ORM\UniqueConstraint(name: 'nexus_leaderboard_entry_uniq', fields: ['leaderboard', 'position']),
ORM\Index(fields: ['leaderboard'], name: 'nexus_leaderboard_entry_leaderboard_idx'),
]
class LeaderboardEntry
@ -22,14 +24,14 @@ class LeaderboardEntry
#[
ORM\Id,
ORM\Column(name: 'position', type: 'integer', nullable: false),
ORM\Column(name: 'position', type: Types::INTEGER, nullable: false),
]
private ?int $position = null;
#[ORM\Column(name: 'character_name', type: 'text', nullable: false)]
#[ORM\Column(name: 'character_name', type: Types::TEXT, nullable: false)]
private ?string $characterName = null;
#[ORM\Column(name: 'score', type: 'integer', nullable: false)]
#[ORM\Column(name: 'score', type: Types::INTEGER, nullable: false)]
private ?int $score = null;
public function getLeaderboard(): ?Leaderboard