<?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 Version20220426230657 extends AbstractMigration
{
public function getDescription(): string
{
return 'StatCumul';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
ini_set('memory_limit', '-1');
$row0 = $this->connection->fetchAssociative('SELECT * FROM stacumul LIMIT 1');
if (!isset($row0['league_id'])) {
$this->connection->executeQuery('ALTER TABLE stacumul ADD league_id INT DEFAULT NULL, ADD team_id INT DEFAULT NULL, ADD player_id INT DEFAULT NULL');
}
$lastId = 0;
$missing = 0;
$leagues = $teams = $players = $seasons = [];
while (true) {
$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");
if (empty($stats)) {
break;
}
$queries = [];
foreach ($stats as $stat) {
$statId = $lastId = $stat['idstacumul'];
$leagueServiceId = $stat['idligue_stacumul'];
$teamServiceId = $stat['idequipe_stacumul'];
$playerServiceId = $stat['idjoueur_stacumul'];
$seasonIdUnsure = $stat['idsaison_stacumul'];
$leagueId = !$leagueServiceId ? null : ($leagues[$leagueServiceId] ?? ($this->connection->fetchOne("SELECT idligue FROM ligue WHERE idligueservice_ligue = {$leagueServiceId}") ?: null));
$teamId = !$teamServiceId ? null : ($teams[$teamServiceId] ?? ($this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$teamServiceId}") ?: null));
$playerId = !$playerServiceId ? null : ($players[$playerServiceId] ?? ($this->connection->fetchOne("SELECT idjoueur FROM joueur WHERE idjoueurservice_joueur = {$playerServiceId}") ?: null));
$seasonId = !$seasonIdUnsure ? null : ($seasons[$seasonIdUnsure] ?? ($this->connection->fetchOne("SELECT idsaison FROM saison WHERE idsaison = {$seasonIdUnsure}") ?: null));
$queries[] = 'UPDATE stacumul SET league_id = ' . ($leagueId ?? 'NULL') . ', team_id = ' . ($teamId ?? 'NULL') . ', player_id = ' . ($playerId ?? 'NULL') . ', idsaison_stacumul = ' . ($seasonId ?? 'NULL') . " WHERE idstacumul = {$statId}";
$leagues[$leagueServiceId] = $leagueId;
$teams[$teamServiceId] = $teamId;
$players[$playerServiceId] = $playerId;
$seasons[$seasonIdUnsure] = $seasonId;
}
if (!empty($queries)) {
$this->connection->executeQuery(implode(';', $queries));
}
}
$this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D58AFC4DE FOREIGN KEY (league_id) REFERENCES ligue (idligue) ON DELETE CASCADE');
$this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe) ON DELETE CASCADE');
$this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D99E6F5DF FOREIGN KEY (player_id) REFERENCES joueur (idjoueur) ON DELETE CASCADE');
$this->addSql('ALTER TABLE stacumul ADD CONSTRAINT FK_4842C45D66047DF8 FOREIGN KEY (idsaison_stacumul) REFERENCES saison (idsaison) ON DELETE CASCADE');
$this->addSql('CREATE INDEX IDX_4842C45D58AFC4DE ON stacumul (league_id)');
$this->addSql('CREATE INDEX IDX_4842C45D296CD8AE ON stacumul (team_id)');
$this->addSql('CREATE INDEX IDX_4842C45D99E6F5DF ON stacumul (player_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D58AFC4DE');
$this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D296CD8AE');
$this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D99E6F5DF');
$this->addSql('ALTER TABLE stacumul DROP FOREIGN KEY FK_4842C45D66047DF8');
$this->addSql('DROP INDEX IDX_4842C45D58AFC4DE ON stacumul');
$this->addSql('DROP INDEX IDX_4842C45D296CD8AE ON stacumul');
$this->addSql('DROP INDEX IDX_4842C45D99E6F5DF ON stacumul');
$this->addSql('ALTER TABLE stacumul DROP league_id, DROP team_id, DROP player_id');
}
}