migrations/Version20220419114109.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20220419114109 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Game + Group';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         ini_set('memory_limit''-1');
  19.         $this->connection->executeQuery('ALTER TABLE `match` ADD league_id INT NOT NULL, ADD team1_id INT NOT NULL, ADD team2_id INT NOT NULL, ADD team_winner_id INT DEFAULT NULL');
  20.         $lastId 0;
  21.         $missing $missingGroups 0;
  22.         $leagues $teams $groups = [];
  23.         while (true) {
  24.             $games $this->connection->fetchAllAssociative("SELECT idmatch, idligue_match, idequipe1_match, idequipe2_match, idequipegagnante_match, idgroupe_match FROM `match` WHERE idmatch > {$lastId} LIMIT 1000");
  25.             if (empty($games)) {
  26.                 break;
  27.             }
  28.             $queries = [];
  29.             foreach ($games as $game) {
  30.                 $gameId $lastId $game['idmatch'];
  31.                 $leagueServiceId $game['idligue_match'];
  32.                 $team1ServiceId $game['idequipe1_match'];
  33.                 $team2ServiceId $game['idequipe2_match'];
  34.                 $winnerTeamServiceId $game['idequipegagnante_match'];
  35.                 $groupId $game['idgroupe_match'];
  36.                 $leagueId $leagues[$leagueServiceId] ?? $this->connection->fetchOne("SELECT idligue FROM ligue WHERE idligueservice_ligue = {$leagueServiceId}");
  37.                 $team1Id $teams[$team1ServiceId] ?? $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$team1ServiceId}");
  38.                 $team2Id $teams[$team2ServiceId] ?? $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$team2ServiceId}");
  39.                 $winnerTeamId = !$winnerTeamServiceId null : ($teams[$winnerTeamServiceId] ?? $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$winnerTeamServiceId}"));
  40.                 $groupIdVerified null === $groupId null : ($groups[$groupId] ?? $this->connection->fetchOne("SELECT idgroupe FROM groupe WHERE idgroupe = {$groupId}"));
  41.                 if (null !== $groupId && !$groupIdVerified) {
  42.                     echo "Game.id {$gameId} : Missing Group.id {$groupId}\n";
  43.                     $queries[] = "UPDATE `match` SET idgroupe_match = NULL WHERE idmatch = {$gameId}";
  44.                     ++$missingGroups;
  45.                 }
  46.                 if ($leagueId && $team1Id && $team2Id) {
  47.                     $queries[] = "UPDATE `match` SET league_id = {$leagueId}, team1_id = {$team1Id}, team2_id = {$team2Id}, team_winner_id = " . ($winnerTeamId ?? 'NULL') . " WHERE idmatch = {$gameId}";
  48.                 } else {
  49.                     $queries[] = "DELETE FROM `match` WHERE idmatch = {$gameId}";
  50.                     ++$missing;
  51.                     if (!$leagueId) {
  52.                         echo "Game.id {$gameId} : Missing League.serviceId {$leagueServiceId}\n";
  53.                     } elseif (!$team1Id) {
  54.                         echo "Game.id {$gameId} : Missing Team1.serviceId {$team1ServiceId}\n";
  55.                     } elseif (!$team2Id) {
  56.                         echo "Game.id {$gameId} : Missing Team2.serviceId {$team2ServiceId}\n";
  57.                     }
  58.                 }
  59.             }
  60.             if (!empty($queries)) {
  61.                 $this->connection->executeQuery(implode(';'$queries));
  62.             }
  63.         }
  64.         echo "Finished. {$missing} Games have been deleted and {$missingGroups} Game.groupId have been set to NULL\n";
  65.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC50588CB7EA5 FOREIGN KEY (idsaison_match) REFERENCES saison (idsaison)');
  66.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC50558AFC4DE FOREIGN KEY (league_id) REFERENCES ligue (idligue)');
  67.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC505E72BCFA4 FOREIGN KEY (team1_id) REFERENCES equipe (idequipe)');
  68.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC505F59E604A FOREIGN KEY (team2_id) REFERENCES equipe (idequipe)');
  69.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC505EAF9CA5F FOREIGN KEY (team_winner_id) REFERENCES equipe (idequipe)');
  70.         $this->addSql('ALTER TABLE `match` ADD CONSTRAINT FK_7A5BC5056959745C FOREIGN KEY (idgroupe_match) REFERENCES groupe (idgroupe)');
  71.         $this->addSql('CREATE INDEX IDX_7A5BC50558AFC4DE ON `match` (league_id)');
  72.         $this->addSql('CREATE INDEX IDX_7A5BC505E72BCFA4 ON `match` (team1_id)');
  73.         $this->addSql('CREATE INDEX IDX_7A5BC505F59E604A ON `match` (team2_id)');
  74.         $this->addSql('CREATE INDEX IDX_7A5BC505EAF9CA5F ON `match` (team_winner_id)');
  75.         $this->addSql('CREATE INDEX IDX_7A5BC5056959745C ON `match` (idgroupe_match)');
  76.     }
  77.     public function down(Schema $schema): void
  78.     {
  79.         // this down() migration is auto-generated, please modify it to your needs
  80.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC50588CB7EA5');
  81.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC50558AFC4DE');
  82.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC505E72BCFA4');
  83.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC505F59E604A');
  84.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC505EAF9CA5F');
  85.         $this->addSql('ALTER TABLE `match` DROP FOREIGN KEY FK_7A5BC5056959745C');
  86.         $this->addSql('DROP INDEX IDX_7A5BC50558AFC4DE ON `match`');
  87.         $this->addSql('DROP INDEX IDX_7A5BC505E72BCFA4 ON `match`');
  88.         $this->addSql('DROP INDEX IDX_7A5BC505F59E604A ON `match`');
  89.         $this->addSql('DROP INDEX IDX_7A5BC505EAF9CA5F ON `match`');
  90.         $this->addSql('DROP INDEX IDX_7A5BC5056959745C ON `match`');
  91.         $this->addSql('ALTER TABLE `match` DROP league_id, DROP team1_id, DROP team2_id, DROP team_winner_id');
  92.     }
  93. }