Skip to content

Commit

Permalink
Merge pull request #1289 from demergent-labs/fix_pre_and_post_upgrade
Browse files Browse the repository at this point in the history
fixing pre_and_post_upgrade functionality and test
  • Loading branch information
lastmjs authored Sep 26, 2023
2 parents 38e684c + 7e029b4 commit aef582c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/pre_and_post_upgrade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default Service({
Object.entries(entries).map((entry) => {
return {
key: entry[0],
value: entry[1]
value: entry[1] + 1n
};
})
);
Expand Down
4 changes: 2 additions & 2 deletions examples/pre_and_post_upgrade/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getTests(
{
name: 'deploy',
prep: async () => {
execSync(`dfx deploy`, {
execSync(`dfx deploy --upgrade-unchanged`, {
stdio: 'inherit'
});
}
Expand All @@ -60,7 +60,7 @@ export function getTests(
Ok:
result.length === 1 &&
result[0].key === '0' &&
result[0].value === 0n
result[0].value === 1n
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// TODO So I'm going to create a macro for the dynamic portion of the code here
// TODO Then I'm going to integrate rquickjs

use crate::traits::to_ident::ToIdent;
use quote::quote;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -56,6 +59,16 @@ fn main() -> Result<(), String> {
quote!(execute_js(#js_function_name, true);)
});

let post_upgrade_method_call =
compiler_info
.canister_methods
.post_upgrade
.map(|post_upgrade_method| {
let js_function_name = &post_upgrade_method.name;

quote!(execute_js(#js_function_name, true);)
});

let pre_upgrade_method = compiler_info
.canister_methods
.pre_upgrade
Expand Down Expand Up @@ -243,20 +256,12 @@ fn main() -> Result<(), String> {
context.eval_global("exports.js", "globalThis.exports = {}").unwrap();
context.eval_global("main.js", std::str::from_utf8(MAIN_JS).unwrap()).unwrap();

let _azle_post_upgrade_name = global.get_property("_azlePostUpgradeName").unwrap();
let _azle_post_upgrade_name_string = if !_azle_post_upgrade_name.is_undefined() {
let _azle_post_upgrade_name_js_value: JSValue = from_qjs_value(&_azle_post_upgrade_name).unwrap();
_azle_post_upgrade_name_js_value.try_into().unwrap()
} else { "".to_string() };

CONTEXT.with(|ctx| {
let mut ctx = ctx.borrow_mut();
*ctx = Some(context);
});

if _azle_post_upgrade_name_string != "" {
execute_js(&_azle_post_upgrade_name_string, true);
}
#post_upgrade_method_call
}

#pre_upgrade_method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function generateLibCargoToml(
ic-wasi-polyfill = { git = "https://github.com/demergent-labs/ic-wasi-polyfill", rev = "f3812b879c096faf930331e76441fadf2ebe6586", features = ["transient"] }
quickjs-wasm-rs = {git = "https://github.com/ulan/javy.git", branch="ulan/local-changes"}
# rquickjs = "0.3.1"
futures-core = "=0.3.27"
anyhow = "1.0.75"
Expand Down
4 changes: 2 additions & 2 deletions src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export function Service<T extends ServiceOptions>(
`;

returnFunction.init = init;
returnFunction.postUpgrade = postUpgrade;
returnFunction.preUpgrade = preUpgrade;
returnFunction.post_upgrade = postUpgrade;
returnFunction.pre_upgrade = preUpgrade;
returnFunction.heartbeat = heartbeat;
returnFunction.inspect_message = inspectMessage;
returnFunction.queries = queries;
Expand Down
6 changes: 5 additions & 1 deletion src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export function executeMethod(

const result = callback(...myDecodedObject);

if (mode === 'init' || mode === 'inspectMessage') {
if (
mode === 'init' ||
mode === 'postUpgrade' ||
mode === 'inspectMessage'
) {
return;
}

Expand Down

0 comments on commit aef582c

Please sign in to comment.