diff --git a/chrome/content/zotero/xpcom/sync/syncEngine.js b/chrome/content/zotero/xpcom/sync/syncEngine.js index 0a1af776c6..577dbf9bc9 100644 --- a/chrome/content/zotero/xpcom/sync/syncEngine.js +++ b/chrome/content/zotero/xpcom/sync/syncEngine.js @@ -204,7 +204,7 @@ Zotero.Sync.Data.Engine.prototype.stop = function () { /** - * Download updated objects from API and save to local cache + * Download updated objects from API and save to DB * * @return {Promise} - A download result code (this.DOWNLOAD_RESULT_*) */ @@ -1107,6 +1107,11 @@ Zotero.Sync.Data.Engine.prototype._uploadObjects = Zotero.Promise.coroutine(func objectsClass.updateSynced(updateSyncedIDs, true); }.bind(this)); + // Purge older objects in sync cache + if (toSave.length) { + yield Zotero.Sync.Data.Local.purgeCache(objectType, this.libraryID); + } + // Handle failed objects for (let index in results.failed) { let { code, message, data } = results.failed[index]; diff --git a/chrome/content/zotero/xpcom/sync/syncLocal.js b/chrome/content/zotero/xpcom/sync/syncLocal.js index 70315212e2..3b92afc653 100644 --- a/chrome/content/zotero/xpcom/sync/syncLocal.js +++ b/chrome/content/zotero/xpcom/sync/syncLocal.js @@ -1104,6 +1104,21 @@ Zotero.Sync.Data.Local = { }, + /** + * Delete entries from sync cache that don't exist or are less than the current object version + */ + purgeCache: Zotero.Promise.coroutine(function* (objectType, libraryID) { + var syncObjectTypeID = Zotero.Sync.Data.Utilities.getSyncObjectTypeID(objectType); + var table = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType).table; + var sql = "DELETE FROM syncCache WHERE ROWID IN (" + + "SELECT SC.ROWID FROM syncCache SC " + + `LEFT JOIN ${table} O USING (libraryID, key, version) ` + + "WHERE syncObjectTypeID=? AND SC.libraryID=? AND " + + "(O.libraryID IS NULL OR SC.version < O.version)"; + yield Zotero.DB.queryAsync(sql, [syncObjectTypeID, libraryID]); + }), + + processConflicts: Zotero.Promise.coroutine(function* (objectType, libraryID, conflicts, options = {}) { if (!conflicts.length) return []; diff --git a/test/tests/syncEngineTest.js b/test/tests/syncEngineTest.js index 3f6a889912..08081b94c4 100644 --- a/test/tests/syncEngineTest.js +++ b/test/tests/syncEngineTest.js @@ -682,6 +682,10 @@ describe("Zotero.Sync.Data.Engine", function () { assert.isArray(cached.data.conditions); break; } + + // Make sure older versions have been removed from the cache + let versions = yield Zotero.Sync.Data.Local.getCacheObjectVersions(type, libraryID, key); + assert.sameMembers(versions, [version]); } })