Check indexes exist when adding them

[MAILPOET-5364]
This commit is contained in:
alex-mailpoet
2023-05-30 20:14:43 +03:00
committed by David Remer
parent bcf57de500
commit e38fd951d1
2 changed files with 24 additions and 5 deletions

View File

@@ -8,10 +8,17 @@ use MailPoet\Migrator\Migration;
class Migration_20230503_210945 extends Migration {
public function run(): void {
$subscribersTable = $this->getTableName(SubscriberEntity::class);
if (!$this->indexExists($subscribersTable, 'first_name')) {
$this->connection->executeQuery(
"ALTER TABLE `{$subscribersTable}`
ADD INDEX `first_name` (`first_name`(10))"
);
}
if (!$this->indexExists($subscribersTable, 'last_name')) {
$this->connection->executeQuery(
"ALTER TABLE `{$subscribersTable}`
ADD INDEX `first_name` (`first_name`(10)),
ADD INDEX `last_name` (`last_name`(10))"
);
}
}
}

View File

@@ -53,4 +53,16 @@ abstract class Migration {
AND column_name = ?
", [Env::$dbName, $tableName, $columnName])->fetchOne() !== false;
}
protected function indexExists(string $tableName, string $indexName): bool {
// We had a problem with the dbName value in ENV for some customers, because it doesn't match DB name in information schema.
// So we decided to use the DATABASE() value instead.
return $this->connection->executeQuery("
SELECT 1
FROM information_schema.statistics
WHERE table_schema = COALESCE(DATABASE(), ?)
AND table_name = ?
AND index_name = ?
", [Env::$dbName, $tableName, $indexName])->fetchOne() !== false;
}
}