first delivery
This commit is contained in:
1 parent
793fbc0c91
commit
f6684f3b2e
65 files changed
+5828
No files matched your search
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user