From 4bfc96e9779740505f229a292c35814f340d4570 Mon Sep 17 00:00:00 2001 From: Luka Trovic Date: Fri, 5 Jan 2024 10:55:33 +0100 Subject: [PATCH] fix: duplicate entry for key 'gf_versions_uniq_index' Signed-off-by: Luka Trovic --- lib/Versions/VersionsBackend.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/Versions/VersionsBackend.php b/lib/Versions/VersionsBackend.php index 6235c0639..fba74f738 100644 --- a/lib/Versions/VersionsBackend.php +++ b/lib/Versions/VersionsBackend.php @@ -30,6 +30,7 @@ use OCA\Files_Versions\Versions\IVersionBackend; use OCA\GroupFolders\Mount\GroupMountPoint; use OCA\GroupFolders\Mount\MountProvider; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Constants; use OCP\Files\File; @@ -78,14 +79,22 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array { return $versions; } - // Insert the entry in the DB for the current version. - $versionEntity = new GroupVersionEntity(); - $versionEntity->setFileId($file->getId()); - $versionEntity->setTimestamp($file->getMTime()); - $versionEntity->setSize($file->getSize()); - $versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype())); - $versionEntity->setDecodedMetadata([]); - $this->groupVersionsMapper->insert($versionEntity); + // Insert or update the entry in the DB for the current version. + try { + $versionEntity = $this->groupVersionsMapper->findVersionForFileId($file->getId(), $file->getMtime()); + $versionEntity->setSize($file->getSize()); + $versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype())); + $versionEntity->setDecodedMetadata([]); + $this->groupVersionsMapper->update($versionEntity); + } catch (DoesNotExistException) { + $versionEntity = new GroupVersionEntity(); + $versionEntity->setFileId($file->getId()); + $versionEntity->setTimestamp($file->getMTime()); + $versionEntity->setSize($file->getSize()); + $versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype())); + $versionEntity->setDecodedMetadata([]); + $this->groupVersionsMapper->insert($versionEntity); + } // Insert entries in the DB for existing versions. $versionsOnFS = $versionsFolder->getDirectoryListing();