From c96363746bfc482173a3376138d11fdd188926c5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 26 Mar 2018 02:09:09 -0400 Subject: [PATCH] Fix related-item relations pointing to a previous user account If somebody switched accounts in a previous version, it was apparently possible for related items to end up pointing at an item URI with the old userID, which could cause a 403 on sync. https://forums.zotero.org/discussion/70989/report-id-477331252 (5.0 deletes data when switching accounts to avoid exactly this sort of bug.) --- chrome/content/zotero/xpcom/schema.js | 17 +++++++++++++++++ resource/schema/userdata.sql | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index f6eb30621c..6a67bf2f51 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -2414,6 +2414,23 @@ Zotero.Schema = new function(){ yield Zotero.DB.queryAsync("DELETE FROM relationPredicates WHERE predicate='dc:isReplacedBy'"); } + else if (i == 100) { + let userID = yield Zotero.DB.valueQueryAsync("SELECT value FROM settings WHERE setting='account' AND key='userID'"); + if (userID) { + let predicateID = yield Zotero.DB.valueQueryAsync("SELECT predicateID FROM relationPredicates WHERE predicate='dc:relation'"); + let rows = yield Zotero.DB.queryAsync("SELECT itemID, object FROM items JOIN itemRelations IR USING (itemID) WHERE libraryID=? AND predicateID=?", [1, predicateID]); + for (let row of rows) { + let matches = row.object.match(/^http:\/\/zotero.org\/users\/(\d+)\/items\/([A-Z0-9]+)$/); + if (matches) { + // Wrong libraryID + if (matches[1] != userID) { + yield Zotero.DB.queryAsync(`UPDATE itemRelations SET object='http://zotero.org/users/${userID}/items/${matches[2]}' WHERE itemID=? AND predicateID=?`, [row.itemID, predicateID]); + } + } + } + } + } + // If breaking compatibility or doing anything dangerous, clear minorUpdateFrom } diff --git a/resource/schema/userdata.sql b/resource/schema/userdata.sql index c95064d727..7e40eca6a9 100644 --- a/resource/schema/userdata.sql +++ b/resource/schema/userdata.sql @@ -1,4 +1,4 @@ --- 99 +-- 100 -- Copyright (c) 2009 Center for History and New Media -- George Mason University, Fairfax, Virginia, USA