Skip to content

Commit

Permalink
fix: services and genetic analyst service migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 committed Sep 29, 2022
1 parent 0c19d59 commit 04e9b0d
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 4 deletions.
18 changes: 15 additions & 3 deletions pallets/genetic-analyst-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use frame_support::{
codec::{Decode, Encode},
pallet_prelude::*,
sp_runtime::SaturatedConversion,
traits::Currency,
traits::{Currency, StorageVersion},
};
pub use pallet::*;
use primitives_duration::ExpectedDuration;
Expand All @@ -16,7 +16,9 @@ use traits_genetic_analyst_services::{
};

pub mod interface;
pub mod migrations;
pub mod weights;

pub use interface::GeneticAnalystServiceInterface;
use sp_std::prelude::*;

Expand Down Expand Up @@ -71,13 +73,18 @@ where
}
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;

use crate::{
interface::GeneticAnalystServiceInterface, weights::WeightInfo, Currency,
GeneticAnalystService, GeneticAnalystServiceInfo, GeneticAnalystServiceOwner,
};
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_system::pallet_prelude::*;
pub use sp_std::prelude::*;

Expand All @@ -91,12 +98,17 @@ pub mod pallet {

// ----- This is template code, every pallet needs this ---
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migrations::migrate::<T>()
}
}
// --------------------------------------------------------

// ----- Types -------
Expand Down
96 changes: 96 additions & 0 deletions pallets/genetic-analyst-services/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::{
AccountIdOf, BalanceOf, Config, GeneticAnalystService, GeneticAnalystServiceInfo,
GeneticAnalystServices, HashOf, Pallet,
};
use frame_support::{
pallet_prelude::{Decode, Encode},
traits::Get,
weights::Weight,
};
use primitives_duration::{DurationType, ExpectedDuration};
use primitives_price_and_currency::PriceByCurrency;
use sp_std::vec::Vec;

pub fn migrate<T: Config>() -> Weight {
use frame_support::traits::StorageVersion;

let mut weight: Weight = 0;
let mut version = StorageVersion::get::<Pallet<T>>();

if version < 1 {
weight = weight.saturating_add(version::v1::migrate::<T>());
version = StorageVersion::new(1);
}

version.put::<Pallet<T>>();
weight
}

mod version {
use super::*;

pub mod v1 {
use super::*;

pub fn migrate<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().writes(1);

#[derive(Encode, Decode)]
pub struct OldExpectedDuration {
pub duration: i8,
pub duration_type: DurationType,
}

#[derive(Encode, Decode)]
pub struct OldGeneticAnalystServiceInfo<Balance> {
pub name: Vec<u8>,
pub prices_by_currency: Vec<PriceByCurrency<Balance>>,
pub expected_duration: OldExpectedDuration,
pub description: Vec<u8>,
pub test_result_sample: Vec<u8>,
}

#[derive(Encode, Decode)]
pub struct OldGeneticAnalystService<AccountId, Hash, Balance> {
pub id: Hash,
pub owner_id: AccountId,
pub info: OldGeneticAnalystServiceInfo<Balance>,
}

pub type OldGeneticAnalystServiceOf<T> =
OldGeneticAnalystService<AccountIdOf<T>, HashOf<T>, BalanceOf<T>>;

GeneticAnalystServices::<T>::translate(
|_key, old_services: OldGeneticAnalystServiceOf<T>| {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));

let old_service_info = &old_services.info;

let old_expected_duration = &old_service_info.expected_duration;
let old_duration = old_expected_duration.duration;
let old_duration_type = old_expected_duration.duration_type.clone();
let expected_duration = ExpectedDuration {
duration: old_duration as u64,
duration_type: old_duration_type,
};

let service_info = GeneticAnalystServiceInfo {
name: old_service_info.name.clone(),
prices_by_currency: old_service_info.prices_by_currency.clone(),
expected_duration,
description: old_service_info.description.clone(),
test_result_sample: old_service_info.test_result_sample.clone(),
};

Some(GeneticAnalystService {
id: old_services.id,
owner_id: old_services.owner_id,
info: service_info,
})
},
);

weight
}
}
}
13 changes: 12 additions & 1 deletion pallets/services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use pallet::*;
pub mod functions;
pub mod impl_services;
pub mod interface;
pub mod migrations;
pub mod types;
pub mod weights;

Expand All @@ -13,6 +14,11 @@ pub use traits_services::{types::ServiceFlow, ServiceOwner, ServicesProvider};
pub use types::*;
pub use weights::WeightInfo;

pub use frame_support::traits::StorageVersion;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -32,12 +38,17 @@ pub mod pallet {

// ----- This is template code, every pallet needs this ---
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migrations::migrate::<T>()
}
}
// --------------------------------------------------------

// ------- Storage -------------
Expand Down
101 changes: 101 additions & 0 deletions pallets/services/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::{AccountIdOf, BalanceOf, Config, HashOf, Pallet, Service, ServiceInfo, Services};
use frame_support::{
pallet_prelude::{Decode, Encode},
traits::Get,
weights::Weight,
};
use primitives_duration::{DurationType, ExpectedDuration};
use primitives_price_and_currency::PriceByCurrency;
use sp_std::vec::Vec;
use traits_services::types::ServiceFlow;

pub fn migrate<T: Config>() -> Weight {
use frame_support::traits::StorageVersion;

let mut weight: Weight = 0;
let mut version = StorageVersion::get::<Pallet<T>>();

if version < 1 {
weight = weight.saturating_add(version::v1::migrate::<T>());
version = StorageVersion::new(1);
}

version.put::<Pallet<T>>();
weight
}

mod version {
use super::*;

pub mod v1 {
use super::*;

pub fn migrate<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().writes(1);

#[derive(Encode, Decode)]
pub struct OldExpectedDuration {
pub duration: i8,
pub duration_type: DurationType,
}

#[derive(Encode, Decode)]
pub struct OldServiceInfo<Balance> {
pub name: Vec<u8>,
pub prices_by_currency: Vec<PriceByCurrency<Balance>>,
pub expected_duration: OldExpectedDuration,
pub category: Vec<u8>,
pub description: Vec<u8>, // TODO: limit the length
pub dna_collection_process: Vec<u8>,
pub test_result_sample: Vec<u8>,
pub long_description: Option<Vec<u8>>,
pub image: Option<Vec<u8>>,
}

#[derive(Encode, Decode)]
pub struct OldService<AccountId, Hash, Balance> {
pub id: Hash,
pub owner_id: AccountId,
pub info: OldServiceInfo<Balance>,
pub service_flow: ServiceFlow,
}

pub type OldServiceOf<T> = OldService<AccountIdOf<T>, HashOf<T>, BalanceOf<T>>;

Services::<T>::translate(|_key, old_services: OldServiceOf<T>| {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));

let old_service_info = &old_services.info;

let old_expected_duration = &old_service_info.expected_duration;
let old_duration = old_expected_duration.duration;
let old_duration_type = old_expected_duration.duration_type.clone();
let expected_duration = ExpectedDuration {
duration: old_duration as u64,
duration_type: old_duration_type,
};

let service_info = ServiceInfo {
name: old_service_info.name.clone(),
prices_by_currency: old_service_info.prices_by_currency.clone(),
expected_duration,
category: old_service_info.category.clone(),
description: old_service_info.description.clone(),
dna_collection_process: old_service_info.dna_collection_process.clone(),
test_result_sample: old_service_info.test_result_sample.clone(),
long_description: old_service_info.long_description.clone(),
image: old_service_info.image.clone(),
};

Some(Service {
id: old_services.id,
owner_id: old_services.owner_id,
info: service_info,
service_flow: old_services.service_flow,
})
});

weight
}
}
}

0 comments on commit 04e9b0d

Please sign in to comment.