Update relations using local user key when first setting sync user

In case items are merged before the first sync
This commit is contained in:
Dan Stillman 2017-03-01 23:34:11 -05:00
parent ba91a2ea52
commit 0d8643087a
4 changed files with 30 additions and 4 deletions

View file

@ -165,14 +165,14 @@ Zotero.Relations = new function () {
}; };
this.updateUser = Zotero.Promise.coroutine(function* (toUserID) { this.updateUser = Zotero.Promise.coroutine(function* (fromUserID, toUserID) {
var fromUserID = Zotero.Users.getCurrentUserID();
if (!fromUserID) { if (!fromUserID) {
fromUserID = "local/" + Zotero.Users.getLocalUserKey(); fromUserID = "local/" + Zotero.Users.getLocalUserKey();
} }
if (!toUserID) { if (!toUserID) {
throw new Error("Invalid target userID " + toUserID); throw new Error("Invalid target userID " + toUserID);
} }
Zotero.DB.requireTransaction(); Zotero.DB.requireTransaction();
for (let type of _types) { for (let type of _types) {
let sql = `SELECT DISTINCT object FROM ${type}Relations WHERE object LIKE ?`; let sql = `SELECT DISTINCT object FROM ${type}Relations WHERE object LIKE ?`;

View file

@ -169,6 +169,10 @@ Zotero.Sync.Data.Local = {
} }
if (!lastUserID) { if (!lastUserID) {
yield Zotero.Users.setCurrentUserID(userID); yield Zotero.Users.setCurrentUserID(userID);
// Replace local user key with libraryID, in case duplicates were merged before the
// first sync
yield Zotero.Relations.updateUser(null, userID);
} }
}); });

View file

@ -38,7 +38,7 @@ describe("Zotero.Relations", function () {
assert.include(rels[0], "/users/local"); assert.include(rels[0], "/users/local");
yield Zotero.DB.executeTransaction(function* () { yield Zotero.DB.executeTransaction(function* () {
yield Zotero.Relations.updateUser(1); yield Zotero.Relations.updateUser(null, 1);
}) })
var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate); var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate);
@ -57,7 +57,7 @@ describe("Zotero.Relations", function () {
assert.include(rels[0], "/users/1"); assert.include(rels[0], "/users/1");
yield Zotero.DB.executeTransaction(function* () { yield Zotero.DB.executeTransaction(function* () {
yield Zotero.Relations.updateUser(2); yield Zotero.Relations.updateUser(1, 2);
}); });
var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate); var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate);

View file

@ -94,6 +94,28 @@ describe("Zotero.Sync.Data.Local", function() {
Zotero.DataDirectory.forceChange.restore(); Zotero.DataDirectory.forceChange.restore();
}); });
it("should migrate relations using local user key", function* () {
yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='account'");
yield Zotero.Users.init();
var item1 = yield createDataObject('item');
var item2 = createUnsavedDataObject('item');
item2.addRelatedItem(item1);
yield item2.save();
var pred = Zotero.Relations.relatedItemPredicate;
assert.isTrue(
item2.toJSON().relations[pred][0].startsWith('http://zotero.org/users/local/')
);
waitForDialog(false, 'accept', 'chrome://zotero/content/hardConfirmationDialog.xul');
yield Zotero.Sync.Data.Local.checkUser(window, 1, "A");
assert.isTrue(
item2.toJSON().relations[pred][0].startsWith('http://zotero.org/users/1/items/')
);
});
}); });