migrations/Version20220426230657.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 Version20220426230657 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'StatCumul';
  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.         $row0 $this->connection->fetchAssociative('SELECT * FROM stacumul LIMIT 1');
  20.         if (!isset($row0['league_id'])) {
  21.             $this->connection->executeQuery('ALTER TABLE stacumul ADD league_id INT DEFAULT NULL, ADD team_id INT DEFAULT NULL, ADD player_id INT DEFAULT NULL');
  22.         }
  23.         $lastId 0;
  24.         $missing 0;
  25.         $leagues $teams $players $seasons = [];
  26.         while (true) {
  27.             $stats $this->connection->fetchAllAssociative("SELECT idstacumul, idligue_stacumul, idequipe_stacumul, idjoueur_stacumul, idsaison_stacumul FROM stacumul WHERE idstacumul > {$lastId} AND (league_id IS NULL OR team_id IS NULL OR player_id IS NULL) LIMIT 1000");
  28.             if (empty($stats)) {
  29.                 break;
  30.             }
  31.             $queries = [];
  32.             foreach ($stats as $stat) {
  33.                 $statId $lastId $stat['idstacumul'];
  34.                 $leagueServiceId $stat['idligue_stacumul'];
  35.                 $teamServiceId $stat['idequipe_stacumul'];
  36.                 $playerServiceId $stat['idjoueur_stacumul'];
  37.                 $seasonIdUnsure $stat['idsaison_stacumul'];
  38.                 $leagueId = !$leagueServiceId null : ($leagues[$leagueServiceId] ?? ($this->connection->fetchOne("SELECT idligue FROM ligue WHERE idligueservice_ligue = {$leagueServiceId}") ?: null));
  39.                 $teamId = !$teamServiceId null : ($teams[$teamServiceId] ?? ($this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$teamServiceId}") ?: null));
  40.                 $playerId = !$playerServiceId null : ($players[$playerServiceId] ?? ($this->connection->fetchOne("SELECT idjoueur FROM joueur WHERE idjoueurservice_joueur = {$playerServiceId}") ?: null));
  41.                 $seasonId = !$seasonIdUnsure null : ($seasons[$seasonIdUnsure] ?? ($this->connection->fetchOne("SELECT idsaison FROM saison WHERE idsaison = {$seasonIdUnsure}") ?: null));
  42.                 $queries[] = 'UPDATE stacumul SET league_id = ' . ($leagueId ?? 'NULL') . ', team_id = ' . ($teamId ?? 'NULL') . ', player_id = ' . ($playerId ?? 'NULL') . ', idsaison_stacumul = ' . ($seasonId ?? 'NULL') . " WHERE idstacumul = {$statId}";
  43.                 $leagues[$leagueServiceId] = $leagueId;
  44.                 $teams[$teamServiceId] = $teamId;
  45.                 $players[$playerServiceId] = $playerId;
  46.                 $seasons[$seasonIdUnsure] = $seasonId;
  47.             }
  48.             if (!empty($queries)) {
  49.                 $this->connection->executeQuery(implode(';'$queries));
  50.             }
  51.         }
  52.         $this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D58AFC4DE FOREIGN KEY (league_id) REFERENCES ligue (idligue) ON DELETE CASCADE');
  53.         $this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe) ON DELETE CASCADE');
  54.         $this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D99E6F5DF FOREIGN KEY (player_id) REFERENCES joueur (idjoueur) ON DELETE CASCADE');
  55.         $this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D66047DF8 FOREIGN KEY (idsaison_stacumul) REFERENCES saison (idsaison) ON DELETE CASCADE');
  56.         $this->addSql('CREATE INDEX IDX_4842C45D58AFC4DE ON stacumul (league_id)');
  57.         $this->addSql('CREATE INDEX IDX_4842C45D296CD8AE ON stacumul (team_id)');
  58.         $this->addSql('CREATE INDEX IDX_4842C45D99E6F5DF ON stacumul (player_id)');
  59.     }
  60.     public function down(Schema $schema): void
  61.     {
  62.         // this down() migration is auto-generated, please modify it to your needs
  63.         $this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D58AFC4DE');
  64.         $this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D296CD8AE');
  65.         $this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D99E6F5DF');
  66.         $this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D66047DF8');
  67.         $this->addSql('DROP INDEX IDX_4842C45D58AFC4DE ON stacumul');
  68.         $this->addSql('DROP INDEX IDX_4842C45D296CD8AE ON stacumul');
  69.         $this->addSql('DROP INDEX IDX_4842C45D99E6F5DF ON stacumul');
  70.         $this->addSql('ALTER TABLE stacumul DROP league_id, DROP team_id, DROP player_id');
  71.     }
  72. }