Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const NEW_USER_DAYS: chrono::Duration = chrono::Duration::days(30);
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 = 19;
pub const SCHEMA_VERSION: u32 = 20;
pub const IMDB_LINK_CATEGORY: &str = "imdb";
pub const GENRES_LINK_CATEGORY: &str = "Genres";
pub const CINEMETA_TOP_CATALOG_ID: &str = "top";
Expand Down
75 changes: 73 additions & 2 deletions src/runtime/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ pub trait Env {
.await?;
schema_version = 19;
}
if schema_version == 19 {
migrate_storage_schema_to_v20::<Self>()
.map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error)))
.await?;
schema_version = 20;
}
if schema_version != SCHEMA_VERSION {
panic!(
"Storage schema version must be upgraded from {} to {}",
Expand Down Expand Up @@ -714,6 +720,29 @@ fn migrate_storage_schema_to_v19<E: Env>() -> TryEnvFuture<()> {
.boxed_env()
}

fn migrate_storage_schema_to_v20<E: Env>() -> TryEnvFuture<()> {
E::get_storage::<serde_json::Value>(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(
"hardwareRendering".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(&20)))
.boxed_env()
}

#[cfg(test)]
mod test {
use serde_json::{json, Value};
Expand All @@ -729,8 +758,9 @@ mod test {
migrate_storage_schema_to_v14, migrate_storage_schema_to_v15,
migrate_storage_schema_to_v16, migrate_storage_schema_to_v17,
migrate_storage_schema_to_v18, migrate_storage_schema_to_v19,
migrate_storage_schema_to_v6, migrate_storage_schema_to_v7,
migrate_storage_schema_to_v8, migrate_storage_schema_to_v9,
migrate_storage_schema_to_v20, migrate_storage_schema_to_v6,
migrate_storage_schema_to_v7, migrate_storage_schema_to_v8,
migrate_storage_schema_to_v9,
},
Env,
},
Expand Down Expand Up @@ -1422,4 +1452,45 @@ mod test {
);
}
}

#[tokio::test]
async fn test_migration_from_19_to_20() {
{
let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv");
let profile_before = json!({
"settings": {}
});

let migrated_profile = json!({
"settings": {
"hardwareRendering": true,
}
});

// setup storage for migration
set_profile_and_schema_version(&profile_before, 19);

// migrate storage
migrate_storage_schema_to_v20::<TestEnv>()
.await
.expect("Should migrate");

let storage = STORAGE.read().expect("Should lock");

assert_eq!(
&20.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"
);
}
}
}
2 changes: 2 additions & 0 deletions src/types/profile/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Settings {
pub binge_watching: bool,
pub play_in_background: bool,
pub hardware_decoding: bool,
pub hardware_rendering: bool,
pub frame_rate_matching_strategy: FrameRateMatchingStrategy,
pub next_video_notification_duration: u32,
pub audio_passthrough: bool,
Expand Down Expand Up @@ -60,6 +61,7 @@ impl Default for Settings {
binge_watching: true,
play_in_background: true,
hardware_decoding: true,
hardware_rendering: true,
hide_spoilers: false,
gamepad_support: false,
frame_rate_matching_strategy: FrameRateMatchingStrategy::FrameRateOnly,
Expand Down
4 changes: 3 additions & 1 deletion src/unit_tests/serde/default_tokens_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl DefaultTokens for Settings {
vec![
Token::Struct {
name: "Settings",
len: 32,
len: 33,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand All @@ -394,6 +394,8 @@ impl DefaultTokens for Settings {
Token::Bool(true),
Token::Str("hardwareDecoding"),
Token::Bool(true),
Token::Str("hardwareRendering"),
Token::Bool(true),
Token::Str("frameRateMatchingStrategy"),
Token::UnitVariant {
name: "FrameRateMatchingStrategy",
Expand Down
9 changes: 7 additions & 2 deletions src/unit_tests/serde/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn settings() {
binge_watching: true,
play_in_background: true,
hardware_decoding: true,
hardware_rendering: true,
frame_rate_matching_strategy: FrameRateMatchingStrategy::FrameRateAndResolution,
next_video_notification_duration: 30,
audio_passthrough: true,
Expand Down Expand Up @@ -45,7 +46,7 @@ fn settings() {
&[
Token::Struct {
name: "Settings",
len: 32,
len: 33,
},
Token::Str("interfaceLanguage"),
Token::Str("interface_language"),
Expand All @@ -64,6 +65,8 @@ fn settings() {
Token::Bool(true),
Token::Str("hardwareDecoding"),
Token::Bool(true),
Token::Str("hardwareRendering"),
Token::Bool(true),
Token::Str("frameRateMatchingStrategy"),
Token::UnitVariant {
name: "FrameRateMatchingStrategy",
Expand Down Expand Up @@ -132,7 +135,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 32,
len: 33,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand All @@ -150,6 +153,8 @@ fn settings_de() {
Token::Bool(true),
Token::Str("hardwareDecoding"),
Token::Bool(true),
Token::Str("hardwareRendering"),
Token::Bool(true),
Token::Str("frameRateMatchingStrategy"),
Token::UnitVariant {
name: "FrameRateMatchingStrategy",
Expand Down