addSql(<<<'SQL' DELETE p FROM entity_insurance_pricing p JOIN ( SELECT entity_type, entity_id, MAX(id) AS keep_id FROM entity_insurance_pricing WHERE insurance_id IS NULL GROUP BY entity_type, entity_id HAVING COUNT(*) > 1 ) newest ON newest.entity_type = p.entity_type AND newest.entity_id = p.entity_id WHERE p.insurance_id IS NULL AND p.id <> newest.keep_id SQL); } public function down(Schema $schema): void { // ردیف‌های حذف‌شده تکراری بودند؛ برگرداندنشان همان وضعیت خراب را باز می‌سازد. $this->throwIrreversibleMigrationException('Duplicate pricing rows are not restored.'); } }