|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Mail\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCP\DB\ISchemaWrapper; |
| 14 | +use OCP\Migration\IOutput; |
| 15 | +use OCP\Migration\SimpleMigrationStep; |
| 16 | +use Override; |
| 17 | + |
| 18 | +class Version5006Date20251023191023 extends SimpleMigrationStep { |
| 19 | + |
| 20 | + /** |
| 21 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 22 | + */ |
| 23 | + #[Override] |
| 24 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 25 | + $schema = $schemaClosure(); |
| 26 | + |
| 27 | + $mailboxes = $schema->getTable('mail_mailboxes'); |
| 28 | + |
| 29 | + /** |
| 30 | + * The migration "Version0161Date20190902103701" created a unique index for account_id and name without |
| 31 | + * specifying a name. Unfortunately, this resulted in different index names depending on the table |
| 32 | + * prefix, which means we now have to loop through the indexes to find the correct one. |
| 33 | + * |
| 34 | + * The index on account_id and name were supposed to be dropped in "Version3500Date20231115184458", |
| 35 | + * but this did not work on every setup due to the name mismatch caused by different table prefixes. |
| 36 | + * |
| 37 | + * On MySQL or MariaDB versions before 10.5, changing the length of the name column to 1024 fails if the |
| 38 | + * index on account_id and name still exists, with the error "Specified key was too long; max key length is 3072 bytes." |
| 39 | + * However, this change works on MariaDB 10.5 or newer. |
| 40 | + * |
| 41 | + * The reason is that MariaDB automatically converts a unique index using btree to hash if the key exceeds |
| 42 | + * the maximum length and is supported by the storage engine: https://mariadb.com/docs/server/mariadb-quickstart-guides/mariadb-indexes-guide#unique-index |
| 43 | + * |
| 44 | + * This means that on setups with a different table prefix using MariaDB 10.5, the index on account_id and name |
| 45 | + * might still exist. Since we don't need it, we will make another attempt to drop it here. |
| 46 | + * |
| 47 | + * @see \OCA\Mail\Migration\Version0161Date20190902103701::changeSchema |
| 48 | + * @see \OCA\Mail\Migration\Version3500Date20231115184458::changeSchema |
| 49 | + * @see \OCA\Mail\Migration\Version5006Date20250927130132::changeSchema |
| 50 | + * @see \OCA\Mail\Migration\Version5006Date20251015082003::changeSchema |
| 51 | + */ |
| 52 | + foreach ($mailboxes->getIndexes() as $index) { |
| 53 | + if ($index->isUnique() && $index->spansColumns(['account_id', 'name'])) { |
| 54 | + $mailboxes->dropIndex($index->getName()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return $schema; |
| 59 | + } |
| 60 | +} |
0 commit comments