matrix_sdk_indexeddb/crypto_store/migrations/
v8_to_v10.rs

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.
14
15//! Migration code that moves from inbound_group_sessions2 to
16//! inbound_group_sessions3, shrinking the values stored in each record.
17
18use indexed_db_futures::IdbQuerySource;
19use matrix_sdk_crypto::olm::InboundGroupSession;
20use tracing::{debug, info};
21use web_sys::{DomException, IdbTransactionMode};
22
23use 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};
35
36/// 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, _, _| {
39        let object_store = db.create_object_store(keys::INBOUND_GROUP_SESSIONS_V3)?;
40
41        add_nonunique_index(
42            &object_store,
43            keys::INBOUND_GROUP_SESSIONS_BACKUP_INDEX,
44            "needs_backup",
45        )?;
46
47        // 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.
50        add_nonunique_index(
51            &object_store,
52            keys::INBOUND_GROUP_SESSIONS_BACKED_UP_TO_INDEX,
53            "backed_up_to",
54        )?;
55
56        Ok(())
57    })
58    .await
59}
60
61/// Migrate data from `inbound_group_sessions2` into `inbound_group_sessions3`.
62pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> {
63    let db = MigrationDb::new(name, 10).await?;
64
65    let txn = db.transaction_on_multi_with_mode(
66        &[old_keys::INBOUND_GROUP_SESSIONS_V2, keys::INBOUND_GROUP_SESSIONS_V3],
67        IdbTransactionMode::Readwrite,
68    )?;
69
70    let inbound_group_sessions2 = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
71    let inbound_group_sessions3 = txn.object_store(keys::INBOUND_GROUP_SESSIONS_V3)?;
72
73    let row_count = inbound_group_sessions2.count()?.await?;
74    info!(row_count, "Shrinking inbound_group_session records");
75
76    // Iterate through all rows
77    if let Some(cursor) = inbound_group_sessions2.open_cursor()?.await? {
78        let mut idx = 0;
79        loop {
80            idx += 1;
81
82            if idx % 100 == 0 {
83                debug!("Migrating session {idx} of {row_count}");
84            }
85
86            // Deserialize the session from the old store
87            let old_value: InboundGroupSessionIndexedDbObject2 =
88                serde_wasm_bindgen::from_value(cursor.value())?;
89
90            let session = InboundGroupSession::from_pickle(
91                serializer.deserialize_value_from_bytes(&old_value.pickled_session)?,
92            )
93            .map_err(|e| IndexeddbCryptoStoreError::CryptoStoreError(e.into()))?;
94
95            // Calculate its key in the new table
96            let new_key = serializer.encode_key(
97                keys::INBOUND_GROUP_SESSIONS_V3,
98                (&session.room_id, session.session_id()),
99            );
100
101            // Serialize the session in the new format
102            let new_value =
103                InboundGroupSessionIndexedDbObject::from_session(&session, serializer).await?;
104
105            // Write it to the new store
106            inbound_group_sessions3
107                .add_key_val(&new_key, &serde_wasm_bindgen::to_value(&new_value)?)?;
108
109            // We are done with the original data, so delete it now.
110            cursor.delete()?;
111
112            // Continue to the next record, or stop if we're done
113            if !cursor.continue_cursor()?.await? {
114                debug!("Migrated {idx} sessions.");
115                break;
116            }
117        }
118    }
119
120    // 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.
123    inbound_group_sessions2.clear()?.await?;
124
125    txn.await.into_result()?;
126    Ok(())
127}
128
129/// 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)?;
133        Ok(())
134    })
135    .await
136}