Lesson 6 of 9 · 2 min 23 sec

Upgrades: Ship New Code Over the Same Data

Chapters

  1. Sort the change
  2. Build, upgrade, check
  3. When the contract does not match
Transcript
Download transcript

Sort the change

This Shop is installed and in use. Branch ten holds item twenty with five left in stock, and the tax is seven hundred and fifty. We want to release new code without losing any of that. Sort the files into two groups. Endpoints, services and checks are business code, and you may change them in any release. The versioned memory files are the saved-data contract. Once a version has shipped, leave its file exactly as it is. The upgrade checks that the new build expects the same contract the canister saved.

Build, upgrade, check

Build the release with the SDK's builder. It produces the prepared Wasm to deploy, along with the client interface files. Deploy that prepared file and never the raw Cargo output. Use the same compiler setup, target and build options as the build that is running. Before upgrading, let work finish. A call that is still waiting on another canister must come back first. The SDK refuses an upgrade while its own calls, tasks or timers are outstanding. An app with longer jobs adds its own readiness check, which we will see in the last lesson. The upgrade request sets Wasm memory persistence to keep. The Shop test uses the same build on both sides. Its restore callback opens the saved data and binds the services again, as in lesson two. The saved values are still available. The check reads everything back through the public methods. Branch ten is there. The tax is seven hundred and fifty. Item twenty still costs one hundred and twenty-five with five in stock. And the app still works, because closing the branch succeeds.

When the contract does not match

What if a release changes the saved-data contract by accident? This test tries two builds with incompatible contracts. Each upgrade is refused. The test calls the canister again, and the old program still answers with the old values. So an ordinary upgrade changes code and keeps the contract. When the saved data itself has to change, you add a new version next to the old one and say how to convert. That is the next lesson.