From fa555bedf0cb94fb0bbbec9b7338f66252ffb230 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 27 Aug 2021 19:34:53 +0200 Subject: [PATCH] log storage migration --- src/matrix/SessionContainer.js | 2 +- src/matrix/storage/idb/StorageFactory.js | 19 +++++++++++-------- src/mocks/Storage.js | 5 +++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index f375fdd7..b58589d0 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -204,7 +204,7 @@ export class SessionContainer { reconnector: this._reconnector, }); this._sessionId = sessionInfo.id; - this._storage = await this._platform.storageFactory.create(sessionInfo.id); + this._storage = await this._platform.storageFactory.create(sessionInfo.id, log); // no need to pass access token to session const filteredSessionInfo = { id: sessionInfo.id, diff --git a/src/matrix/storage/idb/StorageFactory.js b/src/matrix/storage/idb/StorageFactory.js index 719d2672..de166a60 100644 --- a/src/matrix/storage/idb/StorageFactory.js +++ b/src/matrix/storage/idb/StorageFactory.js @@ -21,8 +21,9 @@ import { schema } from "./schema.js"; import { detectWebkitEarlyCloseTxnBug } from "./quirks.js"; const sessionName = sessionId => `hydrogen_session_${sessionId}`; -const openDatabaseWithSessionId = function(sessionId, idbFactory) { - return openDatabase(sessionName(sessionId), createStores, schema.length, idbFactory); +const openDatabaseWithSessionId = function(sessionId, idbFactory, log) { + const create = (db, txn, oldVersion, version) => createStores(db, txn, oldVersion, version, log); + return openDatabase(sessionName(sessionId), create, schema.length, idbFactory); } async function requestPersistedStorage() { @@ -49,7 +50,7 @@ export class StorageFactory { this._IDBKeyRange = IDBKeyRange; } - async create(sessionId) { + async create(sessionId, log) { await this._serviceWorkerHandler?.preventConcurrentSessionAccess(sessionId); requestPersistedStorage().then(persisted => { // Firefox lies here though, and returns true even if the user denied the request @@ -59,7 +60,7 @@ export class StorageFactory { }); const hasWebkitEarlyCloseTxnBug = await detectWebkitEarlyCloseTxnBug(this._idbFactory); - const db = await openDatabaseWithSessionId(sessionId, this._idbFactory); + const db = await openDatabaseWithSessionId(sessionId, this._idbFactory, log); return new Storage(db, this._IDBKeyRange, hasWebkitEarlyCloseTxnBug); } @@ -80,10 +81,12 @@ export class StorageFactory { } } -async function createStores(db, txn, oldVersion, version) { +async function createStores(db, txn, oldVersion, version, log) { const startIdx = oldVersion || 0; + return log.wrap({l: "storage migration", oldVersion, version}, async log => { + for(let i = startIdx; i < version; ++i) { + await log.wrap(`v${i + 1}`, log => schema[i](db, txn, log)); + } + }); - for(let i = startIdx; i < version; ++i) { - await schema[i](db, txn); - } } diff --git a/src/mocks/Storage.js b/src/mocks/Storage.js index 0cddc7bc..48e7056c 100644 --- a/src/mocks/Storage.js +++ b/src/mocks/Storage.js @@ -16,7 +16,8 @@ limitations under the License. import {FDBFactory, FDBKeyRange} from "../../lib/fake-indexeddb/index.js"; import {StorageFactory} from "../matrix/storage/idb/StorageFactory.js"; +import {NullLogItem} from "../logging/NullLogger.js"; export function createMockStorage() { - return new StorageFactory(null, new FDBFactory(), FDBKeyRange).create(1); -} \ No newline at end of file + return new StorageFactory(null, new FDBFactory(), FDBKeyRange).create(1, new NullLogItem()); +}