From 04321fc627e1ec611f9fbbbdf7fdee7cbbc2de3e Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 9 Aug 2026 13:50:21 -0400 Subject: [PATCH] Restore cached user name on transaction rollback Zotero.Users.setName() updated the in-memory cache even if the transaction was later rolled back, so a retry (e.g., of a failed sync download batch) would skip rewriting the users row. --- chrome/content/zotero/xpcom/users.js | 12 ++++++++++++ test/tests/usersTest.js | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/tests/usersTest.js diff --git a/chrome/content/zotero/xpcom/users.js b/chrome/content/zotero/xpcom/users.js index ae069cd1f5..9262131c64 100644 --- a/chrome/content/zotero/xpcom/users.js +++ b/chrome/content/zotero/xpcom/users.js @@ -139,6 +139,18 @@ Zotero.Users = new function () { return; } await Zotero.DB.queryAsync("REPLACE INTO users VALUES (?, ?)", [userID, name]); + // Restore the cached name if the transaction is rolled back + if (Zotero.DB.inTransaction()) { + let previous = _users[userID]; + Zotero.DB.addCurrentCallback("rollback", () => { + if (previous === undefined) { + delete _users[userID]; + } + else { + _users[userID] = previous; + } + }); + } _users[userID] = name; } }; diff --git a/test/tests/usersTest.js b/test/tests/usersTest.js new file mode 100644 index 0000000000..e5828097e9 --- /dev/null +++ b/test/tests/usersTest.js @@ -0,0 +1,20 @@ +describe("Zotero.Users", function () { + describe("#setName()", function () { + it("should restore the cached name if the transaction is rolled back", async function () { + var userID = 24631244; + await Zotero.Users.setName(userID, 'A'); + await executeTransactionWithForcedRollback(async function () { + await Zotero.Users.setName(userID, 'B'); + assert.equal(Zotero.Users.getName(userID), 'B'); + }); + assert.equal(Zotero.Users.getName(userID), 'A'); + + // A later save should write the new name to the database + await Zotero.Users.setName(userID, 'B'); + assert.equal( + await Zotero.DB.valueQueryAsync("SELECT name FROM users WHERE userID=?", userID), + 'B' + ); + }); + }); +});