-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathDeduplicateMounts.php
More file actions
68 lines (56 loc) · 1.96 KB
/
DeduplicateMounts.php
File metadata and controls
68 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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();
}
}