Skip to content

Commit

Permalink
Update example for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 23, 2024
1 parent 3603b5d commit bcf21aa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/doc-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
cargo update -p cosmwasm-schema-derive
cargo update -p cosmwasm-schema
cargo update -p cosmwasm-std
cargo update -p cw2
- uses: Swatinem/rust-cache@v2
with:
workspaces: |
Expand Down
33 changes: 33 additions & 0 deletions docs-test-gen/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs-test-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pulldown-cmark = { version = "0.11.0", default-features = false }
strum = { version = "0.26.2", features = ["derive"] }

[dev-dependencies]
cw2 = "*"
cosmwasm-schema = "*"
cosmwasm-std = { version = "*", features = ["stargate", "staking", "cosmwasm_2_0"] }
9 changes: 1 addition & 8 deletions docs-test-gen/templates/core.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ struct MigrateMsg {}
#[cw_serde]
struct SudoMsg {}

fn from_slice<T: Default>(_data: &[u8]) -> StdResult<T> {
Ok(T::default())
}

fn to_vec<T>(_data: &T) -> StdResult<Vec<u8>> {
Ok(Vec::new())
}

fn transform(_old_data: OldData) -> () {
()
}

#[cw_serde]
#[derive(Default)]
struct OldData {}

Expand Down
16 changes: 5 additions & 11 deletions src/pages/core/entrypoints/migrate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,26 @@ the storage in there.
## Example

```rust filename="contract.rs" template="core"
const STATE_VERSION: u64 = 1;
const CONTRACT_NAME: &str = "my_contract";
const STATE_VERSION: &str = "v2";

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
// Check if the state version is older than the current one
let Some(current_state_version) = deps.storage.get(b"state") else {
return Err(StdError::generic_err("State not found"));
};
let current_state_version: u64 = from_slice(&current_state_version)?;

if current_state_version < STATE_VERSION {
return Ok(Response::default());
}
cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, STATE_VERSION)?;

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

// Transform it
let new_data = transform(old_data);

// Serialize the new data
let new_data = to_vec(&new_data)?;
let new_data = cosmwasm_std::to_json_vec(&new_data)?;
// Store the new data
deps.storage.set(b"persisted_data", &new_data);

Expand Down

0 comments on commit bcf21aa

Please sign in to comment.