From f8346f43f136a060e6938f157338570b60363e97 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 26 Jan 2021 20:24:30 +0100 Subject: [PATCH] handle the cache where a cache entry with the correct path has already been recreated Signed-off-by: Robin Appelman --- apps/files/lib/Command/RepairTree.php | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/apps/files/lib/Command/RepairTree.php b/apps/files/lib/Command/RepairTree.php index d021b59722..1e4607dbe6 100644 --- a/apps/files/lib/Command/RepairTree.php +++ b/apps/files/lib/Command/RepairTree.php @@ -69,12 +69,18 @@ class RepairTree extends Command { $output->writeln("Path of file ${row['fileid']} is ${row['path']} but should be ${row['parent_path']}/${row['name']} based on it's parent", OutputInterface::VERBOSITY_VERBOSE); if ($fix) { - $query->setParameters([ - 'fileid' => $row['fileid'], - 'path' => $row['parent_path'] . '/' . $row['name'], - 'storage' => $row['parent_storage'], - ]); - $query->execute(); + $fileId = $this->getFileId($row['parent_storage'], $row['parent_path'] . '/' . $row['name']); + if ($fileId > 0) { + $output->writeln("Cache entry has already be recreated with id $fileId, deleting instead"); + $this->deleteById($row['fileid']); + } else { + $query->setParameters([ + 'fileid' => $row['fileid'], + 'path' => $row['parent_path'] . '/' . $row['name'], + 'storage' => $row['parent_storage'], + ]); + $query->execute(); + } } } @@ -85,6 +91,22 @@ class RepairTree extends Command { return 0; } + private function getFileId(int $storage, string $path) { + $query = $this->connection->getQueryBuilder(); + $query->select('fileid') + ->from('filecache') + ->where($query->expr()->eq('storage', $query->createNamedParameter($storage))) + ->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($path)))); + return $query->execute()->fetch(\PDO::FETCH_COLUMN); + } + + private function deleteById(int $fileId) { + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId))); + $query->execute(); + } + private function findBrokenTreeBits(): array { $query = $this->connection->getQueryBuilder();