@@ -301,6 +301,12 @@ pub trait Env {
301301 . await ?;
302302 schema_version = 20 ;
303303 }
304+ if schema_version == 20 {
305+ migrate_storage_schema_to_v21 :: < Self > ( )
306+ . map_err ( |error| EnvError :: StorageSchemaVersionUpgrade ( Box :: new ( error) ) )
307+ . await ?;
308+ schema_version = 21 ;
309+ }
304310 if schema_version != SCHEMA_VERSION {
305311 panic ! (
306312 "Storage schema version must be upgraded from {} to {}" ,
@@ -740,6 +746,29 @@ fn migrate_storage_schema_to_v20<E: Env>() -> TryEnvFuture<()> {
740746 . boxed_env ( )
741747}
742748
749+ fn migrate_storage_schema_to_v21 < E : Env > ( ) -> TryEnvFuture < ( ) > {
750+ E :: get_storage :: < serde_json:: Value > ( PROFILE_STORAGE_KEY )
751+ . and_then ( |mut profile| {
752+ match profile
753+ . as_mut ( )
754+ . and_then ( |profile| profile. as_object_mut ( ) )
755+ . and_then ( |profile| profile. get_mut ( "settings" ) )
756+ . and_then ( |settings| settings. as_object_mut ( ) )
757+ {
758+ Some ( settings) => {
759+ settings. insert (
760+ "assSubtitlesStyling" . to_owned ( ) ,
761+ serde_json:: Value :: Bool ( false ) ,
762+ ) ;
763+ E :: set_storage ( PROFILE_STORAGE_KEY , Some ( & profile) )
764+ }
765+ _ => E :: set_storage :: < ( ) > ( PROFILE_STORAGE_KEY , None ) ,
766+ }
767+ } )
768+ . and_then ( |_| E :: set_storage ( SCHEMA_VERSION_STORAGE_KEY , Some ( & 21 ) ) )
769+ . boxed_env ( )
770+ }
771+
743772#[ cfg( test) ]
744773mod test {
745774 use serde_json:: { json, Value } ;
@@ -755,9 +784,9 @@ mod test {
755784 migrate_storage_schema_to_v14, migrate_storage_schema_to_v15,
756785 migrate_storage_schema_to_v16, migrate_storage_schema_to_v17,
757786 migrate_storage_schema_to_v18, migrate_storage_schema_to_v19,
758- migrate_storage_schema_to_v20, migrate_storage_schema_to_v6 ,
759- migrate_storage_schema_to_v7 , migrate_storage_schema_to_v8 ,
760- migrate_storage_schema_to_v9,
787+ migrate_storage_schema_to_v20, migrate_storage_schema_to_v21 ,
788+ migrate_storage_schema_to_v6 , migrate_storage_schema_to_v7 ,
789+ migrate_storage_schema_to_v8 , migrate_storage_schema_to_v9,
761790 } ,
762791 Env ,
763792 } ,
@@ -1490,4 +1519,45 @@ mod test {
14901519 ) ;
14911520 }
14921521 }
1522+
1523+ #[ tokio:: test]
1524+ async fn test_migration_from_20_to_21 ( ) {
1525+ {
1526+ let _test_env_guard = TestEnv :: reset ( ) . expect ( "Should lock TestEnv" ) ;
1527+ let profile_before = json ! ( {
1528+ "settings" : { }
1529+ } ) ;
1530+
1531+ let migrated_profile = json ! ( {
1532+ "settings" : {
1533+ "assSubtitlesStyling" : false ,
1534+ }
1535+ } ) ;
1536+
1537+ // setup storage for migration
1538+ set_profile_and_schema_version ( & profile_before, 20 ) ;
1539+
1540+ // migrate storage
1541+ migrate_storage_schema_to_v21 :: < TestEnv > ( )
1542+ . await
1543+ . expect ( "Should migrate" ) ;
1544+
1545+ let storage = STORAGE . read ( ) . expect ( "Should lock" ) ;
1546+
1547+ assert_eq ! (
1548+ & 21 . to_string( ) ,
1549+ storage
1550+ . get( SCHEMA_VERSION_STORAGE_KEY )
1551+ . expect( "Should have the schema set" ) ,
1552+ "Scheme version should now be updated"
1553+ ) ;
1554+ assert_eq ! (
1555+ & migrated_profile. to_string( ) ,
1556+ storage
1557+ . get( PROFILE_STORAGE_KEY )
1558+ . expect( "Should have the profile set" ) ,
1559+ "Profile should match"
1560+ ) ;
1561+ }
1562+ }
14931563}
0 commit comments