migration.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use crate::forum;
  2. use crate::storage;
  3. use crate::VERSION;
  4. use sr_primitives::print;
  5. use srml_support::{decl_event, decl_module, decl_storage};
  6. use sudo;
  7. use system;
  8. // When preparing a new major runtime release version bump this value to match it and update
  9. // the initialization code in runtime_initialization(). Because of the way substrate runs runtime code
  10. // the runtime doesn't need to maintain any logic for old migrations. All knowledge about state of the chain and runtime
  11. // prior to the new runtime taking over is implicit in the migration code implementation. If assumptions are incorrect
  12. // behaviour is undefined.
  13. const MIGRATION_FOR_SPEC_VERSION: u32 = 0;
  14. impl<T: Trait> Module<T> {
  15. fn runtime_initialization() {
  16. if VERSION.spec_version != MIGRATION_FOR_SPEC_VERSION {
  17. return;
  18. }
  19. print("running runtime initializers");
  20. // ...
  21. // add initialization of other modules introduced in this runtime
  22. // ...
  23. Self::deposit_event(RawEvent::Migrated(
  24. <system::Module<T>>::block_number(),
  25. VERSION.spec_version,
  26. ));
  27. }
  28. }
  29. pub trait Trait:
  30. system::Trait
  31. + storage::data_directory::Trait
  32. + storage::data_object_storage_registry::Trait
  33. + forum::Trait
  34. + sudo::Trait
  35. {
  36. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
  37. }
  38. decl_storage! {
  39. trait Store for Module<T: Trait> as Migration {
  40. /// Records at what runtime spec version the store was initialized. This allows the runtime
  41. /// to know when to run initialize code if it was installed as an update.
  42. pub SpecVersion get(spec_version) build(|_| VERSION.spec_version) : Option<u32>;
  43. }
  44. }
  45. decl_event! {
  46. pub enum Event<T> where <T as system::Trait>::BlockNumber {
  47. Migrated(BlockNumber, u32),
  48. }
  49. }
  50. decl_module! {
  51. pub struct Module<T: Trait> for enum Call where origin: T::Origin {
  52. fn deposit_event() = default;
  53. fn on_initialize(_now: T::BlockNumber) {
  54. if Self::spec_version().map_or(true, |spec_version| VERSION.spec_version > spec_version) {
  55. // mark store version with current version of the runtime
  56. SpecVersion::put(VERSION.spec_version);
  57. // run migrations and store initializers
  58. Self::runtime_initialization();
  59. }
  60. }
  61. }
  62. }