|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Monsieur Biz' Rich Editor plugin for Sylius. |
| 5 | + * |
| 6 | + * (c) Monsieur Biz <sylius@monsieurbiz.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE.txt |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace App\Migrations; |
| 15 | + |
| 16 | +use Doctrine\DBAL\Schema\Schema; |
| 17 | +use Doctrine\Migrations\AbstractMigration; |
| 18 | + |
| 19 | +/** |
| 20 | + * Auto-generated Migration: Please modify to your needs! |
| 21 | + */ |
| 22 | +final class Version20220621100423 extends AbstractMigration |
| 23 | +{ |
| 24 | + public function getDescription(): string |
| 25 | + { |
| 26 | + return 'Change length of messenger_messages.queue_name column and create missing indexes, cf https://github.com/Sylius/Sylius/issues/13838'; |
| 27 | + } |
| 28 | + |
| 29 | + public function up(Schema $schema): void |
| 30 | + { |
| 31 | + if ($schema->hasTable('messenger_messages')) { |
| 32 | + $this->addSql('ALTER TABLE messenger_messages CHANGE queue_name queue_name VARCHAR(190) NOT NULL'); |
| 33 | + if (!$this->tableHasIndexOnColumns('messenger_messages', ['queue_name'])) { |
| 34 | + $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)'); |
| 35 | + } |
| 36 | + if (!$this->tableHasIndexOnColumns('messenger_messages', ['available_at'])) { |
| 37 | + $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)'); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public function down(Schema $schema): void |
| 43 | + { |
| 44 | + if ($schema->hasTable('messenger_messages')) { |
| 45 | + if ($this->tableHasIndexOnColumns('messenger_messages', ['queue_name'])) { |
| 46 | + $this->addSql('DROP INDEX IDX_75EA56E0FB7336F0 ON messenger_messages'); |
| 47 | + } |
| 48 | + if ($this->tableHasIndexOnColumns('messenger_messages', ['available_at'])) { |
| 49 | + $this->addSql('DROP INDEX IDX_75EA56E0E3BD61CE ON messenger_messages'); |
| 50 | + } |
| 51 | + $this->addSql('ALTER TABLE messenger_messages CHANGE queue_name queue_name VARCHAR(255) CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_unicode_ci`'); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private function tableHasIndexOnColumns(string $table, array $columns): bool |
| 56 | + { |
| 57 | + $indexes = $this->connection->getSchemaManager()->listTableIndexes($table); |
| 58 | + foreach ($indexes as $index) { |
| 59 | + foreach ($index->getColumns() as $column) { |
| 60 | + // Another column exists in index |
| 61 | + if (($key = array_search($column, $columns, true)) === false) { |
| 62 | + continue 2; |
| 63 | + } |
| 64 | + unset($columns[$key]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Table has all columns in index if the given array in empty at the end |
| 69 | + return empty($columns); |
| 70 | + } |
| 71 | +} |
0 commit comments