Here is part of my backend code of actor.rs
Is it because the pre_upgrade() didnt even auto called when executing dfx deploy backend
?
//...
/**
* 1. each time upgrade(cmd : dfx deploy ),
* will *erase* all ic-DB (canister stable memory)
* so we can:
* 1.manully erase all,
* 2.or , restore from a in memory data.(such as a hashmap)
*
*
* 2. transational upgrade:
* if pre_upgrade, upgrade ,post_upgrade
* any step go wrong.
* will revert to last version.
*
*
*/
#[pre_upgrade]
fn pre_upgrade() {
let canister_id = id();
print(format!("starting pre_upgrade {:?}", canister_id));
CONTEXT.with(|c| {
let context = c.borrow();
let id = context.id;
// get users list from vilotile storage (computer memory)
let users = Vec::from_iter(context.user_service.users.values().cloned());
let db: CanisterDB = CanisterDB { id, users };
// println!("{:#?}", db);
// IMPORTANT save all userdata into IC-DB
storage::stable_save((db,)).expect("failed to save state data");
// IMPORTANT erase db in running canister.(ic or local)
// in bash, maybe this need to do :
// dfx deploy backend --network ic -m reinstall
// let _empty_db = CanisterDB::default();
// storage::stable_save((_empty_db,)).expect("failed to save state data");
print(format!("started pre_upgrade {:?}", canister_id));
});
}
//...