migrations/Version20260325142749.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 Version20260325142749 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $this->addSql('ALTER TABLE contacts DROP FOREIGN KEY FK_44F066DA72F5A1AA');
  19.         $this->addSql('ALTER TABLE contacts ADD CONSTRAINT FK_44F066DA72F5A1AA_TMP FOREIGN KEY (channel_id) REFERENCES channel_channels (id) ON DELETE CASCADE');
  20.         $this->addSql('CREATE TABLE channel_contacts (channel_id VARCHAR(36) NOT NULL, contact_id VARCHAR(36) NOT NULL, INDEX IDX_44F066DA72F5A1AA (channel_id), INDEX IDX_44F066DAE7A1254A (contact_id), PRIMARY KEY(channel_id, contact_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  21.         $this->addSql('ALTER TABLE channel_contacts ADD CONSTRAINT FK_44F066DA72F5A1AA FOREIGN KEY (channel_id) REFERENCES channel_channels (id) ON DELETE CASCADE');
  22.         $this->addSql('ALTER TABLE channel_contacts ADD CONSTRAINT FK_44F066DAE7A1254A FOREIGN KEY (contact_id) REFERENCES contacts (id) ON DELETE CASCADE');
  23.     }
  24.     public function down(Schema $schema): void
  25.     {
  26.         // this down() migration is auto-generated, please modify it to your needs
  27.         $this->addSql('ALTER TABLE channel_contacts DROP FOREIGN KEY FK_44F066DA72F5A1AA');
  28.         $this->addSql('ALTER TABLE channel_contacts DROP FOREIGN KEY FK_44F066DAE7A1254A');
  29.         $this->addSql('DROP TABLE channel_contacts');
  30.         $this->addSql('ALTER TABLE contacts DROP FOREIGN KEY FK_44F066DA72F5A1AA_TMP');
  31.         $this->addSql('ALTER TABLE contacts ADD CONSTRAINT FK_44F066DA72F5A1AA FOREIGN KEY (channel_id) REFERENCES channel_channels (id) ON DELETE CASCADE');
  32.     }
  33.     public function postUp(Schema $schema): void
  34.     {
  35.         // Check duplicates "email + channel_id"
  36.         $duplicates $this->connection->fetchAllAssociative("
  37.             SELECT email, MIN(id) as keep_id, GROUP_CONCAT(id) as all_ids, GROUP_CONCAT(channel_id) as all_channel_ids
  38.             FROM contacts
  39.             GROUP BY email
  40.             HAVING COUNT(*) > 1
  41.         ");
  42.         foreach ($duplicates as $group) {
  43.             $keepId $group['keep_id'];
  44.             $allIds explode(','$group['all_ids']);
  45.             $allChannelIds explode(','$group['all_channel_ids']);
  46.             $idsToChannelIds array_combine($allIds$allChannelIds);
  47.             $duplicateIds array_filter($allIds, fn($id) => $id !== $keepId);
  48.             foreach ($duplicateIds as $duplicateId) {
  49.                 $duplicateChannelId $idsToChannelIds[$duplicateId];
  50.                 $this->connection->executeStatement(
  51.                     'INSERT IGNORE INTO channel_contacts (channel_id, contact_id) VALUES (?, ?)',
  52.                     [$duplicateChannelId$keepId]
  53.                 );
  54.                 $this->connection->executeStatement(
  55.                     'UPDATE channel_positioning_test_attempts SET prospect_id = ? WHERE prospect_id = ?',
  56.                     [$keepId$duplicateId]
  57.                 );
  58.                 $this->connection->executeStatement(
  59.                     'DELETE FROM contacts WHERE id = ?',
  60.                     [$duplicateId]
  61.                 );
  62.             }
  63.         }
  64.         $contacts $this->connection->fetchAllAssociative('SELECT id, channel_id FROM contacts');
  65.         foreach ($contacts as $contact) {
  66.             $contactId $contact['id'];
  67.             $channelId $contact['channel_id'];
  68.             $this->connection->executeStatement(
  69.                 'INSERT IGNORE INTO channel_contacts (channel_id, contact_id) VALUES (?, ?)',
  70.                 [$channelId$contactId]
  71.             );
  72.         }
  73.     }
  74. }