migrations/Version20230220121817.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. final class Version20230220121817 extends AbstractMigration
  8. {
  9.     public function up(Schema $schema): void
  10.     {
  11.         $this->addSql('CREATE TABLE service_campaign_message (id INT AUTO_INCREMENT NOT NULL, message_id INT NOT NULL, service_campaign_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', deleted_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', UNIQUE INDEX UNIQ_B9428B42537A1329 (message_id), INDEX IDX_B9428B423AA3D1D8 (service_campaign_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  12.         $this->addSql('ALTER TABLE service_campaign_message ADD CONSTRAINT FK_B9428B42537A1329 FOREIGN KEY (message_id) REFERENCES message (id)');
  13.         $this->addSql('ALTER TABLE service_campaign_message ADD CONSTRAINT FK_B9428B423AA3D1D8 FOREIGN KEY (service_campaign_id) REFERENCES service_campaign (id)');
  14.         $serviceCampaigns $this->getData('SELECT id, message_id FROM service_campaign');
  15.         foreach ($serviceCampaigns as $serviceCampaign) {
  16.             $now = new \DateTimeImmutable();
  17.             $this->addSql('INSERT INTO service_campaign_message (message_id, service_campaign_id, created_at, updated_at) VALUES (?,?,?,?)', [
  18.                 $serviceCampaign['message_id'],
  19.                 $serviceCampaign['id'],
  20.                 $now->format('Y-m-d H:i:s'),
  21.                 $now->format('Y-m-d H:i:s'),
  22.             ]);
  23.         }
  24.         $this->addSql('ALTER TABLE service_campaign DROP FOREIGN KEY FK_B176F1DD537A1329');
  25.         $this->addSql('ALTER TABLE service_campaign DROP message_id');
  26.     }
  27.     public function down(Schema $schema): void
  28.     {
  29.         $serviceCampaignMessages $this->getData('SELECT message_id, service_campaign_id FROM service_campaign_message');
  30.         $this->addSql('ALTER TABLE service_campaign_message DROP FOREIGN KEY FK_B9428B42537A1329');
  31.         $this->addSql('ALTER TABLE service_campaign_message DROP FOREIGN KEY FK_B9428B423AA3D1D8');
  32.         $this->addSql('DROP TABLE service_campaign_message');
  33.         $this->addSql('ALTER TABLE service_campaign ADD message_id INT NOT NULL');
  34.         foreach ($serviceCampaignMessages as $serviceCampaignMessage) {
  35.             $this->addSql(
  36.                 'UPDATE service_campaign SET message_id = :messageId WHERE id = :serviceCampaignId',
  37.                 [
  38.                     'messageId' => $serviceCampaignMessage['message_id'],
  39.                     'serviceCampaignId' => $serviceCampaignMessage['service_campaign_id'],
  40.                 ]
  41.             );
  42.         }
  43.     }
  44.     /**
  45.      * @throws Exception
  46.      */
  47.     private function getData(string $sql): \Traversable
  48.     {
  49.         return $this->connection->executeQuery($sql)->iterateAssociative();
  50.     }
  51. }