Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\Migrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\DBAL\Schema\Table;
|
|
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
|
|
use MauticPlugin\ProgramBundle\Entity\Initiative;
|
|
|
|
/**
|
|
* Brings the plans table in line with the entity mapping.
|
|
*
|
|
* The plugin installer only runs SchemaTool on the plugin's own entities, so it drops the foreign keys
|
|
* to core tables (users, categories). This migration adds them with ON DELETE SET NULL, and shrinks the
|
|
* status column to VARCHAR(20). It is idempotent: it runs on plugin update (by the core migration
|
|
* engine) and right after install (by PluginInstallSubscriber).
|
|
*/
|
|
class Version_0_2_0 extends AbstractMigration
|
|
{
|
|
private const STATUS_LENGTH = 20;
|
|
|
|
/**
|
|
* Local column => referenced core table.
|
|
*/
|
|
private const FOREIGN_KEYS = [
|
|
'owner_id' => 'users',
|
|
'category_id' => 'categories',
|
|
];
|
|
|
|
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();
|
|
$table = $this->getTable($schema);
|
|
|
|
if (null === $table) {
|
|
return;
|
|
}
|
|
|
|
if ($this->statusNeedsResize($table)) {
|
|
$this->addSql(sprintf(
|
|
'ALTER TABLE %s MODIFY status VARCHAR(%d) NOT NULL',
|
|
$this->concatPrefix(Initiative::TABLE_NAME),
|
|
self::STATUS_LENGTH
|
|
));
|
|
}
|
|
|
|
foreach ($this->getMissingForeignKeys($table) as $column => $referenceTable) {
|
|
$this->addSql($this->generateAlterTableForeignKeyStatement(
|
|
Initiative::TABLE_NAME,
|
|
[$column],
|
|
$referenceTable,
|
|
['id'],
|
|
'ON DELETE SET NULL'
|
|
));
|
|
}
|
|
}
|
|
|
|
private function getTable(Schema $schema): ?Table
|
|
{
|
|
$name = $this->concatPrefix(Initiative::TABLE_NAME);
|
|
|
|
return $schema->hasTable($name) ? $schema->getTable($name) : null;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private function getMissingForeignKeys(Table $table): array
|
|
{
|
|
$missing = self::FOREIGN_KEYS;
|
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) {
|
|
foreach ($foreignKey->getLocalColumns() as $column) {
|
|
unset($missing[$column]);
|
|
}
|
|
}
|
|
|
|
return $missing;
|
|
}
|
|
|
|
private function statusNeedsResize(Table $table): bool
|
|
{
|
|
return $table->hasColumn('status') && self::STATUS_LENGTH !== $table->getColumn('status')->getLength();
|
|
}
|
|
}
|