use super::super::LOG_TARGET;
use sp_io::hashing::twox_128;
use frame_support::{
traits::{
Get, GetStorageVersion, PalletInfoAccess, StorageVersion,
STORAGE_VERSION_STORAGE_KEY_POSTFIX,
},
weights::Weight,
};
pub fn migrate<T: frame_system::Config, P: GetStorageVersion + PalletInfoAccess, N: AsRef<str>>(
old_pallet_name: N,
new_pallet_name: N,
) -> Weight {
let old_pallet_name = old_pallet_name.as_ref();
let new_pallet_name = new_pallet_name.as_ref();
if new_pallet_name == old_pallet_name {
log::info!(
target: LOG_TARGET,
"New pallet name is equal to the old prefix. No migration needs to be done.",
);
return Weight::zero()
}
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
log::info!(
target: LOG_TARGET,
"Running migration to v4 for membership with storage version {:?}",
on_chain_storage_version,
);
if on_chain_storage_version < 4 {
frame_support::storage::migration::move_pallet(
old_pallet_name.as_bytes(),
new_pallet_name.as_bytes(),
);
log_migration("migration", old_pallet_name, new_pallet_name);
StorageVersion::new(4).put::<P>();
<T as frame_system::Config>::BlockWeights::get().max_block
} else {
log::warn!(
target: LOG_TARGET,
"Attempted to apply migration to v4 but failed because storage version is {:?}",
on_chain_storage_version,
);
Weight::zero()
}
}
pub fn pre_migrate<P: GetStorageVersion, N: AsRef<str>>(old_pallet_name: N, new_pallet_name: N) {
let old_pallet_name = old_pallet_name.as_ref();
let new_pallet_name = new_pallet_name.as_ref();
log_migration("pre-migration", old_pallet_name, new_pallet_name);
if new_pallet_name == old_pallet_name {
return
}
let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX);
let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
new_pallet_prefix.to_vec(),
new_pallet_prefix.to_vec(),
|key| Ok(key.to_vec()),
);
assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key));
assert!(<P as GetStorageVersion>::on_chain_storage_version() < 4);
}
pub fn post_migrate<P: GetStorageVersion, N: AsRef<str>>(old_pallet_name: N, new_pallet_name: N) {
let old_pallet_name = old_pallet_name.as_ref();
let new_pallet_name = new_pallet_name.as_ref();
log_migration("post-migration", old_pallet_name, new_pallet_name);
if new_pallet_name == old_pallet_name {
return
}
let old_pallet_prefix = twox_128(old_pallet_name.as_bytes());
let old_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
old_pallet_prefix.to_vec(),
old_pallet_prefix.to_vec(),
|_| Ok(()),
);
assert_eq!(old_pallet_prefix_iter.count(), 0);
let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
new_pallet_prefix.to_vec(),
new_pallet_prefix.to_vec(),
|_| Ok(()),
);
assert!(new_pallet_prefix_iter.count() >= 1);
assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 4);
}
fn log_migration(stage: &str, old_pallet_name: &str, new_pallet_name: &str) {
log::info!(
target: LOG_TARGET,
"{}, prefix: '{}' ==> '{}'",
stage,
old_pallet_name,
new_pallet_name,
);
}