Rename the plugin's vendor/namespace from experiential/Program to mt3s/MT3SMarketingProgram (composer: mt3s/mautic-marketing-program-bundle): - ProgramBundle -> MT3SMarketingProgramBundle; extension, integration, config support, permissions, events and twig extension renamed likewise - Initiative* -> Program*: controller (InitiativeController -> ProgramController), model, entities, repositories, form types and views (new _tabs/_task_table/_tab_table_filter partials, details page) - Settings config form theme -> _config_mt3smarketingprogramconfig_widget; routes and translations re-prefixed (mt3s_marketing_program_*) - Assets: icon.svg -> mt3s-mp-icon.svg; program.js -> mt3s-mp-program.js (OnLoad hooks renamed mt3sMpProgram*) - Add Command/RefreshStatsCommand; Migrations/Version_0_17_0.php renames the tables to mt3s_mp_program_*
128 lines
5.3 KiB
PHP
128 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\ORM\Tools\SchemaTool;
|
|
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskDependency;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\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('initiative_tasks');
|
|
$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\MT3SMarketingProgramBundle\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('initiative_task_dependencies'))) {
|
|
foreach ($this->getCreateTableSqls(TaskDependency::class, 'initiative_task_dependencies') as $sql) {
|
|
$this->addSql($sql);
|
|
}
|
|
}
|
|
if (!$this->introspectSchema()->hasTable($this->concatPrefix('initiative_task_raci'))) {
|
|
foreach ($this->getCreateTableSqls(TaskRaci::class, 'initiative_task_raci') 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('initiative_task_raci_stakeholders'))) {
|
|
foreach ($this->getCreateTableSqls(TaskRaci::class, 'initiative_task_raci_stakeholders') 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;
|
|
}
|
|
}
|