-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: Add repair step for deduplicating mounts #54014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+33
−0
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
826e95d
feat: Add repair step for deduplicating mounts
provokateurin f4c2f80
fixup! feat: Add repair step for deduplicating mounts
salmart-dev 0d849fe
fixup! fixup! feat: Add repair step for deduplicating mounts
salmart-dev 07ccfdc
fixup! feat: Add repair step for deduplicating mounts
salmart-dev 0294e5d
fixup! fixup! fixup! feat: Add repair step for deduplicating mounts
salmart-dev 4c0ea8a
fixup! fixup! feat: Add repair step for deduplicating mounts
provokateurin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: Add repair step for deduplicating mounts
Signed-off-by: provokateurin <[email protected]>
- Loading branch information
commit 826e95d9e0cfb1bf7a8b6a87000c8b9293db3ea0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
salmart-dev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $result->closeCursor(); | ||
|
|
||
| $this->connection->commit(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.