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_*
143 lines
5.5 KiB
PHP
143 lines
5.5 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\Audience;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\AudienceType as AudienceTypeEnum;
|
|
/**
|
|
* Since 0.11.0 a plan has audiences (table initiative_audiences, initiative_audiences_stages before 0.15.0) with any
|
|
* number of Mautic stages (join table initiative_audiences_stages). Creates both tables for installs created
|
|
* before 0.11.0, and repairs the ones the plugin installer created with camelCase column names (an
|
|
* early version of the mapping). Each step runs only if needed.
|
|
*
|
|
* Since 0.14.0 audiences are a global directory: initiative_audiences no longer carries plan_id (the plan
|
|
* association lives in initiative_audiences_initiative), so the plan_id repair is retired here; only the
|
|
* camelCase repair can still apply to an old install.
|
|
*
|
|
* Since 0.15.0 the tables are initiative_audiences and initiative_audiences_stages: an install already on 0.14.0
|
|
* was migrated by 0.15.0, so this migration creates nothing; one created before 0.11.0 that skipped
|
|
* every version since gets the current tables from the entity mapping directly, by looking for the
|
|
* current names too.
|
|
*/
|
|
class Version_0_11_0 extends AbstractMigration
|
|
{
|
|
/**
|
|
* Field name => column name, for the columns an early version of the mapping left in camelCase.
|
|
*/
|
|
private const COLUMNS = [
|
|
'targetType' => 'target_type',
|
|
'profileCriteria' => 'profile_criteria',
|
|
'painPoints' => 'pain_points',
|
|
'valueProposition' => 'value_proposition',
|
|
];
|
|
|
|
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();
|
|
|
|
// An install created before 0.11.0 that somehow never got the tables: create the current
|
|
// (0.15.0) ones from the entity mapping directly.
|
|
if (!$schema->hasTable($this->concatPrefix('initiative_audiences'))) {
|
|
$this->addSql($this->getCreateTableSql('initiative_audiences'));
|
|
}
|
|
|
|
if (!$schema->hasTable($this->concatPrefix('initiative_audiences_stages'))
|
|
&& !$schema->hasTable($this->concatPrefix('initiative_audiences_stages'))) {
|
|
$this->addSql($this->getCreateTableSql('initiative_audiences_stages'));
|
|
}
|
|
|
|
foreach ($this->getColumnsToRename($schema) as $field => $column) {
|
|
$this->addSql(sprintf(
|
|
'ALTER TABLE %s CHANGE %s %s',
|
|
$this->concatPrefix('initiative_audiences'),
|
|
$field,
|
|
$this->getColumnDefinition($field, $column)
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string> field name => column name, for the camelCase columns still present
|
|
*/
|
|
private function getColumnsToRename(Schema $schema): array
|
|
{
|
|
if (!$schema->hasTable($this->concatPrefix('initiative_audiences'))) {
|
|
return [];
|
|
}
|
|
|
|
$table = $schema->getTable($this->concatPrefix('initiative_audiences'));
|
|
$rename = [];
|
|
|
|
foreach (self::COLUMNS as $field => $column) {
|
|
if ($table->hasColumn($field) && !$table->hasColumn($column)) {
|
|
$rename[$field] = $column;
|
|
}
|
|
}
|
|
|
|
return $rename;
|
|
}
|
|
|
|
private function hasForeignKeys(Schema $schema): bool
|
|
{
|
|
$audiences = $this->concatPrefix('initiative_audiences');
|
|
|
|
if (!$schema->hasTable($audiences)) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($schema->getTable($audiences)->getForeignKeys() as $foreignKey) {
|
|
if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* The MySQL column definition matching the entity mapping, for a CHANGE that renames the column.
|
|
*/
|
|
private function getColumnDefinition(string $field, string $column): string
|
|
{
|
|
$definitions = [
|
|
'targetType' => sprintf("%s VARCHAR(30) NOT NULL DEFAULT '%s'", $column, AudienceTypeEnum::Persona->value),
|
|
'profileCriteria' => $column.' LONGTEXT DEFAULT NULL',
|
|
'painPoints' => $column.' LONGTEXT DEFAULT NULL',
|
|
'valueProposition' => $column.' LONGTEXT DEFAULT NULL',
|
|
];
|
|
|
|
return $definitions[$field] ?? $column;
|
|
}
|
|
|
|
/**
|
|
* The CREATE TABLE statement for one of this migration's tables, from Doctrine's SchemaTool.
|
|
*/
|
|
private function getCreateTableSql(string $table): string
|
|
{
|
|
$schemaTool = new SchemaTool($this->entityManager);
|
|
$prefix = 'CREATE TABLE '.$this->concatPrefix($table).' ';
|
|
|
|
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(Audience::class)]) as $sql) {
|
|
if (str_starts_with($sql, $prefix)) {
|
|
return $sql;
|
|
}
|
|
}
|
|
|
|
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.$table.'.');
|
|
}
|
|
}
|