<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20241203161225 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove duplicate aliments by code, clean up product_aliment and translations';
}
public function up(Schema $schema): void
{
$this->addSql("
CREATE TEMPORARY TABLE tmp_aliment_mapping AS
SELECT MIN(a.id) AS retained_id
FROM aliment a
GROUP BY a.code
HAVING COUNT(a.id) > 0;
");
$this->addSql("
DELETE `at`
FROM aliment_translation `at`
JOIN aliment a ON `at`.aliment_id = a.id
LEFT JOIN tmp_aliment_mapping tam ON a.id = tam.retained_id
WHERE tam.retained_id IS NULL;
");
$this->addSql("
DELETE pa
FROM product_aliment pa
JOIN aliment a ON pa.aliment_id = a.id
LEFT JOIN tmp_aliment_mapping tam ON a.id = tam.retained_id
WHERE tam.retained_id IS NULL;
");
$this->addSql("
DELETE FROM aliment
WHERE id NOT IN (SELECT retained_id FROM tmp_aliment_mapping);
");
$this->addSql("
DROP TEMPORARY TABLE tmp_aliment_mapping;
");
}
}