File tree Expand file tree Collapse file tree
apps/files_versions/lib/Versions Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments