migrations/Version20220425225739.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 Version20220425225739 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Bench';
  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 banc ADD player_id INT NOT NULL, ADD game_id INT NOT NULL, ADD team_id INT NOT NULL');
  20.         $lastId 0;
  21.         $missing 0;
  22.         while (true) {
  23.             $benches $this->connection->fetchAllAssociative("SELECT idbanc, idjoueurservice_banc, idmatchservice_banc, idequipeservice_banc FROM banc WHERE idbanc > {$lastId} LIMIT 1000");
  24.             if (empty($benches)) {
  25.                 break;
  26.             }
  27.             foreach ($benches as $bench) {
  28.                 $benchId $lastId $bench['idbanc'];
  29.                 $playerServiceId $bench['idjoueurservice_banc'];
  30.                 $gameServiceId $bench['idmatchservice_banc'];
  31.                 $teamServiceId $bench['idequipeservice_banc'];
  32.                 $playerId $this->connection->fetchOne("SELECT idjoueur FROM joueur WHERE idjoueurservice_joueur = {$playerServiceId}");
  33.                 $gameId $this->connection->fetchOne("SELECT idmatch FROM `match` WHERE idmatchservice_match = {$gameServiceId}");
  34.                 $teamId $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$teamServiceId}");
  35.                 if ($playerId && $gameId && $teamId) {
  36.                     $this->connection->executeQuery("UPDATE banc SET player_id = {$playerId}, game_id = {$gameId}, team_id = {$teamId} WHERE idbanc = {$benchId}");
  37.                 } else {
  38.                     $this->connection->executeQuery("DELETE FROM banc WHERE idbanc = {$benchId}");
  39.                     ++$missing;
  40.                     if (!$playerId) {
  41.                         echo "Bench.id {$benchId} : Missing Player.serviceId {$playerServiceId}\n";
  42.                     } elseif (!$gameId) {
  43.                         echo "Bench.id {$benchId} : Missing Game.serviceId {$gameServiceId}\n";
  44.                     } elseif (!$teamId) {
  45.                         echo "Bench.id {$benchId} : Missing Team.serviceId {$teamServiceId}\n";
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.         echo "Finished. {$missing} Benches have been deleted\n";
  51.         $this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB374899E6F5DF FOREIGN KEY (player_id) REFERENCES joueur (idjoueur)');
  52.         $this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB3748E48FD905 FOREIGN KEY (game_id) REFERENCES `match` (idmatch)');
  53.         $this->addSql('ALTER TABLE banc ADD CONSTRAINT FK_D6BB3748296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe)');
  54.         $this->addSql('CREATE INDEX IDX_D6BB374899E6F5DF ON banc (player_id)');
  55.         $this->addSql('CREATE INDEX IDX_D6BB3748E48FD905 ON banc (game_id)');
  56.         $this->addSql('CREATE INDEX IDX_D6BB3748296CD8AE ON banc (team_id)');
  57.     }
  58.     public function down(Schema $schema): void
  59.     {
  60.         // this down() migration is auto-generated, please modify it to your needs
  61.         $this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB374899E6F5DF');
  62.         $this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB3748E48FD905');
  63.         $this->addSql('ALTER TABLE banc DROP FOREIGN KEY FK_D6BB3748296CD8AE');
  64.         $this->addSql('DROP INDEX IDX_D6BB374899E6F5DF ON banc');
  65.         $this->addSql('DROP INDEX IDX_D6BB3748E48FD905 ON banc');
  66.         $this->addSql('DROP INDEX IDX_D6BB3748296CD8AE ON banc');
  67.         $this->addSql('ALTER TABLE banc DROP player_id, DROP game_id, DROP team_id');
  68.     }
  69. }