Spaces:
No application file
No application file
File size: 3,086 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php
declare(strict_types=1);
namespace Mautic\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\CoreBundle\Doctrine\PreUpAssertionMigration;
final class Version20240226114528 extends PreUpAssertionMigration
{
protected function preUpAssertions(): void
{
$this->skipAssertion(function (Schema $schema) {
return $schema->hasTable($this->getTableName());
}, sprintf('Table %s already exists', $this->getTableName()));
$this->skipAssertion(function (Schema $schema) {
return $schema->getTable($this->getTableName())
->hasForeignKey($this->getForeignKeyName('email_id'));
}, sprintf('Foreign key %s already exists', $this->getForeignKeyName('email_id')));
$this->skipAssertion(function (Schema $schema) {
return $schema->getTable($this->getTableName())
->hasForeignKey($this->getForeignKeyName('leadlist_id'));
}, sprintf('Foreign key %s already exists', $this->getForeignKeyName('leadlist_id')));
}
public function up(Schema $schema): void
{
$tableName = $this->getTableName();
$this->addSql(sprintf('
CREATE TABLE %s (
email_id INT %s NOT NULL,
leadlist_id INT %s NOT NULL,
INDEX %s (email_id),
INDEX %s (leadlist_id),
PRIMARY KEY(email_id, leadlist_id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC',
$tableName,
$this->getIntegerRange($schema, 'emails'),
$this->getIntegerRange($schema, 'lead_lists'),
$this->getIndexName('email_id'),
$this->getIndexName('leadlist_id')
));
$this->addSql(sprintf('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (email_id) REFERENCES %semails (id) ON DELETE CASCADE',
$tableName,
$this->getForeignKeyName('email_id'),
$this->prefix
));
$this->addSql(sprintf('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (leadlist_id) REFERENCES %slead_lists (id) ON DELETE CASCADE',
$tableName,
$this->getForeignKeyName('leadlist_id'),
$this->prefix
));
}
private function getTableName(): string
{
return "{$this->prefix}{$this->getBareTableName()}";
}
private function getBareTableName(): string
{
return 'email_list_excluded';
}
private function getIndexName(string $column): string
{
return $this->generatePropertyName($this->getBareTableName(), 'idx', [$column]);
}
private function getForeignKeyName(string $column): string
{
return $this->generatePropertyName($this->getBareTableName(), 'fk', [$column]);
}
private function getIntegerRange(Schema $schema, string $bareTableName): string
{
$unsigned = $schema->getTable("{$this->prefix}{$bareTableName}")
->getColumn('id')
->getUnsigned();
return $unsigned ? 'UNSIGNED' : '';
}
}
|