17
17
18
18
//! Migration framework for pallets.
19
19
20
- /// Macro to include all migration modules.
21
- /// We only want to make these modules public when `runtime-benchmarks` is
22
- /// enabled, so we can access migration code in benchmarks.
23
- macro_rules! use_modules {
24
- ( $( $module: ident) ,* ) => {
25
- $(
26
- #[ cfg( feature = "runtime-benchmarks" ) ]
27
- pub mod $module;
28
- #[ cfg( not( feature = "runtime-benchmarks" ) ) ]
29
- mod $module;
30
- ) *
31
- } ;
32
- }
20
+ pub mod v10;
21
+ pub mod v11;
22
+ pub mod v9;
33
23
34
- use_modules ! ( v9, v10, v11) ;
35
24
use crate :: { weights:: WeightInfo , Config , Error , MigrationInProgress , Pallet , Weight , LOG_TARGET } ;
36
25
use codec:: { Codec , Decode } ;
37
26
use frame_support:: {
@@ -58,10 +47,6 @@ fn invalid_version(version: StorageVersion) -> ! {
58
47
/// The cursor used to store the state of the current migration step.
59
48
pub type Cursor = BoundedVec < u8 , ConstU32 < 1024 > > ;
60
49
61
- // In benchmark and tests we use noop migrations, to test and bench the migration framework itself.
62
- #[ cfg( not( any( feature = "runtime-benchmarks" , test) ) ) ]
63
- type Migrations < T > = ( v9:: Migration < T > , v10:: Migration < T > , v11:: Migration < T > ) ;
64
-
65
50
/// IsFinished describes whether a migration is finished or not.
66
51
pub enum IsFinished {
67
52
Yes ,
@@ -206,23 +191,16 @@ pub trait MigrateSequence: private::Sealed {
206
191
}
207
192
208
193
/// Performs all necessary migrations based on `StorageVersion`.
209
- #[ cfg( not( any( feature = "runtime-benchmarks" , test) ) ) ]
210
- pub struct Migration < T : Config , M : MigrateSequence = Migrations < T > > ( PhantomData < ( T , M ) > ) ;
211
-
212
- /// Custom migration for running runtime-benchmarks and tests.
213
- #[ cfg( any( feature = "runtime-benchmarks" , test) ) ]
214
- pub struct Migration < T : Config , M : MigrateSequence = ( NoopMigration < 1 > , NoopMigration < 2 > ) > (
215
- PhantomData < ( T , M ) > ,
216
- ) ;
194
+ pub struct Migration < T : Config > ( PhantomData < T > ) ;
217
195
218
196
#[ cfg( feature = "try-runtime" ) ]
219
- impl < T : Config , M : MigrateSequence > Migration < T , M > {
197
+ impl < T : Config > Migration < T > {
220
198
fn run_all_steps ( ) -> Result < ( ) , TryRuntimeError > {
221
199
let mut weight = Weight :: zero ( ) ;
222
200
let name = <Pallet < T > >:: name ( ) ;
223
201
loop {
224
202
let in_progress_version = <Pallet < T > >:: on_chain_storage_version ( ) + 1 ;
225
- let state = M :: pre_upgrade_step ( in_progress_version) ?;
203
+ let state = T :: Migrations :: pre_upgrade_step ( in_progress_version) ?;
226
204
let ( status, w) = Self :: migrate ( Weight :: MAX ) ;
227
205
weight. saturating_accrue ( w) ;
228
206
log:: info!(
@@ -231,7 +209,7 @@ impl<T: Config, M: MigrateSequence> Migration<T, M> {
231
209
in_progress_version,
232
210
weight
233
211
) ;
234
- M :: post_upgrade_step ( in_progress_version, state) ?;
212
+ T :: Migrations :: post_upgrade_step ( in_progress_version, state) ?;
235
213
if matches ! ( status, MigrateResult :: Completed ) {
236
214
break
237
215
}
@@ -243,7 +221,7 @@ impl<T: Config, M: MigrateSequence> Migration<T, M> {
243
221
}
244
222
}
245
223
246
- impl < T : Config , M : MigrateSequence > OnRuntimeUpgrade for Migration < T , M > {
224
+ impl < T : Config > OnRuntimeUpgrade for Migration < T > {
247
225
fn on_runtime_upgrade ( ) -> Weight {
248
226
let name = <Pallet < T > >:: name ( ) ;
249
227
let latest_version = <Pallet < T > >:: current_storage_version ( ) ;
@@ -274,7 +252,7 @@ impl<T: Config, M: MigrateSequence> OnRuntimeUpgrade for Migration<T, M> {
274
252
"{name}: Upgrading storage from {storage_version:?} to {latest_version:?}." ,
275
253
) ;
276
254
277
- let cursor = M :: new ( storage_version + 1 ) ;
255
+ let cursor = T :: Migrations :: new ( storage_version + 1 ) ;
278
256
MigrationInProgress :: < T > :: set ( Some ( cursor) ) ;
279
257
280
258
#[ cfg( feature = "try-runtime" ) ]
@@ -295,11 +273,14 @@ impl<T: Config, M: MigrateSequence> OnRuntimeUpgrade for Migration<T, M> {
295
273
target: LOG_TARGET ,
296
274
"{}: Range supported {:?}, range requested {:?}" ,
297
275
<Pallet <T >>:: name( ) ,
298
- M :: VERSION_RANGE ,
276
+ T :: Migrations :: VERSION_RANGE ,
299
277
( storage_version, target_version)
300
278
) ;
301
279
302
- ensure ! ( M :: is_upgrade_supported( storage_version, target_version) , "Unsupported upgrade" ) ;
280
+ ensure ! (
281
+ T :: Migrations :: is_upgrade_supported( storage_version, target_version) ,
282
+ "Unsupported upgrade"
283
+ ) ;
303
284
Ok ( Default :: default ( ) )
304
285
}
305
286
}
@@ -324,11 +305,11 @@ pub enum StepResult {
324
305
Completed { steps_done : u32 } ,
325
306
}
326
307
327
- impl < T : Config , M : MigrateSequence > Migration < T , M > {
328
- /// Verify that each migration's step of the MigrateSequence fits into `Cursor`.
308
+ impl < T : Config > Migration < T > {
309
+ /// Verify that each migration's step of the [`T::Migrations`] sequence fits into `Cursor`.
329
310
pub ( crate ) fn integrity_test ( ) {
330
311
let max_weight = <T as frame_system:: Config >:: BlockWeights :: get ( ) . max_block ;
331
- M :: integrity_test ( max_weight)
312
+ T :: Migrations :: integrity_test ( max_weight)
332
313
}
333
314
334
315
/// Migrate
@@ -357,33 +338,36 @@ impl<T: Config, M: MigrateSequence> Migration<T, M> {
357
338
in_progress_version,
358
339
) ;
359
340
360
- let result =
361
- match M :: steps ( in_progress_version, cursor_before. as_ref ( ) , & mut weight_left) {
362
- StepResult :: InProgress { cursor, steps_done } => {
363
- * progress = Some ( cursor) ;
341
+ let result = match T :: Migrations :: steps (
342
+ in_progress_version,
343
+ cursor_before. as_ref ( ) ,
344
+ & mut weight_left,
345
+ ) {
346
+ StepResult :: InProgress { cursor, steps_done } => {
347
+ * progress = Some ( cursor) ;
348
+ MigrateResult :: InProgress { steps_done }
349
+ } ,
350
+ StepResult :: Completed { steps_done } => {
351
+ in_progress_version. put :: < Pallet < T > > ( ) ;
352
+ if <Pallet < T > >:: current_storage_version ( ) != in_progress_version {
353
+ log:: info!(
354
+ target: LOG_TARGET ,
355
+ "{name}: Next migration is {:?}," ,
356
+ in_progress_version + 1
357
+ ) ;
358
+ * progress = Some ( T :: Migrations :: new ( in_progress_version + 1 ) ) ;
364
359
MigrateResult :: InProgress { steps_done }
365
- } ,
366
- StepResult :: Completed { steps_done } => {
367
- in_progress_version. put :: < Pallet < T > > ( ) ;
368
- if <Pallet < T > >:: current_storage_version ( ) != in_progress_version {
369
- log:: info!(
370
- target: LOG_TARGET ,
371
- "{name}: Next migration is {:?}," ,
372
- in_progress_version + 1
373
- ) ;
374
- * progress = Some ( M :: new ( in_progress_version + 1 ) ) ;
375
- MigrateResult :: InProgress { steps_done }
376
- } else {
377
- log:: info!(
378
- target: LOG_TARGET ,
379
- "{name}: All migrations done. At version {:?}," ,
380
- in_progress_version
381
- ) ;
382
- * progress = None ;
383
- MigrateResult :: Completed
384
- }
385
- } ,
386
- } ;
360
+ } else {
361
+ log:: info!(
362
+ target: LOG_TARGET ,
363
+ "{name}: All migrations done. At version {:?}," ,
364
+ in_progress_version
365
+ ) ;
366
+ * progress = None ;
367
+ MigrateResult :: Completed
368
+ }
369
+ } ,
370
+ } ;
387
371
388
372
( result, weight_limit. saturating_sub ( weight_left) )
389
373
} )
@@ -527,11 +511,14 @@ mod test {
527
511
528
512
#[ test]
529
513
fn is_upgrade_supported_works ( ) {
530
- type M = ( MockMigration < 9 > , MockMigration < 10 > , MockMigration < 11 > ) ;
514
+ type Migrations = ( MockMigration < 9 > , MockMigration < 10 > , MockMigration < 11 > ) ;
531
515
532
516
[ ( 1 , 1 ) , ( 8 , 11 ) , ( 9 , 11 ) ] . into_iter ( ) . for_each ( |( from, to) | {
533
517
assert ! (
534
- M :: is_upgrade_supported( StorageVersion :: new( from) , StorageVersion :: new( to) ) ,
518
+ Migrations :: is_upgrade_supported(
519
+ StorageVersion :: new( from) ,
520
+ StorageVersion :: new( to)
521
+ ) ,
535
522
"{} -> {} is supported" ,
536
523
from,
537
524
to
@@ -540,7 +527,10 @@ mod test {
540
527
541
528
[ ( 1 , 0 ) , ( 0 , 3 ) , ( 7 , 11 ) , ( 8 , 10 ) ] . into_iter ( ) . for_each ( |( from, to) | {
542
529
assert ! (
543
- !M :: is_upgrade_supported( StorageVersion :: new( from) , StorageVersion :: new( to) ) ,
530
+ !Migrations :: is_upgrade_supported(
531
+ StorageVersion :: new( from) ,
532
+ StorageVersion :: new( to)
533
+ ) ,
544
534
"{} -> {} is not supported" ,
545
535
from,
546
536
to
@@ -550,27 +540,26 @@ mod test {
550
540
551
541
#[ test]
552
542
fn steps_works ( ) {
553
- type M = ( MockMigration < 2 > , MockMigration < 3 > ) ;
543
+ type Migrations = ( MockMigration < 2 > , MockMigration < 3 > ) ;
554
544
let version = StorageVersion :: new ( 2 ) ;
555
- let mut cursor = M :: new ( version) ;
545
+ let mut cursor = Migrations :: new ( version) ;
556
546
557
547
let mut weight = Weight :: from_all ( 2 ) ;
558
- let result = M :: steps ( version, & cursor, & mut weight) ;
548
+ let result = Migrations :: steps ( version, & cursor, & mut weight) ;
559
549
cursor = vec ! [ 1u8 , 0 ] . try_into ( ) . unwrap ( ) ;
560
550
assert_eq ! ( result, StepResult :: InProgress { cursor: cursor. clone( ) , steps_done: 1 } ) ;
561
551
assert_eq ! ( weight, Weight :: from_all( 1 ) ) ;
562
552
563
553
let mut weight = Weight :: from_all ( 2 ) ;
564
554
assert_eq ! (
565
- M :: steps( version, & cursor, & mut weight) ,
555
+ Migrations :: steps( version, & cursor, & mut weight) ,
566
556
StepResult :: Completed { steps_done: 1 }
567
557
) ;
568
558
}
569
559
570
560
#[ test]
571
561
fn no_migration_in_progress_works ( ) {
572
- type M = ( MockMigration < 1 > , MockMigration < 2 > ) ;
573
- type TestMigration = Migration < Test , M > ;
562
+ type TestMigration = Migration < Test > ;
574
563
575
564
ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
576
565
assert_eq ! ( StorageVersion :: get:: <Pallet <Test >>( ) , 2 ) ;
@@ -580,8 +569,7 @@ mod test {
580
569
581
570
#[ test]
582
571
fn migration_works ( ) {
583
- type M = ( MockMigration < 1 > , MockMigration < 2 > ) ;
584
- type TestMigration = Migration < Test , M > ;
572
+ type TestMigration = Migration < Test > ;
585
573
586
574
ExtBuilder :: default ( ) . set_storage_version ( 0 ) . build ( ) . execute_with ( || {
587
575
assert_eq ! ( StorageVersion :: get:: <Pallet <Test >>( ) , 0 ) ;
0 commit comments