Files
mautic-marketing-program-bu…/Migrations/Version_0_4_0.php
T
Alexandre Gain 8268fad886 feat: Rework task management in PlanBundle
- Introduced migration Version_0_13_0 to update task schema:
  - Changed start_date and end_date to DATE type, truncating time.
  - Added new columns: task_kind, definition_of_done, planned_work.
  - Removed parent_id and its index, replacing with a dependency graph.
  - Created new tables: plan_task_dependencies and plan_task_raci, including a join table for stakeholders.

- Updated Version_0_4_0 migration to remove references to parent_id.

- Refactored TaskModel to return tasks as flat rows with dependencies and RACI assignments.

- Modified task table view to display tasks without hierarchical structure, adding badges for milestones and phases.

- Enhanced task form to include dependencies and RACI roles, with appropriate macros for rendering.

- Updated task list view to reflect new task structure and display status based on task kind.

- Added new validation constraints for task dependencies and dates, ensuring no cycles or self-references.

- Removed obsolete ValidTaskParent validation as parent-child relationships are no longer supported.

- Updated translations to reflect changes in task management and new fields.

- Revised documentation to describe new task handling and features.
2026-11-13 21:58:45 +01:00

103 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\Task;
/**
* Adds the plan_tasks table and its foreign keys to installs created before 0.4.0.
*
* The CREATE TABLE comes from the Task mapping, so it can't drift from the entity. Only that statement
* is kept: SchemaTool's output also contains statements for other tables (for example Symfony
* Messenger's messenger_messages), which would fail on an existing install.
*
* The foreign keys are added one by one when missing, so the migration also repairs a plan_tasks table
* that exists without them. Fresh installs get everything from the entity mapping, so this migration
* then finds nothing to do.
*/
class Version_0_4_0 extends AbstractMigration
{
/**
* Local column => [referenced table, ON DELETE action].
*
* parent_id is gone since 0.13.0 (the parent/child tree was replaced by the dependency graph),
* so only plan_id is repaired here.
*/
private const FOREIGN_KEYS = [
'plan_id' => [Plan::TABLE_NAME, 'CASCADE'],
];
protected function isApplicable(Schema $schema): bool
{
if (!$schema->hasTable($this->concatPrefix(Plan::TABLE_NAME))) {
return false;
}
$tasks = $this->concatPrefix(Task::TABLE_NAME);
$table = $schema->hasTable($tasks) ? $schema->getTable($tasks) : null;
return null === $table || [] !== $this->getMissingForeignKeys($table);
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$tasks = $this->concatPrefix(Task::TABLE_NAME);
$missingFk = self::FOREIGN_KEYS;
if (!$schema->hasTable($tasks)) {
$this->addSql($this->getCreateTableSql());
} else {
$missingFk = $this->getMissingForeignKeys($schema->getTable($tasks));
}
foreach ($missingFk as $column => [$referenceTable, $onDelete]) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
Task::TABLE_NAME,
[$column],
$referenceTable,
['id'],
'ON DELETE '.$onDelete
));
}
}
private function getCreateTableSql(): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix(Task::TABLE_NAME).' ';
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(Task::class)]) as $sql) {
if (str_starts_with($sql, $prefix)) {
return $sql;
}
}
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.Task::TABLE_NAME.'.');
}
/**
* @return array<string, array{0: string, 1: string}>
*/
private function getMissingForeignKeys(Table $table): array
{
$missing = self::FOREIGN_KEYS;
foreach ($table->getForeignKeys() as $foreignKey) {
foreach ($foreignKey->getLocalColumns() as $column) {
unset($missing[$column]);
}
}
return $missing;
}
}