migrations/Version20220705071515.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 Version20220705071515 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'AppUserLogs';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $lastId 0;
  19.         while (true) {
  20.             $logs $this->connection->fetchAllAssociative("SELECT * FROM log WHERE idlog > {$lastId} LIMIT 1000");
  21.             if (empty($logs)) {
  22.                 break;
  23.             }
  24.             foreach ($logs as $log) {
  25.                 $logId $lastId $log['idlog'];
  26.                 $userId $log['idutilisateur_log'];
  27.                 if ($userId) {
  28.                     $user $this->connection->fetchOne('SELECT idutilisateur FROM utilisateur WHERE idutilisateur = :id', ['id' => $userId]);
  29.                     if (!$user) {
  30.                         $this->connection->executeQuery('UPDATE log SET idutilisateur_log = NULL WHERE idlog = :id', ['id' => $logId]);
  31.                         echo "Log.id {$logId} : idutilisateur_log SET NULL (old idutilisateur_log: {$userId})\n";
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         $this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C5E36C343F FOREIGN KEY (idutilisateur_log) REFERENCES utilisateur (idutilisateur)');
  37.     }
  38.     public function down(Schema $schema): void
  39.     {
  40.         // this down() migration is auto-generated, please modify it to your needs
  41.         $this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C5E36C343F');
  42.     }
  43. }