- 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
219 lines
9.5 KiB
PHP
219 lines
9.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
|
|
|
|
/**
|
|
* Since 0.17.0 the ProgramBundle is the MT3SMarketingProgramBundle, and an initiative is a marketing
|
|
* program: the Programs menu is Marketing Programs. The database follows:
|
|
* - initiatives becomes mt3s_mp_programs;
|
|
* - initiative_objectives, initiative_description_items, initiative_allocation_items become
|
|
* mt3s_mp_program_*;
|
|
* - initiative_tasks, initiative_task_dependencies, initiative_task_raci,
|
|
* initiative_task_raci_stakeholders become mt3s_mp_program_task_*;
|
|
* - initiative_stakeholders, initiative_stakeholders_initiative become mt3s_mp_program_stakeholders
|
|
* and mt3s_mp_program_stakeholders_program;
|
|
* - initiative_audiences, initiative_audiences_initiative, initiative_audiences_segments,
|
|
* initiative_audiences_stages become mt3s_mp_program_audiences*;
|
|
* - the link tables' initiative_id columns become program_id; the stakeholders link's
|
|
* role_in_initiative becomes role_in_program.
|
|
*
|
|
* The rows the rename orphans follow too: the plugins row (bundle ProgramBundle, kept as an update
|
|
* so the installer does not treat the plugin as new), the role permission rows (bundle program,
|
|
* name initiatives), the categories rows (bundle plan from before 0.16.0 or initiative since), and
|
|
* the audit log rows (bundle PlanBundle/ProgramBundle).
|
|
*
|
|
* 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_17_0 extends AbstractMigration
|
|
{
|
|
/**
|
|
* Old table name => new table name, in dependency order.
|
|
*/
|
|
private const TABLES = [
|
|
'initiatives' => 'mt3s_mp_programs',
|
|
'initiative_objectives' => 'mt3s_mp_program_objectives',
|
|
'initiative_description_items' => 'mt3s_mp_program_description_items',
|
|
'initiative_allocation_items' => 'mt3s_mp_program_allocation_items',
|
|
'initiative_tasks' => 'mt3s_mp_program_tasks',
|
|
'initiative_task_dependencies' => 'mt3s_mp_program_task_dependencies',
|
|
'initiative_task_raci' => 'mt3s_mp_program_task_raci',
|
|
'initiative_task_raci_stakeholders' => 'mt3s_mp_program_task_raci_stakeholders',
|
|
'initiative_stakeholders' => 'mt3s_mp_program_stakeholders',
|
|
'initiative_stakeholders_initiative' => 'mt3s_mp_program_stakeholders_program',
|
|
'initiative_audiences' => 'mt3s_mp_program_audiences',
|
|
'initiative_audiences_initiative' => 'mt3s_mp_program_audiences_program',
|
|
'initiative_audiences_segments' => 'mt3s_mp_program_audiences_segments',
|
|
'initiative_audiences_stages' => 'mt3s_mp_program_audiences_stages',
|
|
];
|
|
|
|
/**
|
|
* Table (any of its names) => [old column => new column, definition].
|
|
*/
|
|
private const COLUMNS = [
|
|
['initiative_objectives', 'mt3s_mp_program_objectives', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'],
|
|
['initiative_description_items', 'mt3s_mp_program_description_items', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'],
|
|
['initiative_allocation_items', 'mt3s_mp_program_allocation_items', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'],
|
|
['initiative_tasks', 'mt3s_mp_program_tasks', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'],
|
|
['initiative_stakeholders_initiative', 'mt3s_mp_program_stakeholders_program', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'],
|
|
['initiative_stakeholders_initiative', 'mt3s_mp_program_stakeholders_program', 'role_in_initiative', 'role_in_program', 'VARCHAR(191) DEFAULT NULL'],
|
|
['initiative_audiences_initiative', 'mt3s_mp_program_audiences_program', 'initiative_id', 'program_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]) {
|
|
$table = $this->getTableName($schema, [$fromTable, $toTable]);
|
|
if (null !== $table) {
|
|
foreach ($schema->getTable($table)->getColumns() as $name => $column) {
|
|
if (strtolower($name) === strtolower($fromColumn)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The pre-rename plugins row: the update target of an installed 0.16.0.
|
|
try {
|
|
$row = $this->entityManager->getConnection()->fetchOne(
|
|
sprintf("SELECT id FROM %s WHERE bundle = 'ProgramBundle'", $this->concatPrefix('plugins'))
|
|
);
|
|
} catch (\Throwable) {
|
|
$row = false;
|
|
}
|
|
|
|
return false !== $row;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
$this->carryRowsOver();
|
|
}
|
|
|
|
/**
|
|
* The rows the rename orphans: the plugin's own, the roles' permissions, the categories and
|
|
* the audit log.
|
|
*/
|
|
private function carryRowsOver(): void
|
|
{
|
|
$connection = $this->entityManager->getConnection();
|
|
|
|
// The plugins row: keep the install an update of the same plugin, not a new one.
|
|
$oldBundle = $connection->fetchOne(
|
|
sprintf("SELECT id FROM %s WHERE bundle = 'ProgramBundle'", $this->concatPrefix('plugins'))
|
|
);
|
|
if (false !== $oldBundle) {
|
|
$connection->executeStatement(sprintf(
|
|
"UPDATE %s SET bundle = 'MT3SMarketingProgramBundle', name = 'MT3S Marketing Program' WHERE bundle = 'ProgramBundle'",
|
|
$this->concatPrefix('plugins')
|
|
));
|
|
}
|
|
|
|
// The roles' permission rows: the permission base follows the bundle name, and
|
|
// initiatives became programs (the 0.16.0 rename missed these).
|
|
$connection->executeStatement(sprintf(
|
|
"UPDATE %s SET name = 'programs' WHERE bundle = 'program' AND name = 'initiatives'",
|
|
$this->concatPrefix('permissions')
|
|
));
|
|
$connection->executeStatement(sprintf(
|
|
"UPDATE %s SET bundle = 'mt3smarketingprogram' WHERE bundle = 'program'",
|
|
$this->concatPrefix('permissions')
|
|
));
|
|
|
|
// The categories of the program category type (bundle column), plan-era rows included:
|
|
// the 0.16.0 rename left those behind too.
|
|
$connection->executeStatement(sprintf(
|
|
"UPDATE %s SET bundle = 'program' WHERE bundle IN ('plan', 'initiative')",
|
|
$this->concatPrefix('categories')
|
|
));
|
|
|
|
// The audit log rows about this plugin's entities.
|
|
$connection->executeStatement(sprintf(
|
|
"UPDATE %s SET bundle = 'MT3SMarketingProgramBundle' WHERE bundle IN ('PlanBundle', 'ProgramBundle')",
|
|
$this->concatPrefix('audit_logs')
|
|
));
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|