Files
mautic-marketing-program-bu…/Migrations/Version_0_16_0.php
T
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

160 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
/**
* Since 0.16.0 the PlanBundle is the MT3SMarketingProgramBundle, and a plan is an initiative: the Planning
* menu is Programs, the Plans page is Initiatives. The database follows:
* - plans becomes initiatives;
* - plan_objectives, plan_description_items, plan_allocation_items become initiative_*;
* - plan_tasks, plan_task_dependencies, plan_task_raci, plan_task_raci_stakeholders become
* initiative_*;
* - plan_stakeholders, plan_stakeholders_plan become initiative_stakeholders and
* initiative_stakeholders_initiative;
* - plan_audiences, plan_audiences_plan, plan_audiences_segments, plan_audiences_stages become
* initiative_*;
* - the link tables' plan_id columns become initiative_id; plan_stakeholders_plan's
* role_in_plan becomes role_in_initiative; plan_tasks' plan_id becomes initiative_id.
*
* Every statement runs immediately and re-reads the schema first, so the migration picks up
* again wherever an earlier run stopped (MySQL auto-commits DDL, so a failing batch of addSql()
* can leave the renames half done). A fresh install created by the plugin installer already
* uses the new names from the entity mapping.
*/
class Version_0_16_0 extends AbstractMigration
{
/**
* Old table name => new table name, in dependency order (the audited tables first, then the
* ones whose foreign keys point at them — RENAME TABLE takes both in one statement when
* needed, but one rename per line keeps half-applied states recoverable).
*/
private const TABLES = [
'plans' => 'initiatives',
'plan_objectives' => 'initiative_objectives',
'plan_description_items' => 'initiative_description_items',
'plan_allocation_items' => 'initiative_allocation_items',
'plan_tasks' => 'initiative_tasks',
'plan_task_dependencies' => 'initiative_task_dependencies',
'plan_task_raci' => 'initiative_task_raci',
'plan_task_raci_stakeholders' => 'initiative_task_raci_stakeholders',
'plan_stakeholders' => 'initiative_stakeholders',
'plan_stakeholders_plan' => 'initiative_stakeholders_initiative',
'plan_audiences' => 'initiative_audiences',
'plan_audiences_plan' => 'initiative_audiences_initiative',
'plan_audiences_segments' => 'initiative_audiences_segments',
'plan_audiences_stages' => 'initiative_audiences_stages',
];
/**
* Table (any of its names) => [old column => [new column, definition]].
*/
private const COLUMNS = [
['plan_objectives', 'initiative_objectives', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
['plan_description_items', 'initiative_description_items', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
['plan_allocation_items', 'initiative_allocation_items', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
['plan_tasks', 'initiative_tasks', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
['plan_stakeholders_plan', 'initiative_stakeholders_initiative', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
['plan_stakeholders_plan', 'initiative_stakeholders_initiative', 'role_in_plan', 'role_in_initiative', 'VARCHAR(191) DEFAULT NULL'],
['plan_audiences_plan', 'initiative_audiences_initiative', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'],
];
protected function isApplicable(Schema $schema): bool
{
foreach (self::TABLES as $from => $to) {
if ($schema->hasTable($this->concatPrefix($from))) {
return true;
}
}
foreach (self::COLUMNS as [$fromTable, $toTable, $fromColumn, $toColumn]) {
$table = $this->getTableName($schema, [$fromTable, $toTable]);
if (null !== $table) {
foreach ($schema->getTable($table)->getColumns() as $name => $column) {
if (strtolower($name) === strtolower($fromColumn)) {
return true;
}
}
}
}
return false;
}
protected function up(): void
{
// Tables, one rename at a time; stable database names unique.
foreach (self::TABLES as $from => $to) {
$this->renameTableIfExists($from, $to);
}
// Columns, resolving each table to whichever name it has now.
foreach (self::COLUMNS as [$fromTable, $toTable, $fromColumn, $toColumn, $definition]) {
$this->renameColumnIfExists([$fromTable, $toTable], $fromColumn, $toColumn, $definition);
}
}
/**
* Renames a table when the old name still exists and the new one doesn't.
*/
private function renameTableIfExists(string $from, string $to): void
{
$connection = $this->entityManager->getConnection();
if ($this->getTableName($connection->createSchemaManager()->introspectSchema(), [$from, $to]) === $this->concatPrefix($from)) {
$connection->executeStatement(sprintf(
'RENAME TABLE %1$s TO %2$s',
$this->concatPrefix($from),
$this->concatPrefix($to)
));
}
}
/**
* @param list<string> $tables the table's possible names, oldest first: the first that exists is used
*/
private function renameColumnIfExists(array $tables, string $fromColumn, string $toColumn, string $definition): void
{
$connection = $this->entityManager->getConnection();
$table = $this->getTableName($connection->createSchemaManager()->introspectSchema(), $tables);
if (null === $table) {
return;
}
foreach ($connection->createSchemaManager()->introspectTable($table)->getColumns() as $name => $column) {
if (strtolower($name) === strtolower($fromColumn)) {
$connection->executeStatement(sprintf(
'ALTER TABLE %s CHANGE %s %s %s',
$table,
$name,
$toColumn,
$definition
));
return;
}
}
}
/**
* The first of the given table names that exists, prefixed.
*
* @param list<string> $tables
*/
private function getTableName(Schema $schema, array $tables): ?string
{
foreach ($tables as $table) {
if ($schema->hasTable($this->concatPrefix($table))) {
return $this->concatPrefix($table);
}
}
return null;
}
}