|
| 1 | +use super::*; |
| 2 | +use frame_support::weights::Weight; |
| 3 | +use log; |
| 4 | +use scale_info::prelude::string::String; |
| 5 | + |
| 6 | +pub fn migrate_reset_bonds<T: Config>() -> Weight { |
| 7 | + use frame_support::traits::Get; |
| 8 | + let migration_name = b"migrate_reset_bonds".to_vec(); |
| 9 | + |
| 10 | + // Start counting weight |
| 11 | + let mut weight = T::DbWeight::get().reads(1); |
| 12 | + |
| 13 | + // Check if we already ran this migration |
| 14 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 15 | + log::info!( |
| 16 | + target: "runtime", |
| 17 | + "Migration '{:?}' has already run. Skipping.", |
| 18 | + String::from_utf8_lossy(&migration_name) |
| 19 | + ); |
| 20 | + return weight; |
| 21 | + } |
| 22 | + |
| 23 | + log::info!( |
| 24 | + target: "runtime", |
| 25 | + "Running migration '{}'", |
| 26 | + String::from_utf8_lossy(&migration_name) |
| 27 | + ); |
| 28 | + |
| 29 | + /// ===== Migration Body ===== |
| 30 | + // Clear all bonds |
| 31 | + let mut curr = Bonds::<T>::clear(u32::MAX, None); |
| 32 | + weight = weight |
| 33 | + .saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64)); |
| 34 | + while curr.maybe_cursor.is_some() { |
| 35 | + curr = Bonds::<T>::clear(u32::MAX, curr.maybe_cursor.as_deref()); |
| 36 | + weight = weight |
| 37 | + .saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64)); |
| 38 | + } |
| 39 | + |
| 40 | + /// ===== Migration End ===== |
| 41 | + // ----------------------------- |
| 42 | + // Mark the migration as done |
| 43 | + // ----------------------------- |
| 44 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 45 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 46 | + |
| 47 | + log::info!( |
| 48 | + target: "runtime", |
| 49 | + "Migration '{}' completed successfully.", |
| 50 | + String::from_utf8_lossy(&migration_name) |
| 51 | + ); |
| 52 | + |
| 53 | + weight |
| 54 | +} |
0 commit comments