<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20241203142646 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove duplicate taxes by rate and update related ProductShopMode references to point to the retained Tax entry with the same rate.';
}
public function up(Schema $schema): void
{
$this->addSql("
UPDATE product_shop_mode psm
JOIN tax t1 ON psm.tax_id = t1.id
JOIN (
SELECT MIN(id) AS id, rate
FROM tax
GROUP BY rate
) t2 ON t1.rate = t2.rate
SET psm.tax_id = t2.id
");
$this->addSql("
DELETE FROM tax
WHERE id NOT IN (
SELECT id FROM (
SELECT MIN(id) AS id
FROM tax
GROUP BY rate
) AS retained
)
");
}
}