Skip to content
Closed
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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,7 @@
'OC\\Repair\\ClearGeneratedAvatarCache' => $baseDir . '/lib/private/Repair/ClearGeneratedAvatarCache.php',
'OC\\Repair\\ClearGeneratedAvatarCacheJob' => $baseDir . '/lib/private/Repair/ClearGeneratedAvatarCacheJob.php',
'OC\\Repair\\Collation' => $baseDir . '/lib/private/Repair/Collation.php',
'OC\\Repair\\DeduplicateMounts' => $baseDir . '/lib/private/Repair/DeduplicateMounts.php',
'OC\\Repair\\Events\\RepairAdvanceEvent' => $baseDir . '/lib/private/Repair/Events/RepairAdvanceEvent.php',
'OC\\Repair\\Events\\RepairErrorEvent' => $baseDir . '/lib/private/Repair/Events/RepairErrorEvent.php',
'OC\\Repair\\Events\\RepairFinishEvent' => $baseDir . '/lib/private/Repair/Events/RepairFinishEvent.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 @@ -1938,6 +1938,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair\\ClearGeneratedAvatarCache' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearGeneratedAvatarCache.php',
'OC\\Repair\\ClearGeneratedAvatarCacheJob' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearGeneratedAvatarCacheJob.php',
'OC\\Repair\\Collation' => __DIR__ . '/../../..' . '/lib/private/Repair/Collation.php',
'OC\\Repair\\DeduplicateMounts' => __DIR__ . '/../../..' . '/lib/private/Repair/DeduplicateMounts.php',
'OC\\Repair\\Events\\RepairAdvanceEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairAdvanceEvent.php',
'OC\\Repair\\Events\\RepairErrorEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairErrorEvent.php',
'OC\\Repair\\Events\\RepairFinishEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairFinishEvent.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OC\Repair\ClearFrontendCaches;
use OC\Repair\ClearGeneratedAvatarCache;
use OC\Repair\Collation;
use OC\Repair\DeduplicateMounts;
use OC\Repair\Events\RepairAdvanceEvent;
use OC\Repair\Events\RepairErrorEvent;
use OC\Repair\Events\RepairFinishEvent;
Expand Down Expand Up @@ -214,6 +215,7 @@ public static function getExpensiveRepairSteps() {
\OCP\Server::get(IDBConnection::class)
),
\OCP\Server::get(DeleteSchedulingObjects::class),
\OCP\Server::get(DeduplicateMounts::class),
];
}

Expand Down
68 changes: 68 additions & 0 deletions lib/private/Repair/DeduplicateMounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

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

namespace OC\Repair;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class DeduplicateMounts implements IRepairStep {
public function __construct(
private readonly IDBConnection $connection,
private readonly IConfig $config,
) {
}

public function getName(): string {
return 'Deduplicate mounts';
}

public function run(IOutput $output): void {
$threshold = $this->config->getSystemValueInt('repair_duplicate_mounts_threshold', 10);
if ($threshold < 1) {
$threshold = 1;
}

$this->connection->beginTransaction();

$selectQuery = $this->connection->getQueryBuilder();
$selectQuery
->select('root_id', 'user_id', 'mount_point')
->selectAlias($selectQuery->func()->min('id'), 'min_id')
->from('mounts')
->groupBy('root_id', 'user_id', 'mount_point')
->having($selectQuery->expr()->gt($selectQuery->func()->count('*'), $selectQuery->createNamedParameter($threshold, IQueryBuilder::PARAM_INT)));

$deleteQuery = $this->connection->getQueryBuilder();
$deleteQuery
->delete('mounts')
->where(
$deleteQuery->expr()->neq('id', $deleteQuery->createParameter('id')),
$deleteQuery->expr()->eq('root_id', $deleteQuery->createParameter('root_id')),
$deleteQuery->expr()->eq('user_id', $deleteQuery->createParameter('user_id')),
$deleteQuery->expr()->eq('mount_point', $deleteQuery->createParameter('mount_point')),
);

$result = $selectQuery->executeQuery();
while ($row = $result->fetch()) {
$deleteQuery
->setParameter('id', $row['min_id'])
->setParameter('root_id', $row['root_id'])
->setParameter('user_id', $row['user_id'])
->setParameter('mount_point', $row['mount_point'])
->executeStatement();
}
$result->closeCursor();

$this->connection->commit();
}
}
158 changes: 158 additions & 0 deletions tests/lib/Repair/DeduplicateMountsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

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

namespace Test\Repair;

use OC\Repair\DeduplicateMounts;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Server;
use Test\TestCase;

/**
* @group DB
*
* @see DeduplicateMounts
*/
class DeduplicateMountsTest extends TestCase {

private DeduplicateMounts $repair;
private IDBConnection $connection;
private IConfig $config;

protected function setUp(): void {
parent::setUp();
$this->connection = Server::get(IDBConnection::class);
$this->deleteAllMounts();

$this->config = $this->createMock(IConfig::class);
$this->repair = new DeduplicateMounts($this->connection, $this->config);
}

protected function tearDown(): void {
$this->deleteAllMounts();

parent::tearDown();
}

protected function deleteAllMounts(): void {
$this->connection->getQueryBuilder()->delete('mounts')->executeStatement();
}

public function testDeduplicateMounts(): void {
$rows = [
// Original mount
[
'storage_id' => 1,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
// Duplicate mount 1
[
'storage_id' => 2,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
// Duplicate mount 2
[
'storage_id' => 3,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
// Different root_id
[
'storage_id' => 4,
'root_id' => 2,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
// Different user_id
[
'storage_id' => 5,
'root_id' => 1,
'user_id' => 'user2',
'mount_point' => '/user1/files/1.txt/',
],
// Different mount_point
[
'storage_id' => 6,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/2.txt/',
],
];

$qb = $this->connection->getQueryBuilder();
$qb->insert('mounts')
->values([
'storage_id' => $qb->createParameter('storage_id'),
'root_id' => $qb->createParameter('root_id'),
'user_id' => $qb->createParameter('user_id'),
'mount_point' => $qb->createParameter('mount_point'),
]);

foreach ($rows as $row) {
$qb
->setParameter('storage_id', $row['storage_id'], IQueryBuilder::PARAM_INT)
->setParameter('root_id', $row['root_id'], IQueryBuilder::PARAM_INT)
->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR)
->setParameter('mount_point', $row['mount_point'], IQueryBuilder::PARAM_STR)
->executeStatement();
}

$this->config
->expects($this->once())
->method('getSystemValueInt')
->with('repair_duplicate_mounts_threshold', 10)
->willReturn(1);

$output = $this->createMock(IOutput::class);
$this->repair->run($output);

$result = $this->connection->getQueryBuilder()
->select('storage_id', 'root_id', 'user_id', 'mount_point')
->from('mounts')
->orderBy('storage_id', 'ASC')
->executeQuery();

$this->assertEquals([
[
'storage_id' => 1,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
// Duplicate mount 1 is removed
// Duplicate mount 2 is removed
[
'storage_id' => 4,
'root_id' => 2,
'user_id' => 'user1',
'mount_point' => '/user1/files/1.txt/',
],
[
'storage_id' => 5,
'root_id' => 1,
'user_id' => 'user2',
'mount_point' => '/user1/files/1.txt/',
],
[
'storage_id' => 6,
'root_id' => 1,
'user_id' => 'user1',
'mount_point' => '/user1/files/2.txt/',
],
], $result->fetchAll());

$result->closeCursor();
}
}
Loading