Files
mautic-marketing-program-bu…/Migrations/Version_0_13_0.php
T
Alexandre Gain 71ff11390b refactor(plugin): rename Plan to Program and add audience/UTM routes
Rename the Plan bundle to Program across config, routes, menus, and
controllers (Plan -> Initiative), bump version to 0.16.0, and introduce
new audience and UTM taxonomy routes and permissions to support the
expanded Programs feature set.
2026-11-13 22:01:32 +01:00

128 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\ProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\ProgramBundle\Entity\Task;
use MauticPlugin\ProgramBundle\Entity\TaskDependency;
use MauticPlugin\ProgramBundle\Entity\TaskRaci;
/**
* Since 0.13.0 tasks are reworked:
* - start_date and end_date become DATE (no time); existing values are truncated to the date;
* - new columns task_kind (default 'task'), definition_of_done, planned_work;
* - new tables initiative_task_dependencies (dependency graph) and initiative_task_raci with its
* initiative_task_raci_stakeholders join table;
* - the parent/child tree is dropped: parent_id and its index are removed, replaced by the
* dependency graph.
*
* Each step runs only if needed.
*/
class Version_0_13_0 extends AbstractMigration
{
protected function isApplicable(Schema $schema): bool
{
// Historical migration: its changes were applied on every install that ran
// it before 0.16.0 (this plugin renamed Plan→Program/Initiative in 0.16.0), and
// a fresh install creates the current tables from the entity mapping. Never
// applicable anymore.
return false;
}
protected function up(): void
{
$tasks = $this->concatPrefix(Task::TABLE_NAME);
$table = $this->entityManager->getConnection()->createSchemaManager()->introspectTable($tasks);
// New columns.
if (!$table->hasColumn('task_kind')) {
$this->addSql(sprintf(
"ALTER TABLE %s ADD task_kind VARCHAR(20) NOT NULL DEFAULT '%s'",
$tasks,
\MauticPlugin\ProgramBundle\Entity\TaskKind::Task->value
));
}
if (!$table->hasColumn('definition_of_done')) {
$this->addSql("ALTER TABLE {$tasks} ADD definition_of_done LONGTEXT DEFAULT NULL");
}
if (!$table->hasColumn('planned_work')) {
$this->addSql("ALTER TABLE {$tasks} ADD planned_work INT UNSIGNED DEFAULT NULL");
}
// Dates: DATETIME -> DATE, truncating the time part.
$start = $table->getColumn('start_date');
$end = $table->getColumn('end_date');
if ('datetime' === $start->getType()->getName()) {
$this->addSql("ALTER TABLE {$tasks} MODIFY start_date DATE DEFAULT NULL");
$this->addSql("UPDATE {$tasks} SET start_date = DATE(start_date)");
}
if ('datetime' === $end->getType()->getName()) {
$this->addSql("ALTER TABLE {$tasks} MODIFY end_date DATE DEFAULT NULL");
$this->addSql("UPDATE {$tasks} SET end_date = DATE(end_date)");
}
// Drop the parent/child tree.
if ($table->hasColumn('parent_id')) {
$this->addSql("ALTER TABLE {$tasks} DROP COLUMN parent_id");
}
// New tables. Each is created when missing on its own, so a partial earlier run is repaired.
if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskDependency::TABLE_NAME))) {
foreach ($this->getCreateTableSqls(TaskDependency::class, TaskDependency::TABLE_NAME) as $sql) {
$this->addSql($sql);
}
}
if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskRaci::TABLE_NAME))) {
foreach ($this->getCreateTableSqls(TaskRaci::class, TaskRaci::TABLE_NAME) as $sql) {
$this->addSql($sql);
}
}
// The RACI join table comes with the TaskRaci entity's SQL (CREATE TABLE initiative_task_raci_stakeholders),
// but it may be missing while initiative_task_raci exists (a partial earlier run).
if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskRaci::JOIN_TABLE_NAME))) {
foreach ($this->getCreateTableSqls(TaskRaci::class, TaskRaci::JOIN_TABLE_NAME) as $sql) {
$this->addSql($sql);
}
}
}
private function introspectSchema(): Schema
{
return $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
}
/**
* The CREATE TABLE statements of one of this migration's entities, from Doctrine's SchemaTool —
* the entity's table and, for TaskRaci, its join table initiative_task_raci_stakeholders too.
*
* @param class-string $entityClass
*
* @return string[]
*/
private function getCreateTableSqls(string $entityClass, string $table): array
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE ';
$prefixes = [$prefix.$this->concatPrefix($table).' ', $prefix.$this->concatPrefix($table).'_'];
$statements = [];
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata($entityClass)]) as $sql) {
foreach ($prefixes as $candidate) {
if (str_starts_with($sql, $candidate)) {
$statements[] = $sql;
}
}
}
if ([] === $statements) {
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.$table.'.');
}
return $statements;
}
}