Skip to content

Instantly share code, notes, and snippets.

@ethanfrey
Created November 3, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanfrey/ee8959ccfcb97604eb6298f1e22abfc4 to your computer and use it in GitHub Desktop.
Save ethanfrey/ee8959ccfcb97604eb6298f1e22abfc4 to your computer and use it in GitHub Desktop.
Basic migration skeleton
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