|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +use crate::{Config, RetentionPeriod, LOG_TARGET}; |
| 17 | +use core::marker::PhantomData; |
| 18 | +use polkadot_sdk_frame::{ |
| 19 | + prelude::{BlockNumberFor, Weight}, |
| 20 | + traits::{Get, OnRuntimeUpgrade, Zero}, |
| 21 | +}; |
| 22 | + |
| 23 | +/// Runtime migration that sets the `RetentionPeriod` storage item to a |
| 24 | +/// non-zero `NewValue` value **only if it is currently zero**. |
| 25 | +/// |
| 26 | +/// Idempotent migration: safe to run multiple times |
| 27 | +pub struct SetRetentionPeriodIfZero<T, NewValue>(PhantomData<(T, NewValue)>); |
| 28 | +impl<T: Config, NewValue: Get<BlockNumberFor<T>>> OnRuntimeUpgrade |
| 29 | + for SetRetentionPeriodIfZero<T, NewValue> |
| 30 | +{ |
| 31 | + fn on_runtime_upgrade() -> Weight { |
| 32 | + let mut weight = T::DbWeight::get().reads(1); |
| 33 | + |
| 34 | + // If zero, let's reset. |
| 35 | + if RetentionPeriod::<T>::get().is_zero() { |
| 36 | + RetentionPeriod::<T>::set(NewValue::get()); |
| 37 | + weight.saturating_accrue(T::DbWeight::get().writes(1)); |
| 38 | + |
| 39 | + tracing::warn!( |
| 40 | + target: LOG_TARGET, |
| 41 | + "[SetRetentionPeriodIfZero] RetentionPeriod was zero; set to {:?}", |
| 42 | + NewValue::get() |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + weight |
| 47 | + } |
| 48 | +} |
0 commit comments