matrix_sdk_indexeddb/crypto_store/migrations/
v0_to_v5.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//! Schema-only migrations adding various stores and indices, notably
16//! the first version of `inbound_group_sessions`.
17
18use indexed_db_futures::IdbDatabase;
19use web_sys::DomException;
20
21use crate::crypto_store::{
22    keys,
23    migrations::{add_nonunique_index, add_unique_index, do_schema_upgrade, old_keys},
24    Result,
25};
26
27/// Perform schema migrations as needed, up to schema version 5.
28pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
29    do_schema_upgrade(name, 5, |db, _, old_version| {
30        // An old_version of 1 could either mean actually the first version of the
31        // schema, or a completely empty schema that has been created with a
32        // call to `IdbDatabase::open` with no explicit "version". So, to determine
33        // if we need to create the V1 stores, we actually check if the schema is empty.
34        if db.object_store_names().next().is_none() {
35            schema_add_v1(db)?;
36        }
37
38        if old_version < 2 {
39            schema_add_v2(db)?;
40        }
41
42        if old_version < 3 {
43            schema_add_v3(db)?;
44        }
45
46        if old_version < 4 {
47            schema_add_v4(db)?;
48        }
49
50        if old_version < 5 {
51            schema_add_v5(db)?;
52        }
53
54        Ok(())
55    })
56    .await
57}
58
59fn schema_add_v1(db: &IdbDatabase) -> Result<(), DomException> {
60    db.create_object_store(keys::CORE)?;
61    db.create_object_store(keys::SESSION)?;
62
63    db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
64    db.create_object_store(keys::OUTBOUND_GROUP_SESSIONS)?;
65    db.create_object_store(keys::TRACKED_USERS)?;
66    db.create_object_store(keys::OLM_HASHES)?;
67    db.create_object_store(keys::DEVICES)?;
68
69    db.create_object_store(keys::IDENTITIES)?;
70    db.create_object_store(keys::BACKUP_KEYS)?;
71
72    Ok(())
73}
74
75fn schema_add_v2(db: &IdbDatabase) -> Result<(), DomException> {
76    // We changed how we store inbound group sessions, the key used to
77    // be a tuple of `(room_id, sender_key, session_id)` now it's a
78    // tuple of `(room_id, session_id)`
79    //
80    // Let's just drop the whole object store.
81    db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
82    db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
83
84    db.create_object_store(keys::ROOM_SETTINGS)?;
85
86    Ok(())
87}
88
89fn schema_add_v3(db: &IdbDatabase) -> Result<(), DomException> {
90    // We changed the way we store outbound session.
91    // ShareInfo changed from a struct to an enum with struct variant.
92    // Let's just discard the existing outbounds
93    db.delete_object_store(keys::OUTBOUND_GROUP_SESSIONS)?;
94    db.create_object_store(keys::OUTBOUND_GROUP_SESSIONS)?;
95
96    // Support for MSC2399 withheld codes
97    db.create_object_store(keys::DIRECT_WITHHELD_INFO)?;
98
99    Ok(())
100}
101
102fn schema_add_v4(db: &IdbDatabase) -> Result<(), DomException> {
103    db.create_object_store(keys::SECRETS_INBOX)?;
104    Ok(())
105}
106
107fn schema_add_v5(db: &IdbDatabase) -> Result<(), DomException> {
108    // Create a new store for outgoing secret requests
109    let object_store = db.create_object_store(keys::GOSSIP_REQUESTS)?;
110
111    add_nonunique_index(&object_store, keys::GOSSIP_REQUESTS_UNSENT_INDEX, "unsent")?;
112
113    add_unique_index(&object_store, keys::GOSSIP_REQUESTS_BY_INFO_INDEX, "info")?;
114
115    if db.object_store_names().any(|n| n == "outgoing_secret_requests") {
116        // Delete the old store names. We just delete any existing requests.
117        db.delete_object_store("outgoing_secret_requests")?;
118        db.delete_object_store("unsent_secret_requests")?;
119        db.delete_object_store("secret_requests_by_info")?;
120    }
121
122    Ok(())
123}