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.
This commit is contained in:
Dan Stillman 2026-08-09 13:50:21 -04:00
parent 3630b904d7
commit 04321fc627
2 changed files with 32 additions and 0 deletions

View file

@ -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;
}
};

20
test/tests/usersTest.js Normal file
View file

@ -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'
);
});
});
});