diff --git a/src/constants.rs b/src/constants.rs index f7bf865f9..c71c5c62e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -39,7 +39,7 @@ pub const CALENDAR_ITEMS_COUNT: usize = 100; pub const WATCHED_THRESHOLD_COEF: f64 = 0.7; pub const CREDITS_THRESHOLD_COEF: f64 = 0.9; /// The latest migration scheme version -pub const SCHEMA_VERSION: u32 = 15; +pub const SCHEMA_VERSION: u32 = 16; pub const IMDB_LINK_CATEGORY: &str = "imdb"; pub const GENRES_LINK_CATEGORY: &str = "Genres"; pub const CINEMETA_TOP_CATALOG_ID: &str = "top"; diff --git a/src/runtime/env.rs b/src/runtime/env.rs index 9c3d02683..dbbc072ca 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -271,6 +271,12 @@ pub trait Env { .await?; schema_version = 15; } + if schema_version == 15 { + migrate_storage_schema_to_v16::() + .map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error))) + .await?; + schema_version = 16; + } if schema_version != SCHEMA_VERSION { panic!( "Storage schema version must be upgraded from {} to {}", @@ -606,6 +612,30 @@ fn migrate_storage_schema_to_v15() -> TryEnvFuture<()> { .boxed_env() } +fn migrate_storage_schema_to_v16() -> TryEnvFuture<()> { + E::get_storage::(PROFILE_STORAGE_KEY) + .and_then(|mut profile| { + match profile + .as_mut() + .and_then(|profile| profile.as_object_mut()) + .and_then(|profile| profile.get_mut("settings")) + .and_then(|settings| settings.as_object_mut()) + { + Some(settings) => { + settings.insert( + "serverInForeground".to_owned(), + serde_json::Value::Bool(false), + ); + settings.insert("sendCrashReports".to_owned(), serde_json::Value::Bool(true)); + E::set_storage(PROFILE_STORAGE_KEY, Some(&profile)) + } + _ => E::set_storage::<()>(PROFILE_STORAGE_KEY, None), + } + }) + .and_then(|_| E::set_storage(SCHEMA_VERSION_STORAGE_KEY, Some(&16))) + .boxed_env() +} + #[cfg(test)] mod test { use serde_json::{json, Value}; @@ -619,8 +649,9 @@ mod test { migrate_storage_schema_to_v10, migrate_storage_schema_to_v11, migrate_storage_schema_to_v12, migrate_storage_schema_to_v13, migrate_storage_schema_to_v14, migrate_storage_schema_to_v15, - migrate_storage_schema_to_v6, migrate_storage_schema_to_v7, - migrate_storage_schema_to_v8, migrate_storage_schema_to_v9, + migrate_storage_schema_to_v16, migrate_storage_schema_to_v6, + migrate_storage_schema_to_v7, migrate_storage_schema_to_v8, + migrate_storage_schema_to_v9, }, Env, }, @@ -1150,4 +1181,43 @@ mod test { assert_storage_schema_version(15); } } + + #[tokio::test] + async fn test_migration_from_15_to_16() { + let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv"); + + let init_profile = json!({ + "settings": {} + }); + + let migrated_profile = json!({ + "settings": { + "serverInForeground": false, + "sendCrashReports": true + } + }); + + set_profile_and_schema_version(&init_profile, 15); + + migrate_storage_schema_to_v16::() + .await + .expect("Should migrate"); + + let storage = STORAGE.read().expect("Should lock"); + + assert_eq!( + &16.to_string(), + storage + .get(SCHEMA_VERSION_STORAGE_KEY) + .expect("Should have the schema set"), + "Scheme version should now be updated" + ); + assert_eq!( + &migrated_profile.to_string(), + storage + .get(PROFILE_STORAGE_KEY) + .expect("Should have the profile set"), + "Profile should match" + ); + } } diff --git a/src/types/profile/settings.rs b/src/types/profile/settings.rs index 721617555..bea632b38 100644 --- a/src/types/profile/settings.rs +++ b/src/types/profile/settings.rs @@ -38,6 +38,8 @@ pub struct Settings { pub pause_on_minimize: bool, pub surround_sound: bool, pub streaming_server_warning_dismissed: Option>, + pub server_in_foreground: bool, + pub send_crash_reports: bool, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -77,6 +79,8 @@ impl Default for Settings { pause_on_minimize: false, surround_sound: false, streaming_server_warning_dismissed: None, + server_in_foreground: false, + send_crash_reports: true, } } } diff --git a/src/unit_tests/serde/default_tokens_ext.rs b/src/unit_tests/serde/default_tokens_ext.rs index b3462f6e9..6a53cd98d 100644 --- a/src/unit_tests/serde/default_tokens_ext.rs +++ b/src/unit_tests/serde/default_tokens_ext.rs @@ -374,7 +374,7 @@ impl DefaultTokens for Settings { vec![ Token::Struct { name: "Settings", - len: 27, + len: 29, }, Token::Str("interfaceLanguage"), Token::Str("eng"), @@ -435,6 +435,10 @@ impl DefaultTokens for Settings { Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, + Token::Str("serverInForeground"), + Token::Bool(false), + Token::Str("sendCrashReports"), + Token::Bool(true), Token::StructEnd, ] } diff --git a/src/unit_tests/serde/settings.rs b/src/unit_tests/serde/settings.rs index 1e19e49cb..72932d29e 100644 --- a/src/unit_tests/serde/settings.rs +++ b/src/unit_tests/serde/settings.rs @@ -36,11 +36,13 @@ fn settings() { streaming_server_warning_dismissed: Some( Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap(), ), + server_in_foreground: false, + send_crash_reports: true, }, &[ Token::Struct { name: "Settings", - len: 27, + len: 29, }, Token::Str("interfaceLanguage"), Token::Str("interface_language"), @@ -105,6 +107,10 @@ fn settings() { Token::Str("streamingServerWarningDismissed"), Token::Some, Token::Str("2021-01-01T00:00:00Z"), + Token::Str("serverInForeground"), + Token::Bool(false), + Token::Str("sendCrashReports"), + Token::Bool(true), Token::StructEnd, ], ); @@ -117,7 +123,7 @@ fn settings_de() { &[ Token::Struct { name: "Settings", - len: 22, + len: 24, }, Token::Str("interfaceLanguage"), Token::Str("eng"), @@ -174,6 +180,10 @@ fn settings_de() { Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, + Token::Str("serverInForeground"), + Token::Bool(false), + Token::Str("sendCrashReports"), + Token::Bool(true), Token::StructEnd, ], );