|
| 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 | +namespace OC\Core\Migrations; |
| 10 | + |
| 11 | +use Closure; |
| 12 | +use OCP\DB\ISchemaWrapper; |
| 13 | +use OCP\DB\Types; |
| 14 | +use OCP\IDBConnection; |
| 15 | +use OCP\Migration\Attributes\AddIndex; |
| 16 | +use OCP\Migration\Attributes\CreateTable; |
| 17 | +use OCP\Migration\Attributes\IndexType; |
| 18 | +use OCP\Migration\Attributes\ModifyColumn; |
| 19 | +use OCP\Migration\IOutput; |
| 20 | +use OCP\Migration\SimpleMigrationStep; |
| 21 | + |
| 22 | +/** |
| 23 | + * Migrate away from auto-increment |
| 24 | + */ |
| 25 | +#[AddIndex(table: 'preview_locations', type: IndexType::UNIQUE)] |
| 26 | +#[ModifyColumn(table: 'preview_locations', name: 'id', description: 'Remove auto-increment')] |
| 27 | +#[ModifyColumn(table: 'previews', name: 'id', description: 'Remove auto-increment')] |
| 28 | +#[ModifyColumn(table: 'preview_versions', name: 'id', description: 'Remove auto-increment')] |
| 29 | +class Version33000Date20251023110529 extends SimpleMigrationStep { |
| 30 | + public function __construct( |
| 31 | + private readonly IDBConnection $connection, |
| 32 | + ) {} |
| 33 | + |
| 34 | + /** |
| 35 | + * @param Closure(): ISchemaWrapper $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 36 | + */ |
| 37 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 38 | + /** @var ISchemaWrapper $schema */ |
| 39 | + $schema = $schemaClosure(); |
| 40 | + |
| 41 | + if ($schema->hasTable('preview_locations')) { |
| 42 | + $table = $schema->getTable('preview_locations'); |
| 43 | + $table->modifyColumn('id', ['autoincrement' => false]); |
| 44 | + $table->addUniqueIndex(['bucket_name', 'object_store_name'], 'unique_bucket_object_store_name'); |
| 45 | + } |
| 46 | + |
| 47 | + if ($schema->hasTable('preview_versions')) { |
| 48 | + $table = $schema->getTable('preview_versions'); |
| 49 | + $table->modifyColumn('id', ['autoincrement' => false]); |
| 50 | + } |
| 51 | + |
| 52 | + if ($schema->hasTable('previews')) { |
| 53 | + $table = $schema->getTable('previews'); |
| 54 | + $table->modifyColumn('id', ['autoincrement' => false]); |
| 55 | + } |
| 56 | + |
| 57 | + return $schema; |
| 58 | + } |
| 59 | + |
| 60 | + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
| 61 | + // This code should never be run on a production instance but this might be needed on daily/git version. |
| 62 | + $qb = $this->connection->getQueryBuilder(); |
| 63 | + $qb->select('*') |
| 64 | + ->from('preview_locations') |
| 65 | + ->groupBy('bucket_name', 'object_store_name') |
| 66 | + ->having('COUNT(*) > 1'); |
| 67 | + |
| 68 | + $result = $qb->executeQuery(); |
| 69 | + while ($row = $result->fetch()) { |
| 70 | + // Iterate over all the rows with duplicated rows |
| 71 | + $id = $row['id']; |
| 72 | + |
| 73 | + $qb = $this->connection->getQueryBuilder(); |
| 74 | + $qb->select('id') |
| 75 | + ->from('preview_locations') |
| 76 | + ->where($qb->expr()->eq('bucket_name', $qb->createNamedParameter($row['bucket_name']))) |
| 77 | + ->andWhere($qb->expr()->eq('object_store_name', $qb->createNamedParameter($row['object_store_name']))) |
| 78 | + ->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($row['id']))); |
| 79 | + |
| 80 | + $result = $qb->executeQuery(); |
| 81 | + while ($row = $result->fetch()) { |
| 82 | + // Update previews entries to the now de-duplicated id |
| 83 | + $qb = $this->connection->getQueryBuilder(); |
| 84 | + $qb->update('previews') |
| 85 | + ->set('location_id', $qb->createNamedParameter($id)) |
| 86 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($row['id']))); |
| 87 | + $qb->executeStatement(); |
| 88 | + |
| 89 | + $qb = $this->connection->getQueryBuilder(); |
| 90 | + $qb->delete('preview_locations') |
| 91 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($row['id']))); |
| 92 | + $qb->executeStatement(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments