From edf6c9d1ca939da369f6f15c7cfe3e95313a9629 Mon Sep 17 00:00:00 2001 From: Dmitry Vedenko Date: Thu, 28 Mar 2024 19:51:50 +0300 Subject: [PATCH] Correctly handle the case, when unsynced project is removed from Cache Resolves #6183 --- .../sync/CloudProjectsDatabase.cpp | 23 ++++++++++++---- .../sync/ResumedSnaphotUploadOperation.cpp | 26 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/libraries/lib-cloud-audiocom/sync/CloudProjectsDatabase.cpp b/libraries/lib-cloud-audiocom/sync/CloudProjectsDatabase.cpp index e8d605b7e26f..881a0059132b 100644 --- a/libraries/lib-cloud-audiocom/sync/CloudProjectsDatabase.cpp +++ b/libraries/lib-cloud-audiocom/sync/CloudProjectsDatabase.cpp @@ -492,13 +492,26 @@ void CloudProjectsDatabase::RemovePendingSnapshot( if (!connection) return; - auto statement = connection->CreateStatement( - "DELETE FROM pending_snapshots WHERE project_id = ? AND snapshot_id = ?"); + static const char* queries[] = { + "DELETE FROM pending_snapshots WHERE project_id = ? AND snapshot_id = ?", + "DELETE FROM pending_project_blobs WHERE project_id = ? AND snapshot_id = ?", + "DELETE FROM pending_project_blocks WHERE project_id = ? AND snapshot_id = ?", + }; - if (!statement) - return; + auto tx = connection->BeginTransaction("RemovePendingSnapshot"); - statement->Prepare(projectId, snapshotId).Run(); + for (auto query : queries) + { + auto statement = connection->CreateStatement(query); + + if (!statement) + return; + + if (!statement->Prepare(projectId, snapshotId).Run().IsOk()) + return; + } + + tx.Commit(); } std::vector diff --git a/libraries/lib-cloud-audiocom/sync/ResumedSnaphotUploadOperation.cpp b/libraries/lib-cloud-audiocom/sync/ResumedSnaphotUploadOperation.cpp index 825a2b8180e7..1194578aed47 100644 --- a/libraries/lib-cloud-audiocom/sync/ResumedSnaphotUploadOperation.cpp +++ b/libraries/lib-cloud-audiocom/sync/ResumedSnaphotUploadOperation.cpp @@ -28,6 +28,7 @@ #include "SampleBlock.h" #include "WaveTrack.h" +#include "CodeConversions.h" #include "DateTimeConversions.h" #include "FromChars.h" #include "IResponse.h" @@ -214,12 +215,27 @@ class ResumedSnaphotUploadOperation final : task.Block.Format = static_cast(pendingBlock.BlockSampleFormat); - task.Block.Hash = pendingBlock.BlockHash; - task.Block.Id = pendingBlock.BlockId; - task.Block.Block = sampleBlockFactory->CreateFromId( - task.Block.Format, pendingBlock.BlockId); + task.Block.Hash = pendingBlock.BlockHash; + task.Block.Id = pendingBlock.BlockId; + try + { + task.Block.Block = sampleBlockFactory->CreateFromId( + task.Block.Format, pendingBlock.BlockId); - blockTasks.push_back(std::move(task)); + blockTasks.push_back(std::move(task)); + } + catch (const FileException& e) + { + // We have failed to resume the upload, local data is missing + CloudProjectsDatabase::Get().RemovePendingSnapshot( + mProjectId, mSnapshotId); + FailSync( + { SyncResultCode::InternalClientError, + ToUTF8( + XO("Local project data was removed before the sync has completed") + .Translation()) }); + return; + } } mMissingBlocksUploader = MissingBlocksUploader::Create(