<?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 Version20220413125627 extends AbstractMigration
{
public function getDescription(): string
{
return 'TeamAka';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->connection->executeQuery('ALTER TABLE aka ADD team_id INT DEFAULT NULL');
$lastId = 0;
while (true) {
$akas = $this->connection->fetchAllAssociative("SELECT idaka, idelement_aka FROM aka WHERE typeelement_aka = 2 AND idaka > {$lastId} LIMIT 1000");
if (empty($akas)) {
break;
}
foreach ($akas as $aka) {
$akaId = $lastId = $aka['idaka'];
$akaElementId = $aka['idelement_aka'];
$teamId = $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$akaElementId}");
if (!$teamId) {
continue;
}
$this->connection->executeQuery("UPDATE aka SET team_id = {$teamId} WHERE idaka = {$akaId}");
}
}
$this->addSql('ALTER TABLE aka ADD CONSTRAINT FK_AE89BA7296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe)');
$this->addSql('CREATE INDEX IDX_AE89BA7296CD8AE ON aka (team_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE aka DROP FOREIGN KEY FK_AE89BA7296CD8AE');
$this->addSql('DROP INDEX IDX_AE89BA7296CD8AE ON aka');
$this->addSql('ALTER TABLE aka DROP team_id');
}
}