1
0

make it possible to reinsert all sessions at once

This will be needed for database upgrades going forward. For example, an
old session without an update timestamp will not be visible in the index
that will soon be added. But since the UI will switch to that index, it
is paramount that all sessions are part of that.

The session model was built with that in mind, and has the corresponding
"update" logic. However, for that to matter old records first need to be
readded to the database.
This commit is contained in:
Adrian Wannenmacher 2026-02-27 23:02:19 +01:00
parent a7012657b3
commit 4f5f4fbb28
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
2 changed files with 68 additions and 0 deletions

View File

@ -154,4 +154,24 @@ export default class SessionRepo {
return sessions.map(
(session) => SessionRepo.#setupChangeHandling(session, transaction.db));
}
/** Load all sessions, parse them, then reinsert them.
*
* Does not set the intermediate objects up for change handling.
*
* @param {Transactable=} transaction A transaction to use.
*/
static reinsertAll(transaction) {
let trans = toTransaction(transaction, [WbDb.OS_SESSIONS], "readwrite");
let os = trans.objectStore(WbDb.OS_SESSIONS);
let cursor = os.openCursor();
cursor.addEventListener("success", function(ev) {
let cur = ev.target.result;
if (cur !== null) {
os.put((new Session(cur.value)).toStruct());
cur.continue();
}
});
}
}

View File

@ -158,5 +158,53 @@ export default function() {
});
QUnit.test("reinserting all sessions", async function(assert) {
// old structurized session (see v1 tests of session model)
const old = {
goal: 3,
ourTeam: "",
theirTeam: "",
games: [],
currentGame: null,
};
// prepare db
inst = WbDb.get(true);
await waitForOpen(inst);
// manually insert old session (deliberately impossible with SessionRepo)
let trans = inst.db.transaction([WbDb.OS_SESSIONS], "readwrite");
let os = trans.objectStore(WbDb.OS_SESSIONS);
await new Promise((resolve) => os.put(old).onsuccess = resolve);
// now update the old seession
SessionRepo.reinsertAll(inst);
// give the reinsertion a chance to execute
await new Promise((resolve) => setTimeout(resolve, 10));
// check that the session has been updated
let sessions = (await new Promise(function (resolve) {
inst
.db
.transaction([WbDb.OS_SESSIONS], "readonly")
.objectStore(WbDb.OS_SESSIONS)
.getAll()
.onsuccess = resolve;
})).target.result;
assert.strictEqual(sessions.length, 1, "exactly one session present");
assert.deepEqual(
sessions[0].updated,
new Date("2026-02-26T22:00:00"),
"session update date is fallback");
// Note that the inserted session data is older than the `updated` field
// in the model class. Thus it being present proves that the session has
// indeed been parsed and reinserted.
//
// Also note that the exact parsing and default value adding is already
// checked in the model tests, thus it would be a duplicate to test that
// here too.
});
});
}