1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
1415//! Migration code that moves from inbound_group_sessions to
16//! inbound_group_sessions2, adding a `needs_backup` property.
17//!
18//! The migration 5->6 creates the new store inbound_group_sessions2.
19//! Then we move the data into the new store.
20//! The migration 6->7 deletes the old store inbound_group_sessions.
2122use indexed_db_futures::IdbQuerySource;
23use matrix_sdk_crypto::olm::InboundGroupSession;
24use tracing::{debug, info};
25use web_sys::{DomException, IdbTransactionMode};
2627use crate::{
28 crypto_store::{
29 indexeddb_serializer::IndexeddbSerializer,
30 keys,
31 migrations::{add_nonunique_index, do_schema_upgrade, old_keys, v7, MigrationDb},
32Result,
33 },
34 IndexeddbCryptoStoreError,
35};
3637/// Perform the schema upgrade v5 to v6, creating `inbound_group_sessions2`.
38pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
39 do_schema_upgrade(name, 6, |db, _, _| {
40let object_store = db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
4142 add_nonunique_index(
43&object_store,
44 keys::INBOUND_GROUP_SESSIONS_BACKUP_INDEX,
45"needs_backup",
46 )?;
4748Ok(())
49 })
50 .await
51}
5253/// Migrate data from `inbound_group_sessions` into `inbound_group_sessions2`.
54pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> {
55let db = MigrationDb::new(name, 7).await?;
5657// The new store has been made for inbound group sessions; time to populate it.
58let txn = db.transaction_on_multi_with_mode(
59&[old_keys::INBOUND_GROUP_SESSIONS_V1, old_keys::INBOUND_GROUP_SESSIONS_V2],
60 IdbTransactionMode::Readwrite,
61 )?;
6263let old_store = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
64let new_store = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
6566let row_count = old_store.count()?.await?;
67info!(row_count, "Migrating inbound group session data from v1 to v2");
6869if let Some(cursor) = old_store.open_cursor()?.await? {
70let mut idx = 0;
71loop {
72 idx += 1;
73let key = cursor.key().ok_or(matrix_sdk_crypto::CryptoStoreError::Backend(
74"inbound_group_sessions v1 cursor has no key".into(),
75 ))?;
76let value = cursor.value();
7778if idx % 100 == 0 {
79debug!("Migrating session {idx} of {row_count}");
80 }
8182let igs = InboundGroupSession::from_pickle(serializer.deserialize_value(value)?)
83 .map_err(|e| IndexeddbCryptoStoreError::CryptoStoreError(e.into()))?;
8485let new_data =
86 serde_wasm_bindgen::to_value(&v7::InboundGroupSessionIndexedDbObject2 {
87 pickled_session: serializer.serialize_value_as_bytes(&igs.pickle().await)?,
88 needs_backup: !igs.backed_up(),
89 })?;
9091 new_store.add_key_val(&key, &new_data)?;
9293// We are done with the original data, so delete it now.
94cursor.delete()?;
9596if !cursor.continue_cursor()?.await? {
97break;
98 }
99 }
100 }
101102// We have finished with the old store. Clear it, since it is faster to
103 // clear+delete than just delete. See https://www.artificialworlds.net/blog/2024/02/02/deleting-an-indexed-db-store-can-be-incredibly-slow-on-firefox/
104 // for more details.
105old_store.clear()?.await?;
106107Ok(txn.await.into_result()?)
108}
109110/// Perform the schema upgrade v6 to v7, deleting `inbound_group_sessions`.
111pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> {
112 do_schema_upgrade(name, 7, |db, _, _| {
113 db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
114Ok(())
115 })
116 .await
117}