migrations/Version20230427075207.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. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20230427075207 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // Fetch all customer IDs
  18.         $sql "SELECT id FROM customer";
  19.         $customerIds $this->connection->executeQuery($sql)->fetchAllAssociative();
  20.         // Process each customer
  21.         foreach ($customerIds as $customer) {
  22.             $customerId $customer['id'];
  23.             // Update address_id for customer_quotations
  24.             $this->addSql('
  25.                 UPDATE customer_quotations q
  26.                 JOIN customer c ON q.customer_id = c.id
  27.                 JOIN address a1 ON q.address_id = a1.id
  28.                 JOIN address a2 ON c.address_id = a2.id
  29.                 SET q.address_id = a2.id
  30.                 WHERE
  31.                     c.id = :customer_id AND
  32.                     q.address_id <> c.address_id AND
  33.                     a1.line1 <=> a2.line1 AND
  34.                     a1.line2 <=> a2.line2 AND
  35.                     a1.line3 <=> a2.line3 AND
  36.                     a1.city <=> a2.city AND
  37.                     a1.zip <=> a2.zip AND
  38.                     a1.country <=> a2.country AND
  39.                     a1.details <=> a2.details AND
  40.                     a1.type <=> a2.type
  41.                 ', ['customer_id' => $customerId]);
  42.             // Update address_id for customer_quotations with the same address under each customer
  43.             $this->addSql('
  44.                 UPDATE customer_quotations q1
  45.                 JOIN customer_quotations q2 ON q1.customer_id = q2.customer_id
  46.                 JOIN address a1 ON q1.address_id = a1.id
  47.                 JOIN address a2 ON q2.address_id = a2.id
  48.                 SET q1.address_id = a2.id
  49.                 WHERE
  50.                     q1.customer_id = :customer_id AND
  51.                     q1.id <> q2.id AND
  52.                     q1.address_id <> q2.address_id AND
  53.                     a1.line1 <=> a2.line1 AND
  54.                     a1.line2 <=> a2.line2 AND
  55.                     a1.line3 <=> a2.line3 AND
  56.                     a1.city <=> a2.city AND
  57.                     a1.zip <=> a2.zip AND
  58.                     a1.country <=> a2.country AND
  59.                     a1.details <=> a2.details AND
  60.                     a1.type <=> a2.type
  61.             ', ['customer_id' => $customerId]);
  62.         }
  63.         // Fetch all manufacturer IDs
  64.         $sql "SELECT id FROM manufacturer";
  65.         $manufacturerIds $this->connection->executeQuery($sql)->fetchAllAssociative();
  66.         // Process each manufacturer
  67.         foreach ($manufacturerIds as $manufacturer) {
  68.             $manufacturerId $manufacturer['id'];
  69.             // Update address_id for inquiries with the same address as their manufacturer
  70.             $this->addSql('
  71.             UPDATE price_inquiry p
  72.             JOIN manufacturer m ON p.manufacturer_id = m.id
  73.             JOIN address a1 ON p.address_id = a1.id
  74.             JOIN address a2 ON m.address_id = a2.id
  75.             SET p.address_id = a2.id
  76.             WHERE
  77.                 m.id = :manufacturer_id AND
  78.                 p.address_id <> m.address_id AND
  79.                 a1.line1 <=> a2.line1 AND
  80.                 a1.line2 <=> a2.line2 AND
  81.                 a1.line3 <=> a2.line3 AND
  82.                 a1.city <=> a2.city AND
  83.                 a1.zip <=> a2.zip AND
  84.                 a1.country <=> a2.country AND
  85.                 a1.details <=> a2.details AND
  86.                 a1.type <=> a2.type
  87.             ', ['manufacturer_id' => $manufacturerId]);
  88.             // Update address_id for inquiries with the same address under each manufacturer
  89.             $this->addSql('
  90.                 UPDATE price_inquiry p1
  91.                 JOIN price_inquiry p2 ON p1.manufacturer_id = p2.manufacturer_id
  92.                 JOIN address a1 ON p1.address_id = a1.id
  93.                 JOIN address a2 ON p2.address_id = a2.id
  94.                 SET p1.address_id = a2.id
  95.                 WHERE
  96.                     p1.manufacturer_id = :manufacturer_id AND
  97.                     p1.id <> p2.id AND
  98.                     p1.address_id <> p2.address_id AND
  99.                     a1.line1 <=> a2.line1 AND
  100.                     a1.line2 <=> a2.line2 AND
  101.                     a1.line3 <=> a2.line3 AND
  102.                     a1.city <=> a2.city AND
  103.                     a1.zip <=> a2.zip AND
  104.                     a1.country <=> a2.country AND
  105.                     a1.details <=> a2.details AND
  106.                     a1.type <=> a2.type
  107.             ', ['manufacturer_id' => $manufacturerId]);
  108.         }
  109.     }
  110.     public function down(Schema $schema): void
  111.     {
  112.     }
  113. }