Skip to content

Commit bcf21aa

Browse files
committed
Update example for migration
1 parent 3603b5d commit bcf21aa

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

.github/workflows/doc-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
cargo update -p cosmwasm-schema-derive
2727
cargo update -p cosmwasm-schema
2828
cargo update -p cosmwasm-std
29+
cargo update -p cw2
2930
- uses: Swatinem/rust-cache@v2
3031
with:
3132
workspaces: |

docs-test-gen/Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-test-gen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ pulldown-cmark = { version = "0.11.0", default-features = false }
1212
strum = { version = "0.26.2", features = ["derive"] }
1313

1414
[dev-dependencies]
15+
cw2 = "*"
1516
cosmwasm-schema = "*"
1617
cosmwasm-std = { version = "*", features = ["stargate", "staking", "cosmwasm_2_0"] }

docs-test-gen/templates/core.tpl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,11 @@ struct MigrateMsg {}
2121
#[cw_serde]
2222
struct SudoMsg {}
2323

24-
fn from_slice<T: Default>(_data: &[u8]) -> StdResult<T> {
25-
Ok(T::default())
26-
}
27-
28-
fn to_vec<T>(_data: &T) -> StdResult<Vec<u8>> {
29-
Ok(Vec::new())
30-
}
31-
3224
fn transform(_old_data: OldData) -> () {
3325
()
3426
}
3527

28+
#[cw_serde]
3629
#[derive(Default)]
3730
struct OldData {}
3831

src/pages/core/entrypoints/migrate.mdx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,26 @@ the storage in there.
4848
## Example
4949

5050
```rust filename="contract.rs" template="core"
51-
const STATE_VERSION: u64 = 1;
51+
const CONTRACT_NAME: &str = "my_contract";
52+
const STATE_VERSION: &str = "v2";
5253

5354
#[entry_point]
5455
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
5556
// Check if the state version is older than the current one
56-
let Some(current_state_version) = deps.storage.get(b"state") else {
57-
return Err(StdError::generic_err("State not found"));
58-
};
59-
let current_state_version: u64 = from_slice(&current_state_version)?;
60-
61-
if current_state_version < STATE_VERSION {
62-
return Ok(Response::default());
63-
}
57+
cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, STATE_VERSION)?;
6458

6559
// Load the old data
6660
let Some(old_data) = deps.storage.get(b"persisted_data") else {
6761
return Err(StdError::generic_err("Data not found"));
6862
};
6963
// Deserialize it from the old format
70-
let old_data: OldData = from_slice(&old_data)?;
64+
let old_data: OldData = cosmwasm_std::from_json(&old_data)?;
7165

7266
// Transform it
7367
let new_data = transform(old_data);
7468

7569
// Serialize the new data
76-
let new_data = to_vec(&new_data)?;
70+
let new_data = cosmwasm_std::to_json_vec(&new_data)?;
7771
// Store the new data
7872
deps.storage.set(b"persisted_data", &new_data);
7973

0 commit comments

Comments
 (0)