migrations/Version20241203142646.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 Version20241203142646 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Remove duplicate taxes by rate and update related ProductShopMode references to point to the retained Tax entry with the same rate.';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->addSql("
  15.             UPDATE product_shop_mode psm
  16.             JOIN tax t1 ON psm.tax_id = t1.id
  17.             JOIN (
  18.                 SELECT MIN(id) AS id, rate
  19.                 FROM tax
  20.                 GROUP BY rate
  21.             ) t2 ON t1.rate = t2.rate
  22.             SET psm.tax_id = t2.id
  23.         ");
  24.         $this->addSql("
  25.             DELETE FROM tax
  26.             WHERE id NOT IN (
  27.                 SELECT id FROM (
  28.                     SELECT MIN(id) AS id
  29.                     FROM tax
  30.                     GROUP BY rate
  31.                 ) AS retained
  32.             )
  33.         ");
  34.     }
  35. }