first delivery

This commit is contained in:
Alexandre Gain committed 2026-11-07 19:31:49 +01:00
1 parent 793fbc0c91
commit f6684f3b2e
65 files changed
+5828

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
/**
* 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
{
$table = $this->getTable($schema);
if (null === $table) {
return false;
}
return [] !== $this->getMissingForeignKeys($table) || $this->statusNeedsResize($table);
}
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(Plan::TABLE_NAME),
self::STATUS_LENGTH
));
}
foreach ($this->getMissingForeignKeys($table) as $column => $referenceTable) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
Plan::TABLE_NAME,
[$column],
$referenceTable,
['id'],
'ON DELETE SET NULL'
));
}
}
private function getTable(Schema $schema): ?Table
{
$name = $this->concatPrefix(Plan::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();
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\PlanObjective;
/**
* Adds plans.version and the plan_objectives table to installs created before 0.3.0.
*
* Fresh installs get both from the entity mapping, so this migration then finds nothing to do.
*/
class Version_0_3_0 extends AbstractMigration
{
protected function isApplicable(Schema $schema): bool
{
$plans = $this->concatPrefix(Plan::TABLE_NAME);
if (!$schema->hasTable($plans)) {
return false;
}
return !$schema->getTable($plans)->hasColumn('version')
|| !$schema->hasTable($this->concatPrefix(PlanObjective::TABLE_NAME));
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$plans = $this->concatPrefix(Plan::TABLE_NAME);
$objectives = $this->concatPrefix(PlanObjective::TABLE_NAME);
if (!$schema->getTable($plans)->hasColumn('version')) {
$this->addSql("ALTER TABLE {$plans} ADD version SMALLINT UNSIGNED DEFAULT 1 NOT NULL");
}
if (!$schema->hasTable($objectives)) {
$index = $this->generatePropertyName(PlanObjective::TABLE_NAME, 'idx', ['plan_id']);
$foreignKey = $this->generatePropertyName(PlanObjective::TABLE_NAME, 'fk', ['plan_id']);
$this->addSql("CREATE TABLE {$objectives} (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
plan_id INT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description LONGTEXT DEFAULT NULL,
position INT DEFAULT 0 NOT NULL,
is_achieved TINYINT(1) DEFAULT 0 NOT NULL,
INDEX {$index} (plan_id),
PRIMARY KEY(id),
CONSTRAINT {$foreignKey} FOREIGN KEY (plan_id) REFERENCES {$plans} (id) ON DELETE CASCADE
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB ROW_FORMAT = DYNAMIC");
}
}
}
+98
View File
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\Task;
/**
* Adds the plan_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 plan_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].
*/
private const FOREIGN_KEYS = [
'plan_id' => [Plan::TABLE_NAME, 'CASCADE'],
'parent_id' => [Task::TABLE_NAME, 'SET NULL'],
];
protected function isApplicable(Schema $schema): bool
{
if (!$schema->hasTable($this->concatPrefix(Plan::TABLE_NAME))) {
return false;
}
$tasks = $this->concatPrefix(Task::TABLE_NAME);
return !$schema->hasTable($tasks) || [] !== $this->getMissingForeignKeys($schema->getTable($tasks));
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$tasks = $this->concatPrefix(Task::TABLE_NAME);
$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(
Task::TABLE_NAME,
[$column],
$referenceTable,
['id'],
'ON DELETE '.$onDelete
));
}
}
private function getCreateTableSql(): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix(Task::TABLE_NAME).' ';
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 '.Task::TABLE_NAME.'.');
}
/**
* @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;
}
}
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Task;
use MauticPlugin\PlanBundle\Entity\TaskStatus;
/**
* Adds start_date, end_date and status (default "new", indexed) to plan_tasks, for installs created
* before 0.5.0. Existing tasks get the status "new". Each column is added only if missing.
*/
class Version_0_5_0 extends AbstractMigration
{
private const STATUS_INDEX = 'plan_task_status';
protected function isApplicable(Schema $schema): bool
{
$tasks = $this->concatPrefix(Task::TABLE_NAME);
if (!$schema->hasTable($tasks)) {
return false;
}
$table = $schema->getTable($tasks);
return !$table->hasColumn('start_date')
|| !$table->hasColumn('end_date')
|| !$table->hasColumn('status')
|| !$table->hasIndex(self::STATUS_INDEX);
}
protected function up(): void
{
$tasks = $this->concatPrefix(Task::TABLE_NAME);
$table = $this->entityManager->getConnection()->createSchemaManager()->introspectTable($tasks);
if (!$table->hasColumn('start_date')) {
$this->addSql("ALTER TABLE {$tasks} ADD start_date DATETIME DEFAULT NULL");
}
if (!$table->hasColumn('end_date')) {
$this->addSql("ALTER TABLE {$tasks} ADD end_date DATETIME DEFAULT NULL");
}
if (!$table->hasColumn('status')) {
$this->addSql(sprintf(
"ALTER TABLE %s ADD status VARCHAR(20) DEFAULT '%s' NOT NULL",
$tasks,
TaskStatus::New->value
));
}
if (!$table->hasIndex(self::STATUS_INDEX)) {
// Same name as in the Task mapping (index names aren't prefixed).
$this->addSql('CREATE INDEX '.self::STATUS_INDEX." ON {$tasks} (status)");
}
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\PlanDescriptionItem;
/**
* For installs created before 0.6.0:
* - creates plan_description_items (and its foreign key to plans, ON DELETE CASCADE);
* - drops plans.description, which description items replace. Its content is deleted, as decided
* when description items were introduced.
*
* Each step runs only if needed. As in Version_0_4_0, only the CREATE TABLE statement for this table is
* kept from SchemaTool's output.
*/
class Version_0_6_0 extends AbstractMigration
{
protected function isApplicable(Schema $schema): bool
{
$plans = $this->concatPrefix(Plan::TABLE_NAME);
if (!$schema->hasTable($plans)) {
return false;
}
$items = $this->concatPrefix(PlanDescriptionItem::TABLE_NAME);
return !$schema->hasTable($items)
|| !$this->hasPlanForeignKey($schema)
|| $schema->getTable($plans)->hasColumn('description');
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$plans = $this->concatPrefix(Plan::TABLE_NAME);
$items = $this->concatPrefix(PlanDescriptionItem::TABLE_NAME);
if (!$schema->hasTable($items)) {
$this->addSql($this->getCreateTableSql());
}
if (!$schema->hasTable($items) || !$this->hasPlanForeignKey($schema)) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
PlanDescriptionItem::TABLE_NAME,
['plan_id'],
Plan::TABLE_NAME,
['id'],
'ON DELETE CASCADE'
));
}
if ($schema->getTable($plans)->hasColumn('description')) {
$this->addSql("ALTER TABLE {$plans} DROP COLUMN description");
}
}
private function hasPlanForeignKey(Schema $schema): bool
{
$items = $this->concatPrefix(PlanDescriptionItem::TABLE_NAME);
if (!$schema->hasTable($items)) {
return false;
}
foreach ($schema->getTable($items)->getForeignKeys() as $foreignKey) {
if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) {
return true;
}
}
return false;
}
private function getCreateTableSql(): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix(PlanDescriptionItem::TABLE_NAME).' ';
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(PlanDescriptionItem::class)]) as $sql) {
if (str_starts_with($sql, $prefix)) {
return $sql;
}
}
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.PlanDescriptionItem::TABLE_NAME.'.');
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\PlanAllocationItem;
/**
* Creates plan_allocation_items (and its foreign key to plans, ON DELETE CASCADE) for installs created
* before 0.7.0. Each step runs only if needed. As in Version_0_4_0, only the CREATE TABLE statement for
* this table is kept from SchemaTool's output.
*/
class Version_0_7_0 extends AbstractMigration
{
protected function isApplicable(Schema $schema): bool
{
if (!$schema->hasTable($this->concatPrefix(Plan::TABLE_NAME))) {
return false;
}
return !$schema->hasTable($this->concatPrefix(PlanAllocationItem::TABLE_NAME)) || !$this->hasPlanForeignKey($schema);
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
$items = $this->concatPrefix(PlanAllocationItem::TABLE_NAME);
if (!$schema->hasTable($items)) {
$this->addSql($this->getCreateTableSql());
}
if (!$this->hasPlanForeignKey($schema)) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
PlanAllocationItem::TABLE_NAME,
['plan_id'],
Plan::TABLE_NAME,
['id'],
'ON DELETE CASCADE'
));
}
}
private function hasPlanForeignKey(Schema $schema): bool
{
$items = $this->concatPrefix(PlanAllocationItem::TABLE_NAME);
if (!$schema->hasTable($items)) {
return false;
}
foreach ($schema->getTable($items)->getForeignKeys() as $foreignKey) {
if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) {
return true;
}
}
return false;
}
private function getCreateTableSql(): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix(PlanAllocationItem::TABLE_NAME).' ';
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(PlanAllocationItem::class)]) as $sql) {
if (str_starts_with($sql, $prefix)) {
return $sql;
}
}
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.PlanAllocationItem::TABLE_NAME.'.');
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\PlanAllocationItem;
/**
* Since 0.8.0 every amount is in one general currency (Settings > Configuration > Plan Settings, parameter
* plan_currency), so the per-record currency columns are dropped: plans.currency,
* plan_allocation_items.planned_currency and plan_allocation_items.real_currency. Amounts are kept as they
* are. Each column is dropped only if it exists.
*/
class Version_0_8_0 extends AbstractMigration
{
/**
* Table => columns to drop.
*/
private const COLUMNS = [
Plan::TABLE_NAME => ['currency'],
PlanAllocationItem::TABLE_NAME => ['planned_currency', 'real_currency'],
];
protected function isApplicable(Schema $schema): bool
{
return [] !== $this->getColumnsToDrop($schema);
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
foreach ($this->getColumnsToDrop($schema) as $table => $columns) {
foreach ($columns as $column) {
$this->addSql("ALTER TABLE {$table} DROP COLUMN {$column}");
}
}
}
/**
* @return array<string, string[]> prefixed table => existing columns to drop
*/
private function getColumnsToDrop(Schema $schema): array
{
$drop = [];
foreach (self::COLUMNS as $table => $columns) {
$table = $this->concatPrefix($table);
if (!$schema->hasTable($table)) {
continue;
}
$existing = array_values(array_filter($columns, fn (string $column): bool => $schema->getTable($table)->hasColumn($column)));
if ([] !== $existing) {
$drop[$table] = $existing;
}
}
return $drop;
}
}