use frame_support::{
pallet_prelude::{PalletInfoAccess, StorageVersion, Weight},
traits::OnRuntimeUpgrade,
};
pub trait StorageMigration: OnRuntimeUpgrade {
#[allow(clippy::let_and_return)]
fn migrate() -> Weight {
#[cfg(feature = "try-runtime")]
let state = match Self::pre_upgrade() {
Ok(state) => state,
Err(_err) => b"Pre-upgrade failed".to_vec(),
};
let weight = Self::on_runtime_upgrade();
#[cfg(feature = "try-runtime")]
match Self::post_upgrade(state) {
Ok(()) => (),
Err(err) => {
log::error!(
"Post-upgrade failed for pallet. Error: {:?}",
err
);
}
};
weight
}
}
impl<T: OnRuntimeUpgrade> StorageMigration for T {}
pub fn ensure_storage_version<P: PalletInfoAccess>(version: u16) -> Result<(), &'static str> {
if StorageVersion::get::<P>() == StorageVersion::new(version) {
Ok(())
} else {
Err("Bad storage version")
}
}