From f4258507643e1622b6d298eb4994fa15afd78da8 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 11 Jan 2012 01:59:12 -0500 Subject: [PATCH] Fix sync error if duplicates are merged before first sync Replace local user key in relations with libraryID on sync user changes and invalid upload sync errors. The latter will be unnecessary after a DB upgrade step. Also fixes libraryID in relations not being replaced correctly when switching sync users. --- chrome/content/zotero/xpcom/data/relations.js | 6 ++--- chrome/content/zotero/xpcom/schema.js | 1 + chrome/content/zotero/xpcom/sync.js | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/relations.js b/chrome/content/zotero/xpcom/data/relations.js index 70f0ab4315..5324890739 100644 --- a/chrome/content/zotero/xpcom/data/relations.js +++ b/chrome/content/zotero/xpcom/data/relations.js @@ -124,15 +124,15 @@ Zotero.Relations = new function () { Zotero.DB.beginTransaction(); var sql = "UPDATE relations SET libraryID=? WHERE libraryID=?"; - Zotero.DB.query(sql, [fromLibraryID, toLibraryID]); + Zotero.DB.query(sql, [toLibraryID, fromLibraryID]); sql = "UPDATE relations SET " + "subject=REPLACE(subject, 'zotero.org/users/" + fromUserID + "', " + "'zotero.org/users/" + toUserID + "'), " + "object=REPLACE(object, 'zotero.org/users/" + fromUserID + "', " + "'zotero.org/users/" + toUserID + "') " - + "WHERE predicate='owl:sameAs'"; - Zotero.DB.query(sql); + + "WHERE predicate IN (?, ?)"; + Zotero.DB.query(sql, [this.linkedObjectPredicate, this.deletedItemPredicate]); Zotero.DB.commitTransaction(); } diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 3c29ffd42c..128890a68b 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -3067,6 +3067,7 @@ Zotero.Schema = new function(){ // TODO // // Replace customBaseFieldMappings to fix FK fields/customField -> customFields->customFieldID + // If libraryID set, make sure no relations still use a local user key, and then remove on-error code in sync.js _updateDBVersion('userdata', toVersion); diff --git a/chrome/content/zotero/xpcom/sync.js b/chrome/content/zotero/xpcom/sync.js index d05336875c..7752af297c 100644 --- a/chrome/content/zotero/xpcom/sync.js +++ b/chrome/content/zotero/xpcom/sync.js @@ -1890,6 +1890,21 @@ Zotero.Sync.Server = new function () { } } } + + // Make sure this isn't due to relations using a local user key + // + // TEMP: This can be removed once a DB upgrade step is added + try { + var sql = "SELECT libraryID FROM relations WHERE libraryID LIKE 'local/%' LIMIT 1"; + var repl = Zotero.DB.valueQuery(sql); + if (repl) { + Zotero.Relations.updateUser(repl, repl, Zotero.userID, Zotero.libraryID); + } + } + catch (e) { + Components.utils.reportError(e); + Zotero.debug(e); + } break; case 'FULL_SYNC_REQUIRED': @@ -2200,6 +2215,10 @@ Zotero.Sync.Server = new function () { } if (lastUserID != userID || lastLibraryID != libraryID) { + if (!lastLibraryID) { + var repl = "local/" + Zotero.getLocalUserKey(); + } + Zotero.userID = userID; Zotero.libraryID = libraryID; @@ -2211,6 +2230,11 @@ Zotero.Sync.Server = new function () { Zotero.Sync.Server.resetClient(); Zotero.Sync.Storage.resetAllSyncStates(); } + // Replace local user key with libraryID, in case duplicates were + // merged before the first sync + else if (!lastLibraryID) { + Zotero.Relations.updateUser(repl, repl, userID, libraryID); + } } if (lastUsername != username) {