Skip to content

Commit

Permalink
Merge pull request #46887 from nextcloud/fix/versions-catch-insertion…
Browse files Browse the repository at this point in the history
…-error
  • Loading branch information
skjnldsv authored Aug 14, 2024
2 parents dbc2e9c + 30a2e8f commit 1fa9d39
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,27 @@ public function createVersionEntity(File $file): void {
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);

$tries = 1;
while ($tries < 5) {
try {
$this->versionsMapper->insert($versionEntity);
/* No errors, get out of the method */
return;
} catch (\OCP\DB\Exception $e) {
if (!in_array($e->getReason(), [
\OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
\OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION,
])
) {
throw $e;
}
/* Conflict with another version, increase mtime and try again */
$versionEntity->setTimestamp($versionEntity->getTimestamp() + 1);
$tries++;
$this->logger->warning('Constraint violation while inserting version, retrying with increased timestamp', ['exception' => $e]);
}
}
}

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

0 comments on commit 1fa9d39

Please sign in to comment.