Skip to content

Commit b5e3508

Browse files
authored
Merge pull request #38933 from nextcloud/orphaned-entries-filecache-extended
feat: remove orphaned entries from filecache_extended
2 parents 10fc78a + 1d34f0a commit b5e3508

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

apps/files/lib/Command/DeleteOrphanedFiles.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
*/
2525
namespace OCA\Files\Command;
2626

27+
use OCP\DB\QueryBuilder\IQueryBuilder;
2728
use OCP\IDBConnection;
2829
use Symfony\Component\Console\Command\Command;
2930
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
3032
use Symfony\Component\Console\Output\OutputInterface;
3133

3234
/**
@@ -44,7 +46,8 @@ public function __construct(
4446
protected function configure(): void {
4547
$this
4648
->setName('files:cleanup')
47-
->setDescription('cleanup filecache');
49+
->setDescription('cleanup filecache')
50+
->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended');
4851
}
4952

5053
public function execute(InputInterface $input, OutputInterface $output): int {
@@ -75,11 +78,44 @@ public function execute(InputInterface $input, OutputInterface $output): int {
7578

7679
$output->writeln("$deletedEntries orphaned file cache entries deleted");
7780

81+
if (!$input->getOption('skip-filecache-extended')) {
82+
$deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended();
83+
$output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
84+
}
85+
86+
7887
$deletedMounts = $this->cleanupOrphanedMounts();
7988
$output->writeln("$deletedMounts orphaned mount entries deleted");
8089
return self::SUCCESS;
8190
}
8291

92+
private function cleanupOrphanedFileCacheExtended(): int {
93+
$deletedEntries = 0;
94+
95+
$query = $this->connection->getQueryBuilder();
96+
$query->select('fce.fileid')
97+
->from('filecache_extended', 'fce')
98+
->leftJoin('fce', 'filecache', 'fc', $query->expr()->eq('fce.fileid', 'fc.fileid'))
99+
->where($query->expr()->isNull('fc.fileid'))
100+
->setMaxResults(self::CHUNK_SIZE);
101+
102+
$deleteQuery = $this->connection->getQueryBuilder();
103+
$deleteQuery->delete('filecache_extended')
104+
->where($deleteQuery->expr()->in('fileid', $deleteQuery->createParameter('idsToDelete')));
105+
106+
$result = $query->executeQuery();
107+
while ($result->rowCount() > 0) {
108+
$idsToDelete = $result->fetchAll(\PDO::FETCH_COLUMN);
109+
110+
$deleteQuery->setParameter('idsToDelete', $idsToDelete, IQueryBuilder::PARAM_INT_ARRAY);
111+
$deletedEntries += $deleteQuery->executeStatement();
112+
113+
$result = $query->executeQuery();
114+
}
115+
116+
return $deletedEntries;
117+
}
118+
83119
private function cleanupOrphanedMounts(): int {
84120
$deletedEntries = 0;
85121

apps/files/tests/Command/DeleteOrphanedFilesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ public function testClearFiles() {
132132

133133
// parent folder, `files`, ´test` and `welcome.txt` => 4 elements
134134
$output
135-
->expects($this->exactly(2))
135+
->expects($this->exactly(3))
136136
->method('writeln')
137137
->withConsecutive(
138138
['3 orphaned file cache entries deleted'],
139+
['0 orphaned file cache extended entries deleted'],
139140
['1 orphaned mount entries deleted'],
140141
);
141142

0 commit comments

Comments
 (0)