migrations/Version20220420180223.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 Version20220420180223 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'StatDay';
  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("UPDATE stajournee SET date_stajournee = NULL WHERE `date_stajournee` LIKE '%0000-00-00%'");
  20.         $row0 $this->connection->fetchAssociative('SELECT * FROM stajournee LIMIT 1');
  21.         if (!isset($row0['player_id'])) {
  22.             $this->connection->executeQuery('ALTER TABLE stajournee ADD player_id INT NOT NULL, ADD league_id INT NOT NULL, ADD team_id INT NOT NULL, ADD game_id INT NOT NULL');
  23.         }
  24.         $lastId 0;
  25.         $missing 0;
  26.         $leagues $teams $games $players $seasons = [];
  27.         while (true) {
  28.             $stats $this->connection->fetchAllAssociative("SELECT idstajournee, idjoueur_stajournee, idsaison_stajournee, idligue_stajournee, idequipe_stajournee, idmatchservice_stajournee FROM stajournee WHERE idstajournee > {$lastId} AND (player_id IS NULL OR player_id = 0) LIMIT 1000");
  29.             if (empty($stats)) {
  30.                 break;
  31.             }
  32.             $queries = [];
  33.             foreach ($stats as $stat) {
  34.                 $statId $lastId $stat['idstajournee'];
  35.                 $leagueServiceId $stat['idligue_stajournee'];
  36.                 $teamServiceId $stat['idequipe_stajournee'];
  37.                 $gameServiceId $stat['idmatchservice_stajournee'];
  38.                 $playerServiceId $stat['idjoueur_stajournee'];
  39.                 $seasonIdUnsure $stat['idsaison_stajournee'];
  40.                 $leagueId $leagues[$leagueServiceId] ?? $this->connection->fetchOne("SELECT idligue FROM ligue WHERE idligueservice_ligue = {$leagueServiceId}");
  41.                 $teamId $teams[$teamServiceId] ?? $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$teamServiceId}");
  42.                 $gameId $games[$gameServiceId] ?? $this->connection->fetchOne("SELECT idmatch FROM `match` WHERE idmatchservice_match = {$gameServiceId}");
  43.                 $playerId $players[$playerServiceId] ?? $this->connection->fetchOne("SELECT idjoueur FROM joueur WHERE idjoueurservice_joueur = {$playerServiceId}");
  44.                 $seasonId $seasons[$seasonIdUnsure] ?? $this->connection->fetchOne("SELECT idsaison FROM saison WHERE idsaison = {$seasonIdUnsure}");
  45.                 if ($leagueId && $teamId && $gameId && $playerId && $seasonId) {
  46.                     $queries[] = "UPDATE stajournee SET league_id = {$leagueId}, team_id = {$teamId}, game_id = {$gameId}, player_id = {$playerId} WHERE idstajournee = {$statId}";
  47.                     $leagues[$leagueServiceId] = $leagueId;
  48.                     $teams[$teamServiceId] = $teamId;
  49.                     $games[$gameServiceId] = $gameId;
  50.                     $players[$playerServiceId] = $playerId;
  51.                     $seasons[$seasonIdUnsure] = $seasonId;
  52.                 } else {
  53.                     $queries[] = "DELETE FROM stajournee WHERE idstajournee = {$statId}";
  54.                     ++$missing;
  55.                     if (!$leagueId) {
  56.                         echo "StatDay.id {$statId} : Missing League.serviceId {$leagueServiceId}\n";
  57.                     } elseif (!$teamId) {
  58.                         echo "StatDay.id {$statId} : Missing Team.serviceId {$teamServiceId}\n";
  59.                     } elseif (!$gameId) {
  60.                         echo "StatDay.id {$statId} : Missing Game.serviceId {$gameServiceId}\n";
  61.                     } elseif (!$playerId) {
  62.                         echo "StatDay.id {$statId} : Missing Player.serviceId {$playerServiceId}\n";
  63.                     } elseif (!$seasonId) {
  64.                         echo "StatDay.id {$statId} : Missing Season.id {$seasonId}\n";
  65.                     }
  66.                 }
  67.             }
  68.             if (!empty($queries)) {
  69.                 $this->connection->executeQuery(implode(';'$queries));
  70.             }
  71.             echo "OK - {$statId}\n";
  72.         }
  73.         $this->addSql('ALTER TABLE stajournee ADD CONSTRAINT FK_E81218299E6F5DF FOREIGN KEY (player_id) REFERENCES joueur (idjoueur)');
  74.         $this->addSql('ALTER TABLE stajournee ADD CONSTRAINT FK_E8121826F4C4C40 FOREIGN KEY (idsaison_stajournee) REFERENCES saison (idsaison)');
  75.         $this->addSql('ALTER TABLE stajournee ADD CONSTRAINT FK_E81218258AFC4DE FOREIGN KEY (league_id) REFERENCES ligue (idligue)');
  76.         $this->addSql('ALTER TABLE stajournee ADD CONSTRAINT FK_E812182296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe)');
  77.         $this->addSql('ALTER TABLE stajournee ADD CONSTRAINT FK_E812182E48FD905 FOREIGN KEY (game_id) REFERENCES `match` (idmatch)');
  78.         $this->addSql('CREATE INDEX IDX_E81218299E6F5DF ON stajournee (player_id)');
  79.         $this->addSql('CREATE INDEX IDX_E81218258AFC4DE ON stajournee (league_id)');
  80.         $this->addSql('CREATE INDEX IDX_E812182296CD8AE ON stajournee (team_id)');
  81.         $this->addSql('CREATE INDEX IDX_E812182E48FD905 ON stajournee (game_id)');
  82.     }
  83.     public function down(Schema $schema): void
  84.     {
  85.         // this down() migration is auto-generated, please modify it to your needs
  86.         $this->addSql('ALTER TABLE stajournee DROP FOREIGN KEY FK_E81218299E6F5DF');
  87.         $this->addSql('ALTER TABLE stajournee DROP FOREIGN KEY FK_E8121826F4C4C40');
  88.         $this->addSql('ALTER TABLE stajournee DROP FOREIGN KEY FK_E81218258AFC4DE');
  89.         $this->addSql('ALTER TABLE stajournee DROP FOREIGN KEY FK_E812182296CD8AE');
  90.         $this->addSql('ALTER TABLE stajournee DROP FOREIGN KEY FK_E812182E48FD905');
  91.         $this->addSql('DROP INDEX IDX_E81218299E6F5DF ON stajournee');
  92.         $this->addSql('DROP INDEX IDX_E81218258AFC4DE ON stajournee');
  93.         $this->addSql('DROP INDEX IDX_E812182296CD8AE ON stajournee');
  94.         $this->addSql('DROP INDEX IDX_E812182E48FD905 ON stajournee');
  95.         $this->addSql('ALTER TABLE stajournee DROP player_id, DROP league_id, DROP team_id, DROP game_id');
  96.     }
  97. }