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