Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public function __construct() {
'systag_by_objectid',
['objectid']
);

$event->addMissingUniqueIndex(
'vcategory',
'unique_category_per_user',
['uid', 'type', 'category']
);
});

$eventDispatcher->addListener(AddMissingPrimaryKeyEvent::class, function (AddMissingPrimaryKeyEvent $event) {
Expand Down
1 change: 1 addition & 0 deletions core/Migrations/Version13000Date20170718121200.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
$table->addIndex(['uid'], 'uid_index');
$table->addIndex(['type'], 'type_index');
$table->addIndex(['category'], 'category_index');
$table->addUniqueIndex(['uid', 'type', 'category'], 'unique_category_per_user');
}

if (!$schema->hasTable('vcategory_to_object')) {
Expand Down
106 changes: 106 additions & 0 deletions core/Migrations/Version30000Date20250731062008.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Migrations;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;

/**
* Make sure vcategory entries are unique per user and type
* This migration will clean up existing duplicates
* and add a unique constraint to prevent future duplicates.
*/
class Version30000Date20250731062008 extends SimpleMigrationStep {
public function __construct(
private IDBConnection $connection,
) {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
#[Override]
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
// Clean up duplicate categories before adding unique constraint
$this->cleanupDuplicateCategories($output);
}

/**
* Clean up duplicate categories
*/
private function cleanupDuplicateCategories(IOutput $output) {
$output->info('Starting cleanup of duplicate vcategory records...');

// Find all categories, ordered to identify duplicates
$qb = $this->connection->getQueryBuilder();
$qb->select('id', 'uid', 'type', 'category')
->from('vcategory')
->orderBy('uid')
->addOrderBy('type')
->addOrderBy('category')
->addOrderBy('id');

$result = $qb->executeQuery();

$seen = [];
$duplicateCount = 0;

while ($category = $result->fetch()) {
$key = $category['uid'] . '|' . $category['type'] . '|' . $category['category'];
$categoryId = (int) $category['id'];

if (!isset($seen[$key])) {
// First occurrence - keep this one
$seen[$key] = $categoryId;
continue;
}

// Duplicate found
$keepId = $seen[$key];
$duplicateCount++;

$output->info("Found duplicate: keeping ID $keepId, removing ID $categoryId");

// Update object references
$updateQb = $this->connection->getQueryBuilder();
$updateQb->update('vcategory_to_object')
->set('categoryid', $updateQb->createNamedParameter($keepId))
->where($updateQb->expr()->eq('categoryid', $updateQb->createNamedParameter($categoryId)));

$affectedRows = $updateQb->executeStatement();
if ($affectedRows > 0) {
$output->info(" - Updated $affectedRows object references from category $categoryId to $keepId");
}

// Remove duplicate category record
$deleteQb = $this->connection->getQueryBuilder();
$deleteQb->delete('vcategory')
->where($deleteQb->expr()->eq('id', $deleteQb->createNamedParameter($categoryId)));

$deleteQb->executeStatement();
$output->info(" - Deleted duplicate category record ID $categoryId");

}

$result->closeCursor();

if ($duplicateCount === 0) {
$output->info('No duplicate categories found');
} else {
$output->info("Duplicate cleanup completed - processed $duplicateCount duplicates");
}
}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@
'OC\\Core\\Migrations\\Version30000Date20240814180800' => $baseDir . '/core/Migrations/Version30000Date20240814180800.php',
'OC\\Core\\Migrations\\Version30000Date20240815080800' => $baseDir . '/core/Migrations/Version30000Date20240815080800.php',
'OC\\Core\\Migrations\\Version30000Date20240906095113' => $baseDir . '/core/Migrations/Version30000Date20240906095113.php',
'OC\\Core\\Migrations\\Version30000Date20250731062008' => $baseDir . '/core/Migrations/Version30000Date20250731062008.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => $baseDir . '/core/ResponseDefinitions.php',
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Migrations\\Version30000Date20240814180800' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240814180800.php',
'OC\\Core\\Migrations\\Version30000Date20240815080800' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240815080800.php',
'OC\\Core\\Migrations\\Version30000Date20240906095113' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240906095113.php',
'OC\\Core\\Migrations\\Version30000Date20250731062008' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20250731062008.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => __DIR__ . '/../../..' . '/core/ResponseDefinitions.php',
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
Expand Down
1 change: 0 additions & 1 deletion lib/private/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ public function add(string $name) {
return false;
}
if ($this->userHasTag($name, $this->user)) {
// TODO use unique db properties instead of an additional check
$this->logger->debug(__METHOD__ . ' Tag with name already exists', ['app' => 'core']);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level
// when updating major/minor version number.

$OC_Version = [30, 0, 13, 1];
$OC_Version = [30, 0, 13, 2];

// The human-readable string
$OC_VersionString = '30.0.13';
Expand Down
Loading