Skip to content

Commit af74850

Browse files
authored
Merge pull request #7737 from nextcloud/backport/7703/stable32
[stable32] fix(DocumentService): Return 200 steps before saved version in SyncStep2
2 parents 643d89b + 20f39aa commit af74850

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Db/StepMapper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ public function getLatestVersion(int $documentId): ?int {
5656
return $data['id'];
5757
}
5858

59+
public function getBeforeVersion(int $documentId, int $version, int $offset): int {
60+
$qb = $this->db->getQueryBuilder();
61+
$result = $qb->select('id')
62+
->from($this->getTableName())
63+
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
64+
->andWhere($qb->expr()->lt('id', $qb->createNamedParameter($version)))
65+
->setMaxResults($offset)
66+
->orderBy('id', 'DESC')
67+
->executeQuery();
68+
69+
$rows = $result->fetchAll();
70+
$data = end($rows);
71+
if ($data === false) {
72+
return $version;
73+
}
74+
75+
return $data['id'];
76+
}
77+
5978
public function deleteAll(int $documentId): void {
6079
$qb = $this->db->getQueryBuilder();
6180
$qb->delete($this->getTableName())

lib/Service/DocumentService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ public function addStep(Document $document, Session $session, array $steps, int
239239
$stateFile = $this->getStateFile($documentId);
240240
$documentState = $stateFile->getContent();
241241
$this->logger->debug('Existing document, state file loaded ' . $documentId);
242-
// If there were any queries in the steps, send all steps since last save.
243-
$getStepsSinceVersion = $document->getLastSavedVersion();
242+
// If there were any queries in the steps, send all steps starting 200 steps before last save.
243+
// Adding 200 previous steps to workaround race conditions where state with missing step got persisted in the document state. See #7692
244+
$getStepsSinceVersion = $this->stepMapper->getBeforeVersion($documentId, $document->getLastSavedVersion(), 200);
244245
} catch (NotFoundException $e) {
245246
$this->logger->debug('Existing document, but no state file found for ' . $documentId);
246247
// If there is no state file, include all the steps.

0 commit comments

Comments
 (0)