From f6a947ec1575547d51859d40ced1bdc6b8d6f944 Mon Sep 17 00:00:00 2001 From: matthieu Date: Tue, 19 May 2026 19:51:36 +0200 Subject: [PATCH] =?UTF-8?q?feat(workflow)=20:=20migration=20M2=20-=20ratta?= =?UTF-8?q?che=20les=20statuts=20existants=20=C3=A0=20Standard=20+=20categ?= =?UTF-8?q?ory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migrations/Version20260519175114.php | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 migrations/Version20260519175114.php diff --git a/migrations/Version20260519175114.php b/migrations/Version20260519175114.php new file mode 100644 index 0000000..29046bb --- /dev/null +++ b/migrations/Version20260519175114.php @@ -0,0 +1,74 @@ +connection->fetchOne("SELECT id FROM workflow WHERE name = 'Standard'"); + if (!$standardId) { + throw new MigrationException('Workflow Standard introuvable. Lancer M1 d\'abord.'); + } + + // 2) Garde-fou : vérifier qu'il n'y a pas de label hors mapping + $mapping = [ + 'A faire' => 'todo', + 'À faire' => 'todo', + 'En cours' => 'in_progress', + 'Bloqué' => 'blocked', + 'En attente de validation' => 'review', + 'Terminé' => 'done', + ]; + $rows = $this->connection->fetchAllAssociative('SELECT id, label FROM task_status'); + foreach ($rows as $row) { + if (!isset($mapping[$row['label']])) { + throw new MigrationException(sprintf( + 'TaskStatus #%d ("%s") n\'est pas mappable. Ajoutez son mapping dans la migration avant de relancer.', + $row['id'], + $row['label'], + )); + } + } + + // 3) Ajouter colonnes nullable + $this->addSql('ALTER TABLE task_status ADD COLUMN workflow_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE task_status ADD COLUMN category VARCHAR(32) DEFAULT NULL'); + + // 4) Backfill + $this->addSql("UPDATE task_status SET workflow_id = {$standardId}"); + foreach ($mapping as $label => $cat) { + $this->addSql(sprintf( + "UPDATE task_status SET category = '%s' WHERE label = '%s'", + $cat, + str_replace("'", "''", $label), + )); + } + + // 5) NOT NULL + FK + $this->addSql('ALTER TABLE task_status ALTER COLUMN workflow_id SET NOT NULL'); + $this->addSql('ALTER TABLE task_status ALTER COLUMN category SET NOT NULL'); + $this->addSql('ALTER TABLE task_status ADD CONSTRAINT FK_task_status_workflow FOREIGN KEY (workflow_id) REFERENCES workflow (id) ON DELETE CASCADE NOT DEFERRABLE'); + $this->addSql('CREATE INDEX IDX_task_status_workflow ON task_status (workflow_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE task_status DROP CONSTRAINT FK_task_status_workflow'); + $this->addSql('DROP INDEX IDX_task_status_workflow'); + $this->addSql('ALTER TABLE task_status DROP COLUMN workflow_id'); + $this->addSql('ALTER TABLE task_status DROP COLUMN category'); + } +}