# Substrate Runtime Migration Guide
Living document evolving over time.
When you want to change the logic of a running Substrate blockchain you will need to execute a [forkless runtime upgrade](https://substrate.dev/docs/en/tutorials/upgrade-a-chain/). This upgrade will change the logic of your chain, but the logic depends on the state following a certain schema. As a result you will need to include storage migrations that transform the state from the previous into the current schema. We call this transition a runtime storage migration.
This guide aims to give some help with and context for Substrate runtime storage migrations.
Any runtime migration project can be divided into six phases:
2. **Writing Migrations**
3. **Creating the Runtime Upgrade**
We will go through the phases in order and point out relevant considerations and resources.
## 1. Preparation
Before actually migrating you need to figure out which migrations to include in your runtime upgrade. Depending on which version you are migrating from (and to) you will need to decide whether to include these migrations. Note that you will also need to determine whether and how the migrations apply to your particular chain as well as their order.
All Substrate migration PRs since the start of 2020 are tagged with `D1-runtime-migration` [in this PR listing here](https://github.com/paritytech/substrate/pulls?q=is%3Apr+label%3AD1-runtime-migration). You will need to port these to your chain which might include making adjustments.
Referencing the Edgeware migration here as it might be helpful because includes quite a few migrations. [Tracking issue](https://github.com/hicommonwealth/edgeware-node/issues/164).
If you need to determine how the runtime has changed between versions you can diff the metadata. You can use `docker run --network=host jacogr/polkadot-js-tools metadata <ws-old> <ws-new>` (e.g. `docker run --network=host jacogr/polkadot-js-tools metadata wss://rpc.polkadot.io ws://localhost:9944` to compare a local instance with the Polkadot production chain) to determine which modules have changes.
#### The Hasher Migration
If you are migrating from an earlier version of Substrate (`v2.0.0-alpha.3` or earlier; before March 2020) you will need to pay attention to a notable migration.
*The Hasher Migration* was a complicated migration that migrated away from opaque hashers (see [the PR here](https://github.com/paritytech/substrate/pull/5226)).
**Note**: There were issues in the original hasher migration so don't copy it directly and read the comments left on the PR.
If you want to use a similar migration strategy to the one originally chosen for Kusama you will have to provide a [SCALE](https://github.com/paritytech/parity-scale-codec) encoded file of accounts to migrate.
## 2. Writing the Migration
If you are a pallet author or you are not just running migrations already included in the pallets you are using you will have to write your own migrations.
When writing new migrations keep the following in mind:
+ Make sure to include storage version checks so that your migration only runs when migrating from the correct version and only executes once.
+ See [this recent phragmen migration](https://github.com/paritytech/substrate/pull/7040/files#diff-194656be9ce9133b248a5b7cd18842eb5d3faacde3f1221b32e654419870136aR129) for how to do this manually.
+ See the [Pallet Storage Version PR](https://github.com/paritytech/substrate/pull/7208) for how we want to do these version check in the future.
+ You are "looking at" the data from the "perspective" of the new runtime. You need to provide the correct (potentially deprecated) types to decode the data and migrate it into the current format.
+ See [this migration of indices](https://github.com/hicommonwealth/substrate/pull/14/files#diff-00c91017af35e93cc641a7bc0d73202f7bd349f13d5bc7f0736d7a272919195dR5-R22) for an example.
+ Include logging, including whether the migration ran and whether there were any errors. It will help with manual integration testing.
+ Prefer making migrations reusable by e.g. extracting them out into functions. This will allow easier testing and will potentially allow others to use them.
+ See [this example](https://github.com/paritytech/substrate/pull/7040/files#diff-194656be9ce9133b248a5b7cd18842eb5d3faacde3f1221b32e654419870136aR128) (same phragmen migration as above).
+ Add a unit test if your migration includes any logic
## 3. Creating the Runtime Upgrade
When you have identified which migrations to include you need to actually create the runtime upgrade that will apply them. This means choosing how the migrations are applied and in which order.
The default of applying the migrations one pallet after the other will work for the common case but might not be suitable for more complicated situations. When wiring the migrations into the runtime you can place them in a custom order by using the custom runtime upgrade facilities. See [here](https://github.com/hicommonwealth/edgeware-node/blob/7b66f4f0a9ec184fdebcccd41533acc728ebe9dc/node/runtime/src/lib.rs#L845-L866) for an example.
#### Note for Custom Migration Orders
Note the execution order (as defined in the code [here](https://github.com/paritytech/substrate/blob/d766e229466d63afadd19097e277d85146fee3c9/frame/executive/src/lib.rs#L231-L257)):
2. Custom `on_runtime_upgrade`
3. All pallet `on_runtime_upgrade`s in reverse order of declaration (see [this tracking issue](https://github.com/paritytech/substrate/issues/6280)).
6. All pallet `on_initialize`s in reverse order of declaration (see [this tracking issue](https://github.com/paritytech/substrate/issues/6280)).
This determines which state is migrated and initialized when. It means e.g. that you cannot access the block number in a migration because it is set in `frame_system::initialize`.
## 4. Testing
Runtime storage migrations should be tested in order to reduce the risk of a runtime migration mangling the storage.
### Unit Tests
You can run unit tests on generated as well as unit-like tests on live data.
1. Populate the storage of a test externalities environment.
2. Run the migration function.
3. Assert the correct shape of the new data.
#### Populate Storage
This PR shows how to populate the storage from raw data [here](https://github.com/paritytech/substrate/pull/4474/files#diff-64c049b4e94a55bdeaf757c725cf7d1df623b754557af4ced157024a943c4703R3076) and [here](https://github.com/paritytech/substrate/pull/4474/files#diff-9c165a4d56d77f122b251056c560ece123c5c944d949299b99614d0d17b36f4dR17).
Check out [`remote-externalities`](https://github.com/paritytech/substrate-debug-kit/tree/master/remote-externalities) if you want to use live chain data for your migration tests.
### Manual Integration Testing
You can fork off from the live chain to then run the migration locally on live chain data but with a different chain spec.
[The `fork-off-substrate` tool](https://github.com/maxsam4/fork-off-substrate) is aimed at making this easier.
## 5. Execution
Sometimes a new runtime will make use of new host functions which need to be supported by the nodes. You will want to make sure any node upgrades required for the runtime upgrade have high enough prevalence in your network before executing it.
## 6. Removal
As migrations are one-off pieces of code they can be removed after successful execution. The recommendation would be to leave the stand-alone migration function around for a while, but remove the usage in `on_runtime_upgrade` in time for the next runtime upgrade.
We are coordinating improvements to the migration experience in [this Github issue](https://github.com/paritytech/substrate/issues/6482).
### Vision for Migrations
We want storage migrations to be:
- **Correct:** Migrations should leave the chain in a correct state after the migration.
- **Testable:** It should be possible to determine whether they run correctly.
- **Composable:** Running several migrations one after another should not corrupt state.
- **Discoverable:** Migrations should be discoverable when users want to apply them or use them as inspiration for their own migration.
- **Documented:** Migrations should list assumptions so that users can verify whether they will work for their chain.
- **Reusable:** Migrations (especially simple ones) should be reusable so they can be applied to many chains and don't need to be rewritten.
- **Easy and Automatic:** Simple migrations should just work™ without a lot of manual integration.