pallet_fast_unstake/
migrations.rs1pub mod v1 {
19	use crate::{types::BalanceOf, *};
20	use alloc::vec::Vec;
21	use frame_support::{
22		storage::unhashed,
23		traits::{Defensive, Get, GetStorageVersion, OnRuntimeUpgrade},
24		weights::Weight,
25	};
26	use sp_staking::EraIndex;
27
28	#[cfg(feature = "try-runtime")]
29	use frame_support::ensure;
30	#[cfg(feature = "try-runtime")]
31	use sp_runtime::TryRuntimeError;
32
33	pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
34	impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
35		fn on_runtime_upgrade() -> Weight {
36			let current = Pallet::<T>::in_code_storage_version();
37			let onchain = Pallet::<T>::on_chain_storage_version();
38
39			log!(
40				info,
41				"Running migration with in-code storage version {:?} / onchain {:?}",
42				current,
43				onchain
44			);
45
46			if current == 1 && onchain == 0 {
47				current.put::<Pallet<T>>();
49
50				if Head::<T>::exists() {
52					if let Some((stash, _, deposit)) =
53						unhashed::take::<(T::AccountId, Vec<EraIndex>, BalanceOf<T>)>(
54							&Head::<T>::hashed_key(),
55						)
56						.defensive()
57					{
58						Queue::<T>::insert(stash, deposit);
59					} else {
60						}
62					T::DbWeight::get().reads_writes(2, 3)
63				} else {
64					T::DbWeight::get().reads(2)
65				}
66			} else {
67				log!(info, "Migration did not execute. This probably should be removed");
68				T::DbWeight::get().reads(1)
69			}
70		}
71
72		#[cfg(feature = "try-runtime")]
73		fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
74			ensure!(
75				Pallet::<T>::on_chain_storage_version() == 0,
76				"The onchain storage version must be zero for the migration to execute."
77			);
78			Ok(Default::default())
79		}
80
81		#[cfg(feature = "try-runtime")]
82		fn post_upgrade(_: Vec<u8>) -> Result<(), TryRuntimeError> {
83			ensure!(
84				Pallet::<T>::on_chain_storage_version() == 1,
85				"The onchain version must be updated after the migration."
86			);
87			Ok(())
88		}
89	}
90}