<?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 Version20220705071515 extends AbstractMigration
{
public function getDescription(): string
{
return 'AppUserLogs';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$lastId = 0;
while (true) {
$logs = $this->connection->fetchAllAssociative("SELECT * FROM log WHERE idlog > {$lastId} LIMIT 1000");
if (empty($logs)) {
break;
}
foreach ($logs as $log) {
$logId = $lastId = $log['idlog'];
$userId = $log['idutilisateur_log'];
if ($userId) {
$user = $this->connection->fetchOne('SELECT idutilisateur FROM utilisateur WHERE idutilisateur = :id', ['id' => $userId]);
if (!$user) {
$this->connection->executeQuery('UPDATE log SET idutilisateur_log = NULL WHERE idlog = :id', ['id' => $logId]);
echo "Log.id {$logId} : idutilisateur_log SET NULL (old idutilisateur_log: {$userId})\n";
}
}
}
}
$this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C5E36C343F FOREIGN KEY (idutilisateur_log) REFERENCES utilisateur (idutilisateur)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C5E36C343F');
}
}