migrations/Version20221024100548.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. use function Deployer\which;
  7. /**
  8.  * Auto-generated Migration: Please modify to your needs!
  9.  */
  10. final class Version20221024100548 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return '
  15.             Permet de renommer les matricules des etablissements dupliqués. Le nouveau nom est sous le format
  16.             ancienNom-d-n avec n = random(0, 100)
  17.         ';
  18.     }
  19.     public function up(Schema $schema): void
  20.     {
  21.         // this up() migration is auto-generated, please modify it to your needs
  22.         $duplicateNames $this->connection->executeQuery(
  23.             'SELECT COUNT(s.id) as nb, s.registration as school_registration
  24.                 FROM school s
  25.                 group by school_registration
  26.                 having nb > 1'
  27.         )->fetchAllAssociative();
  28.         foreach ($duplicateNames as $item) {
  29.             $registration $item['school_registration'];
  30.             $ids $this->connection->executeQuery('SELECT s2.id FROM school s2 WHERE s2.registration = ?', [$registration])
  31.                 ->fetchAllAssociative();
  32.             foreach ($ids as $id) {
  33.                 $this->addSql(
  34.                     'UPDATE school s1 SET s1.registration = ? WHERE s1.id = ?',
  35.                     [
  36.                         $registration '-d-' rand(0100),
  37.                         $id['id']
  38.                     ]
  39.                 );
  40.             }
  41.         }
  42.     }
  43.     public function down(Schema $schema): void
  44.     {
  45.         // this down() migration is auto-generated, please modify it to your needs
  46.     }
  47. }