migrations/Version20260721120000.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. final class Version20260721120000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Make steps reusable between products and add the template flag';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->addSql('ALTER TABLE step ADD template TINYINT(1) DEFAULT 0 NOT NULL');
  15.         $this->addSql('CREATE TABLE product_step (product_id INT NOT NULL, step_id INT NOT NULL, INDEX IDX_CE51DC9D4584665A (product_id), INDEX IDX_CE51DC9D73B21E9C (step_id), PRIMARY KEY(product_id, step_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  16.         $this->addSql('ALTER TABLE product_step ADD CONSTRAINT FK_CE51DC9D4584665A FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE');
  17.         $this->addSql('ALTER TABLE product_step ADD CONSTRAINT FK_CE51DC9D73B21E9C FOREIGN KEY (step_id) REFERENCES step (id) ON DELETE CASCADE');
  18.         $this->addSql('INSERT INTO product_step (product_id, step_id) SELECT product_id, id FROM step');
  19.         $this->addSql('ALTER TABLE step DROP FOREIGN KEY FK_43B9FE3C4584665A');
  20.         $this->addSql('ALTER TABLE step DROP product_id');
  21.     }
  22.     public function down(Schema $schema): void
  23.     {
  24.         $this->addSql('ALTER TABLE step ADD product_id INT DEFAULT NULL');
  25.         $this->addSql('
  26.             UPDATE step s
  27.             INNER JOIN (
  28.                 SELECT step_id, MIN(product_id) AS product_id
  29.                 FROM product_step
  30.                 GROUP BY step_id
  31.             ) product_step ON product_step.step_id = s.id
  32.             SET s.product_id = product_step.product_id
  33.         ');
  34.         $this->addSql('DROP TABLE product_step');
  35.         $this->addSql('ALTER TABLE step CHANGE product_id product_id INT NOT NULL, DROP template');
  36.         $this->addSql('CREATE INDEX IDX_43B9FE3C4584665A ON step (product_id)');
  37.         $this->addSql('ALTER TABLE step ADD CONSTRAINT FK_43B9FE3C4584665A FOREIGN KEY (product_id) REFERENCES product (id)');
  38.     }
  39. }