Browse Source

clear video_by_id map on migration

ignazio-bovo 3 years ago
parent
commit
f862e75b82

+ 18 - 0
runtime-modules/content/src/lib.rs

@@ -1409,6 +1409,24 @@ impl<T: Trait> Module<T> {
     }
 }
 
+// Some initial config for the module on runtime upgrade
+impl<T: Trait> Module<T> {
+    pub fn on_runtime_upgrade() {
+        // all ids start at 1
+        <NextChannelCategoryId<T>>::put(T::ChannelCategoryId::one());
+        <NextVideoCategoryId<T>>::put(T::VideoCategoryId::one());
+        <NextVideoId<T>>::put(T::VideoId::one());
+        <NextChannelId<T>>::put(T::ChannelId::one());
+        <NextPlaylistId<T>>::put(T::PlaylistId::one());
+        <NextSeriesId<T>>::put(T::SeriesId::one());
+        <NextPersonId<T>>::put(T::PersonId::one());
+        <NextChannelOwnershipTransferRequestId<T>>::put(T::ChannelOwnershipTransferRequestId::one());
+
+        // VideoById map is cleared
+        <VideoById<T>>::remove_all();
+    }
+}
+
 decl_event!(
     pub enum Event<T>
     where

+ 55 - 0
runtime-modules/content/src/tests/migration.rs

@@ -0,0 +1,55 @@
+#![cfg(test)]
+
+use super::mock::*;
+use crate::sp_api_hidden_includes_decl_storage::hidden_include::traits::Currency;
+use crate::*;
+
+#[test]
+fn successful_video_map_clearing() {
+    with_default_mock_builder(|| {
+        run_to_block(1);
+
+        // deposit initial balance
+        let _ = balances::Module::<Test>::deposit_creating(
+            &FIRST_MEMBER_ORIGIN,
+            <Test as balances::Trait>::Balance::from(100u32),
+        );
+
+        // set up 1 channel and two videos (each with no asset)
+        let channel_id = NextChannelId::<Test>::get();
+
+        create_channel_mock(
+            FIRST_MEMBER_ORIGIN,
+            ContentActor::Member(FIRST_MEMBER_ID),
+            ChannelCreationParametersRecord {
+                assets: None,
+                meta: Some(vec![]),
+                reward_account: None,
+            },
+            Ok(()),
+        );
+
+        let params = VideoCreationParametersRecord {
+            assets: None,
+            meta: None,
+        };
+
+        create_video_mock(
+            FIRST_MEMBER_ORIGIN,
+            ContentActor::Member(FIRST_MEMBER_ID),
+            channel_id,
+            params.clone(),
+            Ok(()),
+        );
+        create_video_mock(
+            FIRST_MEMBER_ORIGIN,
+            ContentActor::Member(FIRST_MEMBER_ID),
+            channel_id,
+            params,
+            Ok(()),
+        );
+
+        Content::on_runtime_upgrade();
+        assert_eq!(VideoById::<Test>::iter().next(), None)
+    })
+}

+ 1 - 0
runtime-modules/content/src/tests/mod.rs

@@ -2,5 +2,6 @@
 
 mod channels;
 mod curators;
+mod migration;
 mod mock;
 mod videos;

+ 3 - 0
runtime/src/runtime_api.rs

@@ -58,6 +58,9 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<AccountId, Call, Signa
 pub struct CustomOnRuntimeUpgrade;
 impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
     fn on_runtime_upgrade() -> Weight {
+        // initialize content module
+        content::Module::<Runtime>::on_runtime_upgrade();
+
         10_000_000 // TODO: adjust weight
     }
 }