From 88d10210adb805d2eed8f856a7d9cd2615087f64 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Mon, 5 Feb 2024 15:21:04 +0000 Subject: [PATCH] indexeddb: Regularise migration method names Signed-off-by: Andy Balaam --- .../src/crypto_store/migrations/mod.rs | 21 +++++++++--------- .../src/crypto_store/migrations/v0_to_v5.rs | 22 +++++++++---------- .../src/crypto_store/migrations/v5_to_v7.rs | 9 +++----- .../src/crypto_store/migrations/v7_to_v8.rs | 7 ++---- .../src/crypto_store/migrations/v8_to_v10.rs | 13 +++-------- 5 files changed, 29 insertions(+), 43 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/mod.rs index de82858f2..9f76b13e4 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/mod.rs @@ -49,29 +49,28 @@ pub async fn open_and_upgrade_db( let old_version = db_version(name).await?; if old_version < 5 { - v0_to_v5::migrate_schema_up_to_v5(name).await?; + v0_to_v5::schema_add(name).await?; } if old_version < 6 { - v5_to_v7::migrate_schema_up_to_v6(name).await?; + v5_to_v7::schema_add(name).await?; } if old_version < 7 { - v5_to_v7::prepare_data_for_v7(name, serializer).await?; - v5_to_v7::migrate_schema_for_v7(name).await?; + v5_to_v7::data_migrate(name, serializer).await?; + v5_to_v7::schema_delete(name).await?; } if old_version < 8 { - v7_to_v8::prepare_data_for_v8(name, serializer).await?; - v7_to_v8::migrate_schema_for_v8(name).await?; + v7_to_v8::data_migrate(name, serializer).await?; + v7_to_v8::schema_bump(name).await?; } if old_version < 9 { - v8_to_v10::upgrade_scheme_to_v9_create_inbound_group_sessions3(name).await?; + v8_to_v10::schema_add(name).await?; } if old_version < 10 { - v8_to_v10::migrate_data_before_v10_populate_inbound_group_sessions3(name, serializer) - .await?; - v8_to_v10::upgrade_scheme_to_v10_delete_inbound_group_sessions2(name).await?; + v8_to_v10::data_migrate(name, serializer).await?; + v8_to_v10::schema_delete(name).await?; } // Open and return the DB (we know it's at the latest version) @@ -538,7 +537,7 @@ mod tests { } async fn create_v5_db(name: &str) -> std::result::Result { - v0_to_v5::migrate_schema_up_to_v5(name).await?; + v0_to_v5::schema_add(name).await?; IdbDatabase::open_u32(name, 5)?.await } } diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v0_to_v5.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v0_to_v5.rs index a20ff7018..0800b7e14 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v0_to_v5.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v0_to_v5.rs @@ -25,30 +25,30 @@ use crate::crypto_store::{ }; /// Perform schema migrations as needed, up to schema version 5. -pub(crate) async fn migrate_schema_up_to_v5(name: &str) -> Result<(), DomException> { +pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 5, |db, old_version| { // An old_version of 1 could either mean actually the first version of the // schema, or a completely empty schema that has been created with a // call to `IdbDatabase::open` with no explicit "version". So, to determine // if we need to create the V1 stores, we actually check if the schema is empty. if db.object_store_names().next().is_none() { - migrate_stores_to_v1(db)?; + schema_add_v1(db)?; } if old_version < 2 { - migrate_stores_to_v2(db)?; + schema_add_v2(db)?; } if old_version < 3 { - migrate_stores_to_v3(db)?; + schema_add_v3(db)?; } if old_version < 4 { - migrate_stores_to_v4(db)?; + schema_add_v4(db)?; } if old_version < 5 { - migrate_stores_to_v5(db)?; + schema_add_v5(db)?; } Ok(()) @@ -56,7 +56,7 @@ pub(crate) async fn migrate_schema_up_to_v5(name: &str) -> Result<(), DomExcepti .await } -fn migrate_stores_to_v1(db: &IdbDatabase) -> Result<(), DomException> { +fn schema_add_v1(db: &IdbDatabase) -> Result<(), DomException> { db.create_object_store(keys::CORE)?; db.create_object_store(keys::SESSION)?; @@ -72,7 +72,7 @@ fn migrate_stores_to_v1(db: &IdbDatabase) -> Result<(), DomException> { Ok(()) } -fn migrate_stores_to_v2(db: &IdbDatabase) -> Result<(), DomException> { +fn schema_add_v2(db: &IdbDatabase) -> Result<(), DomException> { // We changed how we store inbound group sessions, the key used to // be a tuple of `(room_id, sender_key, session_id)` now it's a // tuple of `(room_id, session_id)` @@ -86,7 +86,7 @@ fn migrate_stores_to_v2(db: &IdbDatabase) -> Result<(), DomException> { Ok(()) } -fn migrate_stores_to_v3(db: &IdbDatabase) -> Result<(), DomException> { +fn schema_add_v3(db: &IdbDatabase) -> Result<(), DomException> { // We changed the way we store outbound session. // ShareInfo changed from a struct to an enum with struct variant. // Let's just discard the existing outbounds @@ -99,12 +99,12 @@ fn migrate_stores_to_v3(db: &IdbDatabase) -> Result<(), DomException> { Ok(()) } -fn migrate_stores_to_v4(db: &IdbDatabase) -> Result<(), DomException> { +fn schema_add_v4(db: &IdbDatabase) -> Result<(), DomException> { db.create_object_store(keys::SECRETS_INBOX)?; Ok(()) } -fn migrate_stores_to_v5(db: &IdbDatabase) -> Result<(), DomException> { +fn schema_add_v5(db: &IdbDatabase) -> Result<(), DomException> { // Create a new store for outgoing secret requests let object_store = db.create_object_store(keys::GOSSIP_REQUESTS)?; diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v5_to_v7.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v5_to_v7.rs index 6e57f2283..67b737951 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v5_to_v7.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v5_to_v7.rs @@ -35,7 +35,7 @@ use crate::{ }; /// Perform the schema upgrade v5 to v6, creating `inbound_group_sessions2`. -pub(crate) async fn migrate_schema_up_to_v6(name: &str) -> Result<(), DomException> { +pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 6, |db, _| { let object_store = db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?; @@ -51,10 +51,7 @@ pub(crate) async fn migrate_schema_up_to_v6(name: &str) -> Result<(), DomExcepti } /// Migrate data from `inbound_group_sessions` into `inbound_group_sessions2`. -pub(crate) async fn prepare_data_for_v7( - name: &str, - serializer: &IndexeddbSerializer, -) -> Result<()> { +pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> { info!("IndexeddbCryptoStore migrate data before v7 starting"); let db = IdbDatabase::open(name)?.await?; let res = do_prepare_data_for_v7(serializer, &db).await; @@ -119,7 +116,7 @@ async fn do_prepare_data_for_v7(serializer: &IndexeddbSerializer, db: &IdbDataba } /// Perform the schema upgrade v6 to v7, deleting `inbound_group_sessions`. -pub(crate) async fn migrate_schema_for_v7(name: &str) -> Result<(), DomException> { +pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 7, |db, _| { db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?; Ok(()) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v7_to_v8.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v7_to_v8.rs index 7ec0b4447..37b0e5e97 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v7_to_v8.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v7_to_v8.rs @@ -33,10 +33,7 @@ use crate::{ /// `inbound_group_sessions` verbatim into `inbound_group_sessions2`. What we /// should have done is re-hash them using the new table name, so we fix them up /// here. -pub(crate) async fn prepare_data_for_v8( - name: &str, - serializer: &IndexeddbSerializer, -) -> Result<()> { +pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> { info!("IndexeddbCryptoStore upgrade data -> v8 starting"); let db = IdbDatabase::open(name)?.await?; @@ -119,7 +116,7 @@ pub(crate) async fn prepare_data_for_v8( } /// Perform the schema upgrade v7 to v8, Just bumping the schema version. -pub(crate) async fn migrate_schema_for_v8(name: &str) -> Result<(), DomException> { +pub(crate) async fn schema_bump(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 8, |_, _| { // Just bump the version number to 8 to demonstrate that we have run the data // changes from prepare_data_for_v8. diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v8_to_v10.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v8_to_v10.rs index 937134c8f..67196aadc 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v8_to_v10.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/migrations/v8_to_v10.rs @@ -34,9 +34,7 @@ use crate::{ }; /// Perform the schema upgrade v8 to v9, creating `inbound_group_sessions3`. -pub(crate) async fn upgrade_scheme_to_v9_create_inbound_group_sessions3( - name: &str, -) -> Result<(), DomException> { +pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 9, |db, _| { let object_store = db.create_object_store(keys::INBOUND_GROUP_SESSIONS_V3)?; @@ -61,10 +59,7 @@ pub(crate) async fn upgrade_scheme_to_v9_create_inbound_group_sessions3( } /// Migrate data from `inbound_group_sessions2` into `inbound_group_sessions3`. -pub(crate) async fn migrate_data_before_v10_populate_inbound_group_sessions3( - name: &str, - serializer: &IndexeddbSerializer, -) -> Result<()> { +pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -> Result<()> { info!("IndexeddbCryptoStore migrate data before v10 starting"); let db = IdbDatabase::open(name)?.await?; @@ -139,9 +134,7 @@ pub(crate) async fn migrate_data_before_v10_populate_inbound_group_sessions3( } /// Perform the schema upgrade v8 to v10, deleting `inbound_group_sessions2`. -pub(crate) async fn upgrade_scheme_to_v10_delete_inbound_group_sessions2( - name: &str, -) -> Result<(), DomException> { +pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> { do_schema_upgrade(name, 10, |db, _| { db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?; Ok(())