How to use the scheduler pallet for storage migrations
🎯
#
GoalImplement storage migration logic using Substrate’s scheduler pallet for non-core migrations.
#
Use cases- Removing old unused storage during a runtime upgrade
- Migrating storage unrelated to the core logic of the chain (if the upgraded logic does not depend on the new storage format).
#
OverviewThis guide outlines steps to schedule non-core storage or other runtime migrations using the scheduler pallet.
#
Steps#
1. Define extrinsics in the runtimeDefine extrinsics that you want to be scheduled. Ensure the extrinsics can only be called via root or a pallet origin. When scheduling the extrinsics from your pallet, use this origin.
#
2. Add the Scheduler type to the Configuration trait.You need to define the Scheduler trait in your Config trait to access the Scheduler pallet from your pallet. Traits in the Scheduler pallet require a defined Call type as a parameter. You can also define the Call type in your Config trait to match your on-chain Scheduler pallet.
on_runtime_upgrade
hook#
3. Schedule the calls within the - use the
schedule_named
method from the Scheduler pallet to trigger your migration extrinsic.
tip
Schedule the extrinsic for the blocks after the migration executes. If it takes an unknown length of time to execute, set up a counter within the extrinsic to make sure that it stops once it hits a certain weight and then schedules itself again for the next block.
#
4. Trigger the migrations.Once you have defined the calls and the on_runtime_upgrade
hook you can trigger the migrations by upgrading the runtime as described here
#
Examples- Calls scheduling in the democracy pallet
#
Resources- Scheduler pallet implementation
- Manual storage migration