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_sessions2 to
16//! inbound_group_sessions3, shrinking the values stored in each record.
1718use indexed_db_futures::IdbQuerySource;
19use matrix_sdk_crypto::olm::InboundGroupSession;
20use tracing::{debug, info};
21use web_sys::{DomException, IdbTransactionMode};
2223use crate::{
24 crypto_store::{
25 indexeddb_serializer::IndexeddbSerializer,
26 keys,
27 migrations::{
28 add_nonunique_index, do_schema_upgrade, old_keys,
29 v7::InboundGroupSessionIndexedDbObject2, MigrationDb,
30 },
31 InboundGroupSessionIndexedDbObject, Result,
32 },
33 IndexeddbCryptoStoreError,
34};
3536/// Perform the schema upgrade v8 to v9, creating `inbound_group_sessions3`.
37pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
38 do_schema_upgrade(name, 9, |db, _, _| {
39let object_store = db.create_object_store(keys::INBOUND_GROUP_SESSIONS_V3)?;
4041 add_nonunique_index(
42&object_store,
43 keys::INBOUND_GROUP_SESSIONS_BACKUP_INDEX,
44"needs_backup",
45 )?;
4647// See https://github.com/element-hq/element-web/issues/26892#issuecomment-1906336076
48 // for the plan concerning this property and index. At time of writing, it is
49 // unused, and needs_backup is still used.
50add_nonunique_index(
51&object_store,
52 keys::INBOUND_GROUP_SESSIONS_BACKED_UP_TO_INDEX,
53"backed_up_to",
54 )?;
5556Ok(())
57 })
58 .await
59}
6061/// Migrate data from `inbound_group_sessions2` into `inbound_group_sessions3`.
62pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> {
63let db = MigrationDb::new(name, 10).await?;
6465let txn = db.transaction_on_multi_with_mode(
66&[old_keys::INBOUND_GROUP_SESSIONS_V2, keys::INBOUND_GROUP_SESSIONS_V3],
67 IdbTransactionMode::Readwrite,
68 )?;
6970let inbound_group_sessions2 = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
71let inbound_group_sessions3 = txn.object_store(keys::INBOUND_GROUP_SESSIONS_V3)?;
7273let row_count = inbound_group_sessions2.count()?.await?;
74info!(row_count, "Shrinking inbound_group_session records");
7576// Iterate through all rows
77if let Some(cursor) = inbound_group_sessions2.open_cursor()?.await? {
78let mut idx = 0;
79loop {
80 idx += 1;
8182if idx % 100 == 0 {
83debug!("Migrating session {idx} of {row_count}");
84 }
8586// Deserialize the session from the old store
87let old_value: InboundGroupSessionIndexedDbObject2 =
88 serde_wasm_bindgen::from_value(cursor.value())?;
8990let session = InboundGroupSession::from_pickle(
91 serializer.deserialize_value_from_bytes(&old_value.pickled_session)?,
92 )
93 .map_err(|e| IndexeddbCryptoStoreError::CryptoStoreError(e.into()))?;
9495// Calculate its key in the new table
96let new_key = serializer.encode_key(
97 keys::INBOUND_GROUP_SESSIONS_V3,
98 (&session.room_id, session.session_id()),
99 );
100101// Serialize the session in the new format
102let new_value =
103 InboundGroupSessionIndexedDbObject::from_session(&session, serializer).await?;
104105// Write it to the new store
106inbound_group_sessions3
107 .add_key_val(&new_key, &serde_wasm_bindgen::to_value(&new_value)?)?;
108109// We are done with the original data, so delete it now.
110cursor.delete()?;
111112// Continue to the next record, or stop if we're done
113if !cursor.continue_cursor()?.await? {
114debug!("Migrated {idx} sessions.");
115break;
116 }
117 }
118 }
119120// We have finished with the old store. Clear it, since it is faster to
121 // 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/
122 // for more details.
123inbound_group_sessions2.clear()?.await?;
124125 txn.await.into_result()?;
126Ok(())
127}
128129/// Perform the schema upgrade v8 to v10, deleting `inbound_group_sessions2`.
130pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> {
131 do_schema_upgrade(name, 10, |db, _, _| {
132 db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
133Ok(())
134 })
135 .await
136}