<?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 Version20220425225739 extends AbstractMigration
{
public function getDescription(): string
{
return 'Bench';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
ini_set('memory_limit', '-1');
$this->connection->executeQuery('ALTER TABLE banc ADD player_id INT NOT NULL, ADD game_id INT NOT NULL, ADD team_id INT NOT NULL');
$lastId = 0;
$missing = 0;
while (true) {
$benches = $this->connection->fetchAllAssociative("SELECT idbanc, idjoueurservice_banc, idmatchservice_banc, idequipeservice_banc FROM banc WHERE idbanc > {$lastId} LIMIT 1000");
if (empty($benches)) {
break;
}
foreach ($benches as $bench) {
$benchId = $lastId = $bench['idbanc'];
$playerServiceId = $bench['idjoueurservice_banc'];
$gameServiceId = $bench['idmatchservice_banc'];
$teamServiceId = $bench['idequipeservice_banc'];
$playerId = $this->connection->fetchOne("SELECT idjoueur FROM joueur WHERE idjoueurservice_joueur = {$playerServiceId}");
$gameId = $this->connection->fetchOne("SELECT idmatch FROM `match` WHERE idmatchservice_match = {$gameServiceId}");
$teamId = $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$teamServiceId}");
if ($playerId && $gameId && $teamId) {
$this->connection->executeQuery("UPDATE banc SET player_id = {$playerId}, game_id = {$gameId}, team_id = {$teamId} WHERE idbanc = {$benchId}");
} else {
$this->connection->executeQuery("DELETE FROM banc WHERE idbanc = {$benchId}");
++$missing;
if (!$playerId) {
echo "Bench.id {$benchId} : Missing Player.serviceId {$playerServiceId}\n";
} elseif (!$gameId) {
echo "Bench.id {$benchId} : Missing Game.serviceId {$gameServiceId}\n";
} elseif (!$teamId) {
echo "Bench.id {$benchId} : Missing Team.serviceId {$teamServiceId}\n";
}
}
}
}
echo "Finished. {$missing} Benches have been deleted\n";
$this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB374899E6F5DF FOREIGN KEY (player_id) REFERENCES joueur (idjoueur)');
$this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB3748E48FD905 FOREIGN KEY (game_id) REFERENCES `match` (idmatch)');
$this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB3748296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe)');
$this->addSql('CREATE INDEX IDX_D6BB374899E6F5DF ON banc (player_id)');
$this->addSql('CREATE INDEX IDX_D6BB3748E48FD905 ON banc (game_id)');
$this->addSql('CREATE INDEX IDX_D6BB3748296CD8AE ON banc (team_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB374899E6F5DF');
$this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB3748E48FD905');
$this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB3748296CD8AE');
$this->addSql('DROP INDEX IDX_D6BB374899E6F5DF ON banc');
$this->addSql('DROP INDEX IDX_D6BB3748E48FD905 ON banc');
$this->addSql('DROP INDEX IDX_D6BB3748296CD8AE ON banc');
$this->addSql('ALTER TABLE banc DROP player_id, DROP game_id, DROP team_id');
}
}