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

Contract Migration? #12

Open
Reecepbcups opened this issue Jul 31, 2022 · 0 comments
Open

Contract Migration? #12

Reecepbcups opened this issue Jul 31, 2022 · 0 comments

Comments

@Reecepbcups
Copy link
Contributor

At the end just having a chapter on contract migration would be useful, took me a bit to learn how to do it via docs.
( This may be a good part-2 to #6 's chapter )

Could be as simple as this

pub struct ConfigResponse {
    pub name: String, // cw2 name
    pub version: String, // cw2 version from the .toml
}

pub const CONFIG = Item<ConfigResponse>

Then a migrate entry point which changes this value on upload

#[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());
    }
    
    if ver.version >= (*CONTRACT_VERSION).to_string() {
        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)?;

    // state breaking migrations here

    // update the version field in the CONFIG since we have updated the contract
    let mut config = CONFIG.load(deps.storage)?;
    config.version = CONTRACT_VERSION.to_string();
    CONFIG.save(deps.storage, &config)?;

    Ok(Response::default()
        .add_attribute("action", "migration")
        .add_attribute("version", CONTRACT_VERSION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant