Created
November 3, 2021 19:46
Basic migration skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CONTRACT_NAME: &str = "crates.io:my-crate-name"; | |
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | |
#[entry_point] | |
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> { | |
let ver = cw2::get_contract_version(deps.storage)?; | |
// ensure we are migrating from an allowed contract | |
if ver.contract != CONTRACT_NAME { | |
return Err(StdError::generic_err("Can only upgrade from same type").into()); | |
} | |
// note: better to do proper semver compare, but string compare *usually* works | |
if ver.version >= CONTRACT_VERSION { | |
return Err(StdError::generic_err("Cannot upgrade from a newer version").into()); | |
} | |
// set the new version | |
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | |
// do any desired state migrations... | |
Ok(Response::default()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment