Skip to content

Commit 114eeec

Browse files
come-ncAndyScherzinger
authored andcommitted
fix(files_versions): Catch constraint error on version insertion
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 247a1e7 commit 114eeec

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

apps/files_versions/lib/Versions/LegacyVersionsBackend.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,27 @@ public function createVersionEntity(File $file): void {
246246
$versionEntity->setSize($file->getSize());
247247
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
248248
$versionEntity->setMetadata([]);
249-
$this->versionsMapper->insert($versionEntity);
249+
250+
$tries = 1;
251+
while ($tries < 5) {
252+
try {
253+
$this->versionsMapper->insert($versionEntity);
254+
/* No errors, get out of the method */
255+
return;
256+
} catch (\OCP\DB\Exception $e) {
257+
if (!in_array($e->getReason(), [
258+
\OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
259+
\OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION,
260+
])
261+
) {
262+
throw $e;
263+
}
264+
/* Conflict with another version, increase mtime and try again */
265+
$versionEntity->setTimestamp($versionEntity->getTimestamp() + 1);
266+
$tries++;
267+
$this->logger->warning('Constraint violation while inserting version, retrying with increased timestamp', ['exception' => $e]);
268+
}
269+
}
250270
}
251271

252272
public function updateVersionEntity(File $sourceFile, int $revision, array $properties): void {

0 commit comments

Comments
 (0)