<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230427075207 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// Fetch all customer IDs
$sql = "SELECT id FROM customer";
$customerIds = $this->connection->executeQuery($sql)->fetchAllAssociative();
// Process each customer
foreach ($customerIds as $customer) {
$customerId = $customer['id'];
// Update address_id for customer_quotations
$this->addSql('
UPDATE customer_quotations q
JOIN customer c ON q.customer_id = c.id
JOIN address a1 ON q.address_id = a1.id
JOIN address a2 ON c.address_id = a2.id
SET q.address_id = a2.id
WHERE
c.id = :customer_id AND
q.address_id <> c.address_id AND
a1.line1 <=> a2.line1 AND
a1.line2 <=> a2.line2 AND
a1.line3 <=> a2.line3 AND
a1.city <=> a2.city AND
a1.zip <=> a2.zip AND
a1.country <=> a2.country AND
a1.details <=> a2.details AND
a1.type <=> a2.type
', ['customer_id' => $customerId]);
// Update address_id for customer_quotations with the same address under each customer
$this->addSql('
UPDATE customer_quotations q1
JOIN customer_quotations q2 ON q1.customer_id = q2.customer_id
JOIN address a1 ON q1.address_id = a1.id
JOIN address a2 ON q2.address_id = a2.id
SET q1.address_id = a2.id
WHERE
q1.customer_id = :customer_id AND
q1.id <> q2.id AND
q1.address_id <> q2.address_id AND
a1.line1 <=> a2.line1 AND
a1.line2 <=> a2.line2 AND
a1.line3 <=> a2.line3 AND
a1.city <=> a2.city AND
a1.zip <=> a2.zip AND
a1.country <=> a2.country AND
a1.details <=> a2.details AND
a1.type <=> a2.type
', ['customer_id' => $customerId]);
}
// Fetch all manufacturer IDs
$sql = "SELECT id FROM manufacturer";
$manufacturerIds = $this->connection->executeQuery($sql)->fetchAllAssociative();
// Process each manufacturer
foreach ($manufacturerIds as $manufacturer) {
$manufacturerId = $manufacturer['id'];
// Update address_id for inquiries with the same address as their manufacturer
$this->addSql('
UPDATE price_inquiry p
JOIN manufacturer m ON p.manufacturer_id = m.id
JOIN address a1 ON p.address_id = a1.id
JOIN address a2 ON m.address_id = a2.id
SET p.address_id = a2.id
WHERE
m.id = :manufacturer_id AND
p.address_id <> m.address_id AND
a1.line1 <=> a2.line1 AND
a1.line2 <=> a2.line2 AND
a1.line3 <=> a2.line3 AND
a1.city <=> a2.city AND
a1.zip <=> a2.zip AND
a1.country <=> a2.country AND
a1.details <=> a2.details AND
a1.type <=> a2.type
', ['manufacturer_id' => $manufacturerId]);
// Update address_id for inquiries with the same address under each manufacturer
$this->addSql('
UPDATE price_inquiry p1
JOIN price_inquiry p2 ON p1.manufacturer_id = p2.manufacturer_id
JOIN address a1 ON p1.address_id = a1.id
JOIN address a2 ON p2.address_id = a2.id
SET p1.address_id = a2.id
WHERE
p1.manufacturer_id = :manufacturer_id AND
p1.id <> p2.id AND
p1.address_id <> p2.address_id AND
a1.line1 <=> a2.line1 AND
a1.line2 <=> a2.line2 AND
a1.line3 <=> a2.line3 AND
a1.city <=> a2.city AND
a1.zip <=> a2.zip AND
a1.country <=> a2.country AND
a1.details <=> a2.details AND
a1.type <=> a2.type
', ['manufacturer_id' => $manufacturerId]);
}
}
public function down(Schema $schema): void
{
}
}