Skip to content

Commit

Permalink
pallet-uniques: Move migration over to VersionedMigration (parityte…
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr authored Dec 12, 2023
1 parent 4182171 commit 648d04d
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions substrate/frame/uniques/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@

//! Various pieces of common functionality.
use super::*;
use frame_support::traits::{Get, GetStorageVersion, PalletInfoAccess, StorageVersion};

/// Migrate the pallet storage to v1.
pub fn migrate_to_v1<T: Config<I>, I: 'static, P: GetStorageVersion + PalletInfoAccess>(
) -> frame_support::weights::Weight {
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
log::info!(
target: LOG_TARGET,
"Running migration storage v1 for uniques with storage version {:?}",
on_chain_storage_version,
);

if on_chain_storage_version < 1 {
let mut count = 0;
for (collection, detail) in Collection::<T, I>::iter() {
CollectionAccount::<T, I>::insert(&detail.owner, &collection, ());
count += 1;
use frame_support::traits::{Get, OnRuntimeUpgrade};
use sp_std::marker::PhantomData;

mod v1 {
use super::*;

/// Actual implementation of the storage migration.
pub struct MigrateToV1Impl<T, I>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for MigrateToV1Impl<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let mut count = 0;
for (collection, detail) in Collection::<T, I>::iter() {
CollectionAccount::<T, I>::insert(&detail.owner, &collection, ());
count += 1;
}

log::info!(
target: LOG_TARGET,
"Storage migration v1 for uniques finished.",
);

// calculate and return migration weights
T::DbWeight::get().reads_writes(count as u64 + 1, count as u64 + 1)
}
StorageVersion::new(1).put::<P>();
log::info!(
target: LOG_TARGET,
"Running migration storage v1 for uniques with storage version {:?} was complete",
on_chain_storage_version,
);
// calculate and return migration weights
T::DbWeight::get().reads_writes(count as u64 + 1, count as u64 + 1)
} else {
log::warn!(
target: LOG_TARGET,
"Attempted to apply migration to v1 but failed because storage version is {:?}",
on_chain_storage_version,
);
T::DbWeight::get().reads(1)
}
}

/// Migrate the pallet storage from `0` to `1`.
pub type MigrateV0ToV1<T, I> = frame_support::migrations::VersionedMigration<
0,
1,
v1::MigrateToV1Impl<T, I>,
Pallet<T, I>,
<T as frame_system::Config>::DbWeight,
>;

0 comments on commit 648d04d

Please sign in to comment.