Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cw1-subkeys: Migration example #525

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion contracts/cw1-subkeys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ cosmwasm-std = { version = "1.0.0-beta", features = ["staking"] }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.10.2" }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.23" }
thiserror = "1.0.23"
semver = "1"

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0-beta" }
Expand Down
19 changes: 18 additions & 1 deletion contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use cw1_whitelist::{
msg::InstantiateMsg,
state::ADMIN_LIST,
};
use cw2::set_contract_version;
use cw2::{get_contract_version, set_contract_version};
use cw_storage_plus::Bound;
use semver::Version;

use crate::error::ContractError;
use crate::msg::{
Expand Down Expand Up @@ -455,6 +456,22 @@ pub fn query_all_permissions(
Ok(AllPermissionsResponse { permissions: res? })
}

// Migrate contract if version is lower than current version
#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
let version: Version = CONTRACT_VERSION.parse()?;
let storage_version: Version = get_contract_version(deps.storage)?.version.parse()?;

if storage_version < version {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

// If state structure changed in any contract version in the way migration is needed, it
// should occur here
}

Ok(Response::new())
}

#[cfg(test)]
mod tests {
use cosmwasm_std::testing::{
Expand Down
9 changes: 9 additions & 0 deletions contracts/cw1-subkeys/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub enum ContractError {

#[error("Allowance already expired while setting: {0}")]
SettingExpiredAllowance(Expiration),

#[error("Semver parsing error: {0}")]
SemVer(String),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be moved to StdError if we decide this is common one.

}

impl From<cw1_whitelist::ContractError> for ContractError {
Expand All @@ -52,3 +55,9 @@ impl From<cw1_whitelist::ContractError> for ContractError {
}
}
}

impl From<semver::Error> for ContractError {
fn from(err: semver::Error) -> Self {
Self::SemVer(err.to_string())
}
}