How to deploy project after modifying the elements of a structure

Hey guys,here is my first post on dfinity forum.I use rust to write the backend of this project,but here is the problem:I modified a strucuture with changing some new elements,but when I wanted to deploy the backend,it warned:

WARNING
Candid interface compatibility check failed for canister 'backend'.
You are making a BREAKING change.Other canisters or frontend clients relying on your canister may stop working.
Method get_payload_from_stable_mem_simple_raw:func ()-(CanisterContext)query is not a subtype of func ()-(CanisterContext/1)query
Do you want to proceed?yes/No

I think the reason of it is that I didn’t targetly clean the old data,but I don’t know how to clean,somebody can help me?Thanks a lot!

Welcome!

It’s warning you that you’ve changed the structure of the CanisterContext type, which is used in one of the methods in your backend canister’s interface.

If the canister was deployed on mainnet then other canisters or clients might exist that still send data to the canister using the old type, which won’t map to the new type. But if you’re developing locally you can just proceed past the warning to deploy.

Aside, if you need to upgrade types in a backward compatible way you can follow the subtyping rules, which would allow the method to still handle data sent using the old type. See https://internetcomputer.org/docs/current/motoko/main/canister-maintenance/compatibility/#evolving-the-candid-interface (the “Candid interface” examples are the ones that concern you here).

Thank you!I want to know what would happen if I clicked yes?The modified structure’s old data would be cleaned or the entire data would be cleaned?

Not all the data will be cleaned, the data related to the changed structure might be cleaned depending on the change you’re making.

Oh thanks a lot man,I’m trying to test this problem,really aapreciate!

Okay,man,here is a new problem:I changed my structure by adding a new property,then I deployed the backend,and it showed warning information in the title,and I clicked “yes”,then it showed error:

2024-05-12 16:22:34.312445810 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai] Panicked at '!!!! deserialize_error: !!!! Error("missing field `test_field`", line: 1, column: 364)', backend/canisters/backend/src/common/life_cycle.rs:93:7
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'backend'.
      Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai trapped explicitly: Panicked at '!!!! deserialize_error: !!!! Error("missing field `test_field`", line: 1, column: 364)', backend/canisters/backend/src/common/life_cycle.rs:93:7, error code None

I don’t know what caused it,do you know that?

can you send a screenshot of your code or maybe the change you made?


Look,I just add a property for testing,nothing more.

I think the reason you’re getting this error is that you have to make the new parameters of functions optional, so if there is a frontend working with the previos version of your canister doesn’t face any problems.
the second reason is you had to define test_field somewhere else as well, but you didn’t

Try to fix these two things

Thanks a lot man,I will try to fix them and give you the feedback

If the UserConfig type is being used in a “stable” variable to persist data between upgrades then changing this in a non-backward compatible way would cause an error as the canister attempts to repopulate the data post-upgrade.

The same subtyping rules linked above are relevant here. (But this would be about changing the stable data types that are storing your user data, rather than changing the canister’s interface/Candid).

This page has a general overview of all this: 2.1 Canister upgrades, storage, and persistence | Internet Computer

If you want to change stable data structures without adhering to subtyping rules then you could add some migration code in the post_upgrade hook to move data between the old and new types.