migrations/Version20241203161225.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. final class Version20241203161225 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Remove duplicate aliments by code, clean up product_aliment and translations';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->addSql("
  15.             CREATE TEMPORARY TABLE tmp_aliment_mapping AS 
  16.             SELECT MIN(a.id) AS retained_id
  17.             FROM aliment a
  18.             GROUP BY a.code
  19.             HAVING COUNT(a.id) > 0;
  20.         ");
  21.         $this->addSql("
  22.             DELETE `at`
  23.             FROM aliment_translation `at`
  24.             JOIN aliment a ON `at`.aliment_id = a.id
  25.             LEFT JOIN tmp_aliment_mapping tam ON a.id = tam.retained_id
  26.             WHERE tam.retained_id IS NULL;
  27.         ");
  28.         $this->addSql("
  29.             DELETE pa
  30.             FROM product_aliment pa
  31.             JOIN aliment a ON pa.aliment_id = a.id
  32.             LEFT JOIN tmp_aliment_mapping tam ON a.id = tam.retained_id
  33.             WHERE tam.retained_id IS NULL;
  34.         ");
  35.         $this->addSql("
  36.             DELETE FROM aliment
  37.             WHERE id NOT IN (SELECT retained_id FROM tmp_aliment_mapping);
  38.         ");
  39.         $this->addSql("
  40.             DROP TEMPORARY TABLE tmp_aliment_mapping;
  41.         ");
  42.     }
  43. }