migrations: prevent accidentally using unversioned migrations instead of VersionedMigration (#3835)

closes #1324 

#### Problem
Currently, it is possible to accidentally use inner unversioned
migration instead of `VersionedMigration` since both implement
`OnRuntimeUpgrade`.

#### Solution

With this change, we make it clear that value of `Inner` is not intended
to be used directly. It is achieved by bounding `Inner` to new trait
`UncheckedOnRuntimeUpgrade`, which has the same interface (except
`unchecked_` prefix) as `OnRuntimeUpgrade`.

#### `try-runtime` functions

Since developers can implement `try-runtime` for `Inner` value in
`VersionedMigration` and have custom logic for it, I added the same
`try-runtime` functions to `UncheckedOnRuntimeUpgrade`. I looked for a
ways to not duplicate functions, but couldn't find anything that doesn't
significantly change the codebase. So I would appreciate If you have any
suggestions to improve this

cc @liamaharon @xlc 

polkadot address: 16FqwPZ8GRC5U5D4Fu7W33nA55ZXzXGWHwmbnE1eT6pxuqcT

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
This commit is contained in:
Dastan
2024-04-02 15:43:09 +02:00
committed by GitHub
parent 8e95a3e1aa
commit e54279699b
22 changed files with 255 additions and 187 deletions
+4 -4
View File
@@ -18,15 +18,15 @@
//! Various pieces of common functionality.
use super::*;
use core::marker::PhantomData;
use frame_support::traits::{Get, OnRuntimeUpgrade};
use frame_support::traits::{Get, UncheckedOnRuntimeUpgrade};
mod v1 {
use super::*;
/// Actual implementation of the storage migration.
pub struct MigrateToV1Impl<T, I>(PhantomData<(T, I)>);
pub struct UncheckedMigrateToV1Impl<T, I>(PhantomData<(T, I)>);
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for MigrateToV1Impl<T, I> {
impl<T: Config<I>, I: 'static> UncheckedOnRuntimeUpgrade for UncheckedMigrateToV1Impl<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let mut count = 0;
for (collection, detail) in Collection::<T, I>::iter() {
@@ -49,7 +49,7 @@ mod v1 {
pub type MigrateV0ToV1<T, I> = frame_support::migrations::VersionedMigration<
0,
1,
v1::MigrateToV1Impl<T, I>,
v1::UncheckedMigrateToV1Impl<T, I>,
Pallet<T, I>,
<T as frame_system::Config>::DbWeight,
>;