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_*
204 lines
9.7 KiB
PHP
204 lines
9.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Audience;
|
|
/**
|
|
* Since 0.14.0 audiences are a global directory (Planning > Audiences, called Targets before 0.15.0),
|
|
* like stakeholders:
|
|
* - the existing initiative_audiences rows become the directory entries; is_published and the audit
|
|
* columns (FormEntity) are added;
|
|
* - the new persona attributes are added: tagline, motivations, barriers, frustrations
|
|
* (replacing pain_points, whose texts are moved), triggers, decision_role, preferred_tones;
|
|
* - the initiative-specific fields (priority, value proposition, position) move to the new link table
|
|
* initiative_audiences_initiative, one row per (plan, target); the existing rows are copied there, then the
|
|
* initiative-specific columns (and plan_id) are dropped from initiative_audiences.
|
|
*
|
|
* Since 0.15.0 the tables are named initiative_audiences and initiative_audiences_initiative: this migration only
|
|
* still applies to an install on 0.13.x with the old names, or to repair one the plugin installer
|
|
* created without foreign keys (it drops foreign keys that point to core tables). Each step runs
|
|
* only if needed. Which persona attributes are offered is an admin setting (AudienceAttributes).
|
|
*/
|
|
class Version_0_14_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
|
|
{
|
|
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
|
|
|
|
$names = $this->getTableNames($schema);
|
|
|
|
// Fresh install created before 0.11.0: no directory table yet. Nothing to migrate here.
|
|
if (null === $names) {
|
|
return;
|
|
}
|
|
|
|
[$directory, $links] = $names;
|
|
$oldNaming = 'initiative_audiences' === $directory;
|
|
|
|
$table = $schema->getTable($directory);
|
|
$shared = fn (string $column): bool => $table->hasColumn($this->concatPrefix($column));
|
|
|
|
// Directory columns: is_published and the audit columns of FormEntity (the installer adds
|
|
// them on a fresh install; they are missing only on an upgrade from 0.13.0).
|
|
if (!$shared('is_published')) {
|
|
$this->addSql(sprintf('ALTER TABLE %s ADD is_published TINYINT(1) NOT NULL DEFAULT 1', $directory));
|
|
$this->addSql(sprintf(
|
|
'ALTER TABLE %1$s ADD date_added DATETIME DEFAULT NULL, ADD created_by INT DEFAULT NULL, '.
|
|
'ADD created_by_user VARCHAR(191) DEFAULT NULL, ADD date_modified DATETIME DEFAULT NULL, '.
|
|
'ADD modified_by INT DEFAULT NULL, ADD modified_by_user VARCHAR(191) DEFAULT NULL, '.
|
|
'ADD checked_out DATETIME DEFAULT NULL, ADD checked_out_by INT DEFAULT NULL, '.
|
|
'ADD checked_out_by_user VARCHAR(191) DEFAULT NULL',
|
|
$directory
|
|
));
|
|
}
|
|
|
|
// The new persona attributes (free text).
|
|
foreach (['tagline', 'motivations', 'barriers', 'triggers'] as $column) {
|
|
if (!$shared($column)) {
|
|
$this->addSql(sprintf('ALTER TABLE %s ADD %s LONGTEXT DEFAULT NULL', $directory, $column));
|
|
}
|
|
}
|
|
|
|
// frustrations replaces pain_points: add the column, move the texts, then drop the old one
|
|
// (dropped with the initiative-specific columns below, in one sweep).
|
|
if (!$shared('frustrations')) {
|
|
$this->addSql(sprintf('ALTER TABLE %s ADD frustrations LONGTEXT DEFAULT NULL', $directory));
|
|
if ($shared('pain_points')) {
|
|
$this->addSql(sprintf(
|
|
'UPDATE %s SET frustrations = pain_points WHERE frustrations IS NULL AND pain_points IS NOT NULL',
|
|
$directory
|
|
));
|
|
}
|
|
}
|
|
|
|
// decision_role: a key of the admin-configured list (program_audience_decision_roles).
|
|
if (!$shared('decision_role')) {
|
|
$this->addSql(sprintf('ALTER TABLE %s ADD decision_role VARCHAR(64) DEFAULT NULL', $directory));
|
|
}
|
|
|
|
// preferred_tones: a JSON list of keys of the admin-configured list (program_audience_tones).
|
|
if (!$shared('preferred_tones')) {
|
|
$this->addSql(sprintf("ALTER TABLE %s ADD preferred_tones LONGTEXT DEFAULT NULL COMMENT '(DC2Type:json)'", $directory));
|
|
}
|
|
|
|
// The link table, one row per (plan, audience), with the initiative-specific fields. Under the old
|
|
// naming it keeps the old names (initiative_audiences_initiative, target_id); 0.15.0 renames both.
|
|
if (!$schema->hasTable($links)) {
|
|
$linkIdColumn = $oldNaming ? 'target_id' : 'audience_id';
|
|
$this->addSql(sprintf(
|
|
'CREATE TABLE IF NOT EXISTS %s (id INT UNSIGNED AUTO_INCREMENT NOT NULL, '.
|
|
'plan_id INT UNSIGNED NOT NULL, %s INT UNSIGNED NOT NULL, priority VARCHAR(20) NOT NULL, '.
|
|
'value_proposition LONGTEXT DEFAULT NULL, position INT NOT NULL, '.
|
|
'INDEX IDX_%1$s_PLAN (plan_id), INDEX IDX_%1$s_LINK (%2$s), '.
|
|
'PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB',
|
|
$links,
|
|
$linkIdColumn
|
|
));
|
|
|
|
// Copy the existing rows: each audience of each plan becomes a directory entry plus a
|
|
// link carrying the initiative-specific fields. The position follows the id order, so the
|
|
// tab shows the same order as before the migration.
|
|
if ($shared('plan_id')) {
|
|
$this->addSql(sprintf(
|
|
'INSERT INTO %1$s (plan_id, %2$s, priority, value_proposition, position) '.
|
|
'SELECT t.plan_id, t.id, t.priority, t.value_proposition, '.
|
|
'(SELECT COUNT(*) FROM %1$s l WHERE l.plan_id = t.plan_id AND l.%2$s <= t.id) '.
|
|
'FROM %3$s t WHERE t.plan_id IS NOT NULL ORDER BY t.id ASC',
|
|
$links,
|
|
$linkIdColumn,
|
|
$directory
|
|
));
|
|
|
|
// The directory entry no longer belongs to a plan; the initiative-specific fields moved
|
|
// to the link table; pain_points moved to frustrations; position is the link's.
|
|
// Drop the old plan_id foreign key first: it would block the column drop.
|
|
foreach ($schema->getTable($directory)->getForeignKeys() as $foreignKey) {
|
|
if (in_array($this->concatPrefix('plan_id'), $foreignKey->getLocalColumns(), true)) {
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP FOREIGN KEY %s', $directory, $foreignKey->getName()));
|
|
break;
|
|
}
|
|
}
|
|
|
|
foreach (['plan_id', 'priority', 'value_proposition', 'position', 'pain_points'] as $column) {
|
|
if ($shared($column)) {
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP COLUMN %s', $directory, $column));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Repair the foreign keys the plugin installer stripped (it drops the ones that point to
|
|
// core tables, such as plans, at install time).
|
|
if (!$this->hasForeignKeys($schema)) {
|
|
$this->addSql($this->generateAlterTableForeignKeyStatement(
|
|
$oldNaming ? 'initiative_audiences_initiative' : 'initiative_audiences_initiative',
|
|
['plan_id'],
|
|
'initiatives',
|
|
['id'],
|
|
'ON DELETE CASCADE'
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The directory and link table names this install uses: the current ones (initiative_audiences,
|
|
* initiative_audiences_initiative, since 0.15.0) or the ones before 0.15.0 (initiative_audiences, initiative_audiences_initiative).
|
|
* Null when neither exists (a fresh install created before 0.11.0).
|
|
*
|
|
* @return array{string, string}|null
|
|
*/
|
|
private function getTableNames(Schema $schema): ?array
|
|
{
|
|
foreach ([
|
|
['initiative_audiences', 'initiative_audiences_initiative'],
|
|
['initiative_audiences', 'initiative_audiences_initiative'],
|
|
] as [$directory, $links]) {
|
|
if ($schema->hasTable($this->concatPrefix($directory))) {
|
|
return [$this->concatPrefix($directory), $this->concatPrefix($links)];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* True when the link table's plan_id foreign key exists (the one the installer strips; the
|
|
* audience_id one points to a plugin table and survives). Checks the link table under either
|
|
* naming (initiative_audiences_initiative before 0.15.0, initiative_audiences_initiative since).
|
|
*/
|
|
private function hasForeignKeys(Schema $schema): bool
|
|
{
|
|
foreach (['initiative_audiences_initiative', 'initiative_audiences_initiative'] as $links) {
|
|
$links = $this->concatPrefix($links);
|
|
|
|
if (!$schema->hasTable($links)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($schema->getTable($links)->getForeignKeys() as $foreignKey) {
|
|
if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// No link table (fresh install from before 0.11.0): up() creates it from the
|
|
// mapping, foreign keys included — nothing to repair first.
|
|
return true;
|
|
}
|
|
} |