<?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 Version20220404231854 extends AbstractMigration
{
public function getDescription(): string
{
return 'Fill Player.university + SET Player.university = NULL WHERE Player.university = 0';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$universities = $this->connection->fetchAllAssociative('SELECT iduniversite FROM universite');
$universitiesIds = array_column($universities, 'iduniversite');
$this->connection->executeQuery('UPDATE joueur SET iduniversite_joueur = NULL WHERE iduniversite_joueur = 0');
$lastId = 0;
while (true) {
$players = $this->connection->fetchAllAssociative("SELECT idjoueur, iduniversite_joueur FROM joueur WHERE idjoueur > {$lastId} ORDER BY idjoueur ASC LIMIT 1000");
if (empty($players)) {
break;
}
foreach ($players as $player) {
$playerId = $lastId = $player['idjoueur'];
if (null !== $player['iduniversite_joueur'] && !\in_array($player['iduniversite_joueur'], $universitiesIds)) {
$this->connection->executeQuery("UPDATE joueur SET iduniversite_joueur = NULL WHERE idjoueur = {$playerId}");
}
}
}
$this->addSql('ALTER TABLE joueur ADD CONSTRAINT FK_FD71A9C51169E224 FOREIGN KEY (iduniversite_joueur) REFERENCES universite (iduniversite) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_FD71A9C51169E224 ON joueur (iduniversite_joueur)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE joueur DROP FOREIGN KEY FK_FD71A9C51169E224');
$this->addSql('DROP INDEX IDX_FD71A9C51169E224 ON joueur');
}
}