Skip to content

Commit

Permalink
Add migrate example
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 21, 2024
1 parent 9c82668 commit 5dd3110
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 51 deletions.
96 changes: 48 additions & 48 deletions docs-test-gen/Cargo.lock

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

2 changes: 1 addition & 1 deletion docs-test-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
anyhow = "1.0.82"
glob = "0.3.1"
phf = { version = "0.11.2", features = ["macros"] }
pulldown-cmark = { version = "0.10.3", default-features = false }
pulldown-cmark = { version = "0.11.0", default-features = false }
strum = { version = "0.26.2", features = ["derive"] }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions docs-test-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use strum::{AsRefStr, EnumIter, IntoEnumIterator};

static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
"core" => include_str!("../templates/core.tpl"),
"core-migrate" => include_str!("../templates/core-migrate.tpl"),
"execute" => include_str!("../templates/execute.tpl"),
};

Expand Down
25 changes: 25 additions & 0 deletions docs-test-gen/templates/core-migrate.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cosmwasm_std::*;
use cosmwasm_schema::cw_serde;

#[derive(Default)]
struct OldData {}

#[cw_serde]
struct MigrateMsg {}

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) -> () {
()
}

#[test]
fn doctest() {
{{code}}
}
40 changes: 38 additions & 2 deletions src/pages/core/entrypoints/migrate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Callout } from "nextra/components";
This is another special endpoint. It is, just like `instantiate`, not called
frequently.

`migrate` is only called once you upload a new version to the chain
and lets you run all the required changes to the storage.
`migrate` is only called once you upload a new version to the chain and lets you
run all the required changes to the storage.

Let's say your storage has the following layout, expressed as JSON for
simplicity's sake:
Expand Down Expand Up @@ -45,6 +45,42 @@ transform it.
That's what you do in the `migrate` entrypoint. You transform the structure of
the storage in there.

## Example

```rust filename="contract.rs" template="core-migrate"
const STATE_VERSION: u64 = 1;

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
// Check if the state version is olden 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());
}

// 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)?;

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

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

Ok(Response::default())
}
```

## Definition

```rust filename="contract.rs" template="core"
Expand Down

0 comments on commit 5dd3110

Please sign in to comment.