<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260721120000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Make steps reusable between products and add the template flag';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE step ADD template TINYINT(1) DEFAULT 0 NOT NULL');
$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');
$this->addSql('ALTER TABLE product_step ADD CONSTRAINT FK_CE51DC9D4584665A FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE product_step ADD CONSTRAINT FK_CE51DC9D73B21E9C FOREIGN KEY (step_id) REFERENCES step (id) ON DELETE CASCADE');
$this->addSql('INSERT INTO product_step (product_id, step_id) SELECT product_id, id FROM step');
$this->addSql('ALTER TABLE step DROP FOREIGN KEY FK_43B9FE3C4584665A');
$this->addSql('ALTER TABLE step DROP product_id');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE step ADD product_id INT DEFAULT NULL');
$this->addSql('
UPDATE step s
INNER JOIN (
SELECT step_id, MIN(product_id) AS product_id
FROM product_step
GROUP BY step_id
) product_step ON product_step.step_id = s.id
SET s.product_id = product_step.product_id
');
$this->addSql('DROP TABLE product_step');
$this->addSql('ALTER TABLE step CHANGE product_id product_id INT NOT NULL, DROP template');
$this->addSql('CREATE INDEX IDX_43B9FE3C4584665A ON step (product_id)');
$this->addSql('ALTER TABLE step ADD CONSTRAINT FK_43B9FE3C4584665A FOREIGN KEY (product_id) REFERENCES product (id)');
}
}