matrix_sdk_indexeddb/crypto_store/migrations/
v5_to_v7.rs1use indexed_db_futures::IdbQuerySource;
23use matrix_sdk_crypto::olm::InboundGroupSession;
24use tracing::{debug, info};
25use web_sys::{DomException, IdbTransactionMode};
26
27use crate::{
28 crypto_store::{
29 indexeddb_serializer::IndexeddbSerializer,
30 keys,
31 migrations::{add_nonunique_index, do_schema_upgrade, old_keys, v7, MigrationDb},
32 Result,
33 },
34 IndexeddbCryptoStoreError,
35};
36
37pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
39 do_schema_upgrade(name, 6, |db, _, _| {
40 let object_store = db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
41
42 add_nonunique_index(
43 &object_store,
44 keys::INBOUND_GROUP_SESSIONS_BACKUP_INDEX,
45 "needs_backup",
46 )?;
47
48 Ok(())
49 })
50 .await
51}
52
53pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> {
55 let db = MigrationDb::new(name, 7).await?;
56
57 let txn = db.transaction_on_multi_with_mode(
59 &[old_keys::INBOUND_GROUP_SESSIONS_V1, old_keys::INBOUND_GROUP_SESSIONS_V2],
60 IdbTransactionMode::Readwrite,
61 )?;
62
63 let old_store = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
64 let new_store = txn.object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
65
66 let row_count = old_store.count()?.await?;
67 info!(row_count, "Migrating inbound group session data from v1 to v2");
68
69 if let Some(cursor) = old_store.open_cursor()?.await? {
70 let mut idx = 0;
71 loop {
72 idx += 1;
73 let key = cursor.key().ok_or(matrix_sdk_crypto::CryptoStoreError::Backend(
74 "inbound_group_sessions v1 cursor has no key".into(),
75 ))?;
76 let value = cursor.value();
77
78 if idx % 100 == 0 {
79 debug!("Migrating session {idx} of {row_count}");
80 }
81
82 let igs = InboundGroupSession::from_pickle(serializer.deserialize_value(value)?)
83 .map_err(|e| IndexeddbCryptoStoreError::CryptoStoreError(e.into()))?;
84
85 let 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 })?;
90
91 new_store.add_key_val(&key, &new_data)?;
92
93 cursor.delete()?;
95
96 if !cursor.continue_cursor()?.await? {
97 break;
98 }
99 }
100 }
101
102 old_store.clear()?.await?;
106
107 Ok(txn.await.into_result()?)
108}
109
110pub(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)?;
114 Ok(())
115 })
116 .await
117}