Skip to content

Commit 43c93fe

Browse files
rohitpavaskarrohitp19escopecz
authored
Fix migrations preup with table prefix and migration performance (#11371)
* Fixed migration prefix index issue * Fix Version20191219155630 migration to 100% cover perUp condition * Fix Version20191017140848 migration to 100% cover perUp condition * Fix Version20200227110431 migration to 100% cover preUp condition to check if index is present * Fix Version20200805185714 migration to 100% cover preUp condition * Fixed isse with prefix and innodb_strict_mode * Fixed CSFixer Co-authored-by: Rohit Pavaskar <[email protected]> Co-authored-by: John Linhart <[email protected]>
1 parent b95461b commit 43c93fe

5 files changed

+91
-39
lines changed

migrations/Version20190724110039.php

+48-28
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,78 @@
55
use Doctrine\DBAL\Schema\Schema;
66
use Mautic\CoreBundle\Doctrine\AbstractMauticMigration;
77

8-
/**
9-
* Auto-generated Migration: Please modify to your needs!
10-
*/
11-
class Version20190724110039 extends AbstractMauticMigration
8+
final class Version20190724110039 extends AbstractMauticMigration
129
{
10+
private const LEADS_TABLE = 'leads';
11+
private const LEAD_FIELDS_TABLE = 'lead_fields';
12+
13+
/**
14+
* Change multiselect custom fields from VARCHAR(191) to TEXT.
15+
*/
1316
public function up(Schema $schema): void
1417
{
15-
// Change multiselect custom fields from VARCHAR(255) to TEXT
16-
$qb = $this->connection->createQueryBuilder();
17-
$qb->select('lf.alias')
18-
->from($this->prefix.'lead_fields', 'lf')
19-
->where(
20-
$qb->expr()->eq('lf.type', $qb->expr()->literal('multiselect'))
21-
);
22-
$multiselectFields = $qb->execute()->fetchAll();
23-
24-
$leadsTable = $schema->getTable($this->prefix.'leads');
18+
$multiselectFields = $this->getMultiselectFields();
19+
$leadsTable = $schema->getTable($this->prefix.self::LEADS_TABLE);
20+
$changeStatements = [];
2521

2622
if (!empty($multiselectFields)) {
2723
foreach ($multiselectFields as $field) {
2824
if ($leadsTable->hasColumn($field['alias'])) {
2925
if ($leadsTable->hasIndex("{$this->prefix}{$field['alias']}_search")) {
30-
$this->addSql("DROP INDEX `{$this->prefix}{$field['alias']}_search` ON `{$this->prefix}leads`");
26+
$changeStatements[] = "DROP INDEX `{$this->prefix}{$field['alias']}_search`";
27+
}
28+
if (!empty($this->prefix) && $leadsTable->hasIndex("{$field['alias']}_search")) {
29+
$changeStatements[] = "DROP INDEX `{$field['alias']}_search`";
3130
}
32-
$this->addSql("ALTER TABLE `{$this->prefix}leads` CHANGE `{$field['alias']}` `{$field['alias']}` TEXT");
31+
$changeStatements[] = "CHANGE `{$field['alias']}` `{$field['alias']}` TEXT";
3332
}
3433
}
34+
35+
if (!empty($changeStatements)) {
36+
$this->addSql('SET innodb_strict_mode = OFF;');
37+
$this->addSql('ALTER TABLE `'.$this->prefix.self::LEADS_TABLE.'` '.implode(', ', $changeStatements));
38+
$this->addSql('SET innodb_strict_mode = ON;');
39+
}
3540
}
3641
}
3742

43+
/**
44+
* Change multiselect custom fields from VARCHAR(191) to TEXT.
45+
*/
3846
public function down(Schema $schema): void
3947
{
40-
// Change multiselect custom fields from VARCHAR(255) to TEXT
41-
$qb = $this->connection->createQueryBuilder();
42-
$qb->select('lf.alias')
43-
->from($this->prefix.'lead_fields', 'lf')
44-
->where(
45-
$qb->expr()->eq('lf.type', $qb->expr()->literal('multiselect'))
46-
);
47-
$multiselectFields = $qb->execute()->fetchAll();
48-
49-
$leadsTable = $schema->getTable($this->prefix.'leads');
48+
$multiselectFields = $this->getMultiselectFields();
49+
$leadsTable = $schema->getTable($this->prefix.self::LEADS_TABLE);
50+
$changeStatements = [];
5051

5152
if (!empty($multiselectFields)) {
5253
foreach ($multiselectFields as $field) {
5354
if ($leadsTable->hasColumn($field['alias'])) {
54-
$this->addSql("ALTER TABLE `{$this->prefix}leads` CHANGE `{$field['alias']}` `{$field['alias']}` VARCHAR(255)");
55+
$changeStatements[] = "CHANGE `{$field['alias']}` `{$field['alias']}` VARCHAR(191)";
5556
if (!$leadsTable->hasIndex("{$this->prefix}{$field['alias']}_search")) {
56-
$this->addSql("CREATE INDEX `{$this->prefix}{$field['alias']}_search` ON `{$this->prefix}leads`(`{$field['alias']}`)");
57+
$changeStatements[] = "ADD INDEX `{$this->prefix}{$field['alias']}_search` (`{$field['alias']}`)";
5758
}
5859
}
5960
}
61+
62+
if (!empty($changeStatements)) {
63+
$this->addSql('ALTER TABLE `'.$this->prefix.self::LEADS_TABLE.'` '.implode(', ', $changeStatements));
64+
}
6065
}
6166
}
67+
68+
/**
69+
* @return array<array<string, mixed>>
70+
*/
71+
private function getMultiselectFields(): array
72+
{
73+
$qb = $this->connection->createQueryBuilder();
74+
$qb->select('lf.alias')
75+
->from($this->prefix.self::LEAD_FIELDS_TABLE, 'lf')
76+
->where(
77+
$qb->expr()->eq('lf.type', $qb->expr()->literal('multiselect'))
78+
);
79+
80+
return $qb->execute()->fetchAll();
81+
}
6282
}

migrations/Version20191017140848.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Version20191017140848 extends AbstractMauticMigration
1919
public function preUp(Schema $schema): void
2020
{
2121
$smsStatsTable = $schema->getTable(MAUTIC_TABLE_PREFIX.'sms_message_stats');
22-
if ($smsStatsTable->hasColumn('is_failed') && $smsStatsTable->hasColumn('details')) {
22+
if ($smsStatsTable->hasColumn('is_failed') && $smsStatsTable->hasColumn('details') && $smsStatsTable->hasIndex($this->prefix.'stat_sms_failed_search')) {
2323
throw new SkipMigration('Schema includes this migration');
2424
}
2525
}
@@ -33,6 +33,9 @@ public function up(Schema $schema): void
3333
if (!$smsStatsTable->hasColumn('is_failed')) {
3434
$this->addSql('ALTER TABLE '.$this->prefix.'sms_message_stats ADD is_failed TINYINT(1) DEFAULT NULL');
3535
$this->addSql("UPDATE {$this->prefix}sms_message_stats SET is_failed = '0'");
36+
}
37+
38+
if (!$smsStatsTable->hasIndex($this->prefix.'stat_sms_failed_search')) {
3639
$this->addSql("CREATE INDEX {$this->prefix}stat_sms_failed_search ON {$this->prefix}sms_message_stats (is_failed)");
3740
}
3841

migrations/Version20191219155630.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ public function getDescription(): string
2525
*/
2626
public function preUp(Schema $schema): void
2727
{
28-
if ($schema->hasTable($this->prefix.'sync_object_field_change_report')) {
28+
if ($schema->hasTable($this->prefix.'sync_object_field_change_report') && $schema->hasTable($this->prefix.'sync_object_mapping')) {
2929
throw new SkipMigration('Schema includes this migration');
3030
}
3131
}
3232

3333
public function up(Schema $schema): void
3434
{
35-
$this->addSql(
36-
"# Dump of table mautic_sync_object_field_change_report
35+
if (!$schema->hasTable($this->prefix.'sync_object_field_change_report')) {
36+
$this->addSql(
37+
"# Dump of table mautic_sync_object_field_change_report
3738
# ------------------------------------------------------------
3839
3940
CREATE TABLE `{$this->prefix}sync_object_field_change_report` (
@@ -50,10 +51,12 @@ public function up(Schema $schema): void
5051
KEY `{$this->prefix}integration_object_composite_key` (`integration`,`object_type`,`object_id`,`column_name`),
5152
KEY `{$this->prefix}integration_object_type_modification_composite_key` (`integration`,`object_type`,`modified_at`)
5253
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
53-
);
54+
);
55+
}
5456

55-
$this->addSql(
56-
"# Dump of table mautic_sync_object_mapping
57+
if (!$schema->hasTable($this->prefix.'sync_object_mapping')) {
58+
$this->addSql(
59+
"# Dump of table mautic_sync_object_mapping
5760
# ------------------------------------------------------------
5861
5962
CREATE TABLE `{$this->prefix}sync_object_mapping` (
@@ -75,7 +78,8 @@ public function up(Schema $schema): void
7578
KEY `{$this->prefix}integration_object` (`integration`,`integration_object_name`,`integration_object_id`,`integration_reference_id`),
7679
KEY `{$this->prefix}integration_reference` (`integration`,`integration_object_name`,`integration_reference_id`,`integration_object_id`)
7780
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
78-
);
81+
);
82+
}
7983
}
8084

8185
public function down(Schema $schema): void

migrations/Version20200227110431.php

+15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,28 @@
33
namespace Mautic\Migrations;
44

55
use Doctrine\DBAL\Schema\Schema;
6+
use Doctrine\DBAL\Schema\SchemaException;
7+
use Doctrine\Migrations\Exception\SkipMigration;
68
use Mautic\CoreBundle\Doctrine\AbstractMauticMigration;
79

810
/**
911
* Migration.
1012
*/
1113
class Version20200227110431 extends AbstractMauticMigration
1214
{
15+
/**
16+
* @throws SkipMigration
17+
* @throws SchemaException
18+
*/
19+
public function preUp(Schema $schema): void
20+
{
21+
$table = $schema->getTable($this->prefix.'lead_donotcontact');
22+
23+
if ($table->hasIndex($this->prefix.'dnc_channel_id_search')) {
24+
throw new SkipMigration('Schema includes this migration');
25+
}
26+
}
27+
1328
public function up(Schema $schema): void
1429
{
1530
$this->addSql('CREATE INDEX '.$this->prefix.'dnc_channel_id_search ON '.$this->prefix.'lead_donotcontact (channel_id)');

migrations/Version20200805185714.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,26 @@ final class Version20200805185714 extends AbstractMauticMigration
1616
*/
1717
public function preUp(Schema $schema): void
1818
{
19-
$tweetsTable = $schema->getTable($this->prefix.'tweets');
19+
$tweetsTable = $schema->getTable($this->prefix.'tweets');
2020

21-
if (!$tweetsTable->hasIndex('tweet_text_index')) {
21+
if (!$tweetsTable->hasIndex('tweet_text_index') && !$tweetsTable->hasIndex($this->prefix.'tweet_text_index') && 280 === $tweetsTable->getColumn('text')->getLength()) {
2222
throw new SkipMigration('Schema includes this migration');
2323
}
2424
}
2525

2626
public function up(Schema $schema): void
2727
{
28-
$this->addSql('DROP INDEX '.$this->prefix.'tweet_text_index ON '.$this->prefix.'tweets');
28+
$tweetsTable = $schema->getTable($this->prefix.'tweets');
29+
if ($tweetsTable->hasIndex('tweet_text_index')) {
30+
$this->addSql('DROP INDEX tweet_text_index ON '.$this->prefix.'tweets');
31+
}
32+
if ($tweetsTable->hasIndex($this->prefix.'tweet_text_index')) {
33+
$this->addSql('DROP INDEX '.$this->prefix.'tweet_text_index ON '.$this->prefix.'tweets');
34+
}
35+
2936
$this->addSql('ALTER TABLE '.$this->prefix.'tweets CHANGE text text VARCHAR(280)');
37+
if (280 !== $tweetsTable->getColumn('text')->getLength()) {
38+
$this->addSql('ALTER TABLE '.$this->prefix.'tweets CHANGE text text VARCHAR(280)');
39+
}
3040
}
3141
}

0 commit comments

Comments
 (0)