|
| 1 | +# Migrate |
| 2 | + |
| 3 | +This is another special entrypoint. It is, just like [`instantiate`](./instantiate), not called frequently. |
| 4 | + |
| 5 | +`migrate` is only called once you upload a new version of your contract to the chain |
| 6 | + and lets you run all the required changes to the storage. |
| 7 | + |
| 8 | +Let's say your storage has the following layout, expressed as JSON for simplicity: |
| 9 | + |
| 10 | +::: code-group |
| 11 | + |
| 12 | +```JSON [structure.json] |
| 13 | +{ |
| 14 | + "user_count": 205, |
| 15 | + "call_count": 543, |
| 16 | + "balance": 43 |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +::: |
| 21 | + |
| 22 | + |
| 23 | +But then you notice "Hey! Why don't I nest all the counts into an own object? That way I don't have |
| 24 | +that redundant postfix, making the keys smaller". |
| 25 | + |
| 26 | +So you go ahead and rework your logic to query the data from the following structure: |
| 27 | + |
| 28 | +::: code-group |
| 29 | + |
| 30 | +```JSON [structure.json] |
| 31 | +{ |
| 32 | + "count": { |
| 33 | + "user": 205, |
| 34 | + "call": 543 |
| 35 | + }, |
| 36 | + "balance": 43 |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +::: |
| 41 | + |
| 42 | +But your storage on chain still stores the old format. You need to transform it somehow. |
| 43 | + |
| 44 | +That's what you do in the `migrate` entrypoint. You transform the structure of the storage. |
| 45 | + |
| 46 | +## Example |
| 47 | + |
| 48 | +For CosmWasm `v2.2.0` and newer, the new migrate info feature can be used: |
| 49 | + |
| 50 | +::: code-group |
| 51 | + |
| 52 | +```Rust [contract.rs] |
| 53 | +const MIGRATE_VERSION: u64 = 2; |
| 54 | + |
| 55 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 56 | +#[migrate_version(MIGRATE_VERSION)] |
| 57 | +pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg, migrate_info: MigrateInfo) -> StdResult<Response> { |
| 58 | + match migrate_info.old_migrate_version { |
| 59 | + Some(1) | None => { |
| 60 | + // If the old on-chain version of the contract don't use the |
| 61 | + // `migrate_version` macro, there will be no version provided here |
| 62 | + // (it's the `None` variant). |
| 63 | + |
| 64 | + // Load the old data |
| 65 | + let Some(old_data) = deps.storage.get(b"persisted_data") else { |
| 66 | + return Err(StdError::generic_err("Data not found")); |
| 67 | + }; |
| 68 | + // Deserialize it from the old format |
| 69 | + let old_data: OldData = cosmwasm_std::from_json(&old_data)?; |
| 70 | + |
| 71 | + // Transform it |
| 72 | + let new_data = transform(old_data); |
| 73 | + |
| 74 | + // Serialize the new data |
| 75 | + let new_data = cosmwasm_std::to_json_vec(&new_data)?; |
| 76 | + // Store the new data |
| 77 | + deps.storage.set(b"persisted_data", &new_data); |
| 78 | + } |
| 79 | + Some(x) if x >= MIGRATE_VERSION => { |
| 80 | + // Assume we don't support downgrading the contract's state version |
| 81 | + // Note that `migrate_info.old_migrate_version` is never eq to `MIGRATE_VERSION`. |
| 82 | + return Err(StdError::generic_err("Downgrades are not supported for this contract.")); |
| 83 | + } |
| 84 | + _ => { |
| 85 | + return Err(StdError::generic_err("Unexpected migrate version.")); |
| 86 | + } |
| 87 | + } |
| 88 | + Ok(Response::default()) |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +::: |
| 93 | + |
| 94 | +For CosmWasm versions before `v2.2.0` the old method looks like this: |
| 95 | + |
| 96 | +::: code-group |
| 97 | + |
| 98 | +```Rust [contract.rs] |
| 99 | +const STATE_VERSION: &str = "v2"; |
| 100 | +const CONTRACT_NAME: &str = "my_contract"; |
| 101 | + |
| 102 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 103 | +pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> { |
| 104 | + // Check if the state version is older than the current one and update it |
| 105 | + cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, STATE_VERSION)?; |
| 106 | + |
| 107 | + // Load the old data |
| 108 | + let Some(old_data) = deps.storage.get(b"persisted_data") else { |
| 109 | + return Err(StdError::generic_err("Data not found")); |
| 110 | + }; |
| 111 | + // Deserialize it from the old format |
| 112 | + let old_data: OldData = cosmwasm_std::from_json(&old_data)?; |
| 113 | + |
| 114 | + // Transform it |
| 115 | + let new_data = transform(old_data); |
| 116 | + |
| 117 | + // Serialize the new data |
| 118 | + let new_data = cosmwasm_std::to_json_vec(&new_data)?; |
| 119 | + // Store the new data |
| 120 | + deps.storage.set(b"persisted_data", &new_data); |
| 121 | + |
| 122 | + Ok(Response::default()) |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +::: |
| 127 | + |
| 128 | +## Definition |
| 129 | + |
| 130 | +To get the additional migrate info, the new signature can be used (CosmWasm `v2.2.0` and newer): |
| 131 | + |
| 132 | +::: code-group |
| 133 | + |
| 134 | +```Rust [contract.rs] |
| 135 | +const MIGRATE_VERSION: u64 = 2; |
| 136 | + |
| 137 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 138 | +#[migrate_version(MIGRATE_VERSION)] |
| 139 | +pub fn migrate( |
| 140 | + deps: DepsMut, |
| 141 | + env: Env, |
| 142 | + msg: MigrateMsg, |
| 143 | + migrate_info: MigrateInfo) -> StdResult<Response> { |
| 144 | + // TODO: Put your migration logic here. |
| 145 | + Ok(Response::default()) |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +::: |
| 150 | + |
| 151 | +The legacy CosmWasm (before version `v2.2.0`) migrate entrypoint function signature is defined like this: |
| 152 | + |
| 153 | +::: code-group |
| 154 | + |
| 155 | +```Rust [contract.rs] |
| 156 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 157 | +pub fn migrate( |
| 158 | + deps: DepsMut, |
| 159 | + env: Env, |
| 160 | + msg: MigrateMsg) -> StdResult<Response> { |
| 161 | + // TODO: Put your migration logic here. |
| 162 | + Ok(Response::default()) |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +::: |
0 commit comments