@@ -2,9 +2,10 @@ use super::*;
22use frame_support:: {
33 pallet_prelude:: { Identity , OptionQuery } ,
44 storage_alias,
5- traits:: { Get , GetStorageVersion , StorageVersion } ,
5+ traits:: { Get , StorageVersion } ,
66 weights:: Weight ,
77} ;
8+ use alloc:: string:: String ;
89use sp_std:: vec:: Vec ;
910
1011// TODO (camfairchild): TEST MIGRATION
@@ -50,24 +51,41 @@ pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
5051}
5152// Public migrate function to be called by Lib.rs on upgrade.
5253pub fn migrate_fix_total_coldkey_stake < T : Config > ( ) -> Weight {
53- let current_storage_version: u16 = 7 ;
54- let next_storage_version: u16 = 8 ;
54+ let migration_name = b"fix_total_coldkey_stake_v7" . to_vec ( ) ;
5555
5656 // Initialize the weight with one read operation.
5757 let mut weight = T :: DbWeight :: get ( ) . reads ( 1 ) ;
5858
59- // Grab the current on-chain storage version.
60- // Cant fail on retrieval.
61- let onchain_version = Pallet :: < T > :: on_chain_storage_version ( ) ;
62-
63- // Only run this migration on storage version 6.
64- if onchain_version == current_storage_version {
65- weight = weight. saturating_add ( do_migrate_fix_total_coldkey_stake :: < T > ( ) ) ;
66- // Cant fail on insert.
67- StorageVersion :: new ( next_storage_version) . put :: < Pallet < T > > ( ) ;
68- weight. saturating_accrue ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
59+ // Check if the migration has already run
60+ if HasMigrationRun :: < T > :: get ( & migration_name) {
61+ log:: info!(
62+ "Migration '{:?}' has already run. Skipping." ,
63+ migration_name
64+ ) ;
65+ return Weight :: zero ( ) ;
6966 }
7067
68+ log:: info!(
69+ "Running migration '{}'" ,
70+ String :: from_utf8_lossy( & migration_name)
71+ ) ;
72+
73+ // Run the migration
74+ weight = weight. saturating_add ( do_migrate_fix_total_coldkey_stake :: < T > ( ) ) ;
75+
76+ // Mark the migration as completed
77+ HasMigrationRun :: < T > :: insert ( & migration_name, true ) ;
78+ weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
79+
80+ // Set the storage version to 7
81+ StorageVersion :: new ( 7 ) . put :: < Pallet < T > > ( ) ;
82+ weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
83+
84+ log:: info!(
85+ "Migration '{:?}' completed. Storage version set to 7." ,
86+ String :: from_utf8_lossy( & migration_name)
87+ ) ;
88+
7189 // Return the migration weight.
7290 weight
7391}
0 commit comments