|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Text\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCP\DB\ISchemaWrapper; |
| 14 | +use OCP\IDBConnection; |
| 15 | +use OCP\Migration\Attributes\AddIndex; |
| 16 | +use OCP\Migration\Attributes\IndexType; |
| 17 | +use OCP\Migration\IOutput; |
| 18 | +use OCP\Migration\SimpleMigrationStep; |
| 19 | +use Override; |
| 20 | + |
| 21 | +#[AddIndex(table: 'text_steps', type: IndexType::INDEX, description: 'Optimize cleanup job')] |
| 22 | +class Version080000Date20260331132113 extends SimpleMigrationStep { |
| 23 | + private bool $addIndexLater = false; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly IDBConnection $connection, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + #[Override] |
| 31 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 32 | + /** @var ISchemaWrapper $schema */ |
| 33 | + $schema = $schemaClosure(); |
| 34 | + |
| 35 | + $table = $schema->getTable('text_steps'); |
| 36 | + if (!$table->hasIndex('text_steps_doc_id_id_index')) { |
| 37 | + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MARIADB |
| 38 | + || $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) { |
| 39 | + $table->addIndex(['document_id', 'id'], 'text_steps_doc_id_id_index'); |
| 40 | + } else { |
| 41 | + $this->addIndexLater = true; |
| 42 | + } |
| 43 | + return $schema; |
| 44 | + } |
| 45 | + |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + #[Override] |
| 50 | + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 51 | + /** @var ISchemaWrapper $schema */ |
| 52 | + $schema = $schemaClosure(); |
| 53 | + if ($this->addIndexLater) { |
| 54 | + // We need to add the index with a DESC manually |
| 55 | + // See https://github.com/doctrine/orm/issues/8128 |
| 56 | + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { |
| 57 | + $stm = $this->connection->prepare('CREATE INDEX CONCURRENTLY text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC);'); |
| 58 | + } elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { |
| 59 | + $stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC) ONLINE;'); |
| 60 | + } elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) { |
| 61 | + $stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC);'); |
| 62 | + } else { |
| 63 | + throw new \RuntimeException('Unsupported platform'); |
| 64 | + } |
| 65 | + $stm->execute(); |
| 66 | + return $schema; |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | +} |
0 commit comments