|  | 
|  | 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 OC\Core\Migrations; | 
|  | 11 | + | 
|  | 12 | +use Closure; | 
|  | 13 | +use OCP\DB\ISchemaWrapper; | 
|  | 14 | +use OCP\IDBConnection; | 
|  | 15 | +use OCP\Migration\IOutput; | 
|  | 16 | +use OCP\Migration\SimpleMigrationStep; | 
|  | 17 | +use Override; | 
|  | 18 | + | 
|  | 19 | +/** | 
|  | 20 | + * Make sure vcategory entries are unique per user and type | 
|  | 21 | + * This migration will clean up existing duplicates | 
|  | 22 | + * and add a unique constraint to prevent future duplicates. | 
|  | 23 | + */ | 
|  | 24 | +class Version30000Date20250731062008 extends SimpleMigrationStep { | 
|  | 25 | +	public function __construct( | 
|  | 26 | +		private IDBConnection $connection, | 
|  | 27 | +	) { | 
|  | 28 | +	} | 
|  | 29 | + | 
|  | 30 | +	/** | 
|  | 31 | +	 * @param IOutput $output | 
|  | 32 | +	 * @param Closure(): ISchemaWrapper $schemaClosure | 
|  | 33 | +	 * @param array $options | 
|  | 34 | +	 */ | 
|  | 35 | +	#[Override] | 
|  | 36 | +	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | 
|  | 37 | +		// Clean up duplicate categories before adding unique constraint | 
|  | 38 | +		$this->cleanupDuplicateCategories($output); | 
|  | 39 | +	} | 
|  | 40 | + | 
|  | 41 | +	/** | 
|  | 42 | +	 * Clean up duplicate categories | 
|  | 43 | +	 */ | 
|  | 44 | +	private function cleanupDuplicateCategories(IOutput $output) { | 
|  | 45 | +		$output->info('Starting cleanup of duplicate vcategory records...'); | 
|  | 46 | + | 
|  | 47 | +		// Find all categories, ordered to identify duplicates | 
|  | 48 | +		$qb = $this->connection->getQueryBuilder(); | 
|  | 49 | +		$qb->select('id', 'uid', 'type', 'category') | 
|  | 50 | +			->from('vcategory') | 
|  | 51 | +			->orderBy('uid') | 
|  | 52 | +			->addOrderBy('type') | 
|  | 53 | +			->addOrderBy('category') | 
|  | 54 | +			->addOrderBy('id'); | 
|  | 55 | + | 
|  | 56 | +		$result = $qb->executeQuery(); | 
|  | 57 | + | 
|  | 58 | +		$seen = []; | 
|  | 59 | +		$duplicateCount = 0; | 
|  | 60 | + | 
|  | 61 | +		while ($category = $result->fetch()) { | 
|  | 62 | +			$key = $category['uid'] . '|' . $category['type'] . '|' . $category['category']; | 
|  | 63 | +			$categoryId = (int) $category['id']; | 
|  | 64 | + | 
|  | 65 | +			if (!isset($seen[$key])) { | 
|  | 66 | +				// First occurrence - keep this one | 
|  | 67 | +				$seen[$key] = $categoryId; | 
|  | 68 | +				continue; | 
|  | 69 | +			} | 
|  | 70 | + | 
|  | 71 | +			// Duplicate found | 
|  | 72 | +			$keepId = $seen[$key]; | 
|  | 73 | +			$duplicateCount++; | 
|  | 74 | + | 
|  | 75 | +			$output->info("Found duplicate: keeping ID $keepId, removing ID $categoryId"); | 
|  | 76 | + | 
|  | 77 | +			// Update object references | 
|  | 78 | +			$updateQb = $this->connection->getQueryBuilder(); | 
|  | 79 | +			$updateQb->update('vcategory_to_object') | 
|  | 80 | +				->set('categoryid', $updateQb->createNamedParameter($keepId)) | 
|  | 81 | +				->where($updateQb->expr()->eq('categoryid', $updateQb->createNamedParameter($categoryId))); | 
|  | 82 | + | 
|  | 83 | +			$affectedRows = $updateQb->executeStatement(); | 
|  | 84 | +			if ($affectedRows > 0) { | 
|  | 85 | +				$output->info(" - Updated $affectedRows object references from category $categoryId to $keepId"); | 
|  | 86 | +			} | 
|  | 87 | + | 
|  | 88 | +			// Remove duplicate category record | 
|  | 89 | +			$deleteQb = $this->connection->getQueryBuilder(); | 
|  | 90 | +			$deleteQb->delete('vcategory') | 
|  | 91 | +				->where($deleteQb->expr()->eq('id', $deleteQb->createNamedParameter($categoryId))); | 
|  | 92 | + | 
|  | 93 | +			$deleteQb->executeStatement(); | 
|  | 94 | +			$output->info(" - Deleted duplicate category record ID $categoryId"); | 
|  | 95 | + | 
|  | 96 | +		} | 
|  | 97 | + | 
|  | 98 | +		$result->closeCursor(); | 
|  | 99 | + | 
|  | 100 | +		if ($duplicateCount === 0) { | 
|  | 101 | +			$output->info('No duplicate categories found'); | 
|  | 102 | +		} else { | 
|  | 103 | +			$output->info("Duplicate cleanup completed - processed $duplicateCount duplicates"); | 
|  | 104 | +		} | 
|  | 105 | +	} | 
|  | 106 | +} | 
0 commit comments