Files
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

98 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
/**
* Adds the initiative_tasks table and its foreign keys to installs created before 0.4.0.
*
* The CREATE TABLE comes from the Task mapping, so it can't drift from the entity. Only that statement
* is kept: SchemaTool's output also contains statements for other tables (for example Symfony
* Messenger's messenger_messages), which would fail on an existing install.
*
* The foreign keys are added one by one when missing, so the migration also repairs a initiative_tasks table
* that exists without them. Fresh installs get everything from the entity mapping, so this migration
* then finds nothing to do.
*/
class Version_0_4_0 extends AbstractMigration
{
/**
* Local column => [referenced table, ON DELETE action].
*
* parent_id is gone since 0.13.0 (the parent/child tree was replaced by the dependency graph),
* so only plan_id is repaired here.
*/
private const FOREIGN_KEYS = [
'plan_id' => ['initiatives', 'CASCADE'],
];
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();
$tasks = $this->concatPrefix('initiative_tasks');
$missingFk = self::FOREIGN_KEYS;
if (!$schema->hasTable($tasks)) {
$this->addSql($this->getCreateTableSql());
} else {
$missingFk = $this->getMissingForeignKeys($schema->getTable($tasks));
}
foreach ($missingFk as $column => [$referenceTable, $onDelete]) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
'initiative_tasks',
[$column],
$referenceTable,
['id'],
'ON DELETE '.$onDelete
));
}
}
private function getCreateTableSql(): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix('initiative_tasks').' ';
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(Task::class)]) as $sql) {
if (str_starts_with($sql, $prefix)) {
return $sql;
}
}
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for initiative_tasks.');
}
/**
* @return array<string, array{0: string, 1: 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;
}
}