Upgrades: Ship New Code Over the Same Data
Audience task and outcome
- Task Release a change to the Shop's business code on a canister that already holds data.
- Observable outcome The viewer can list what an ordinary upgrade needs from the author. They build with the canonical builder, let in-flight work finish, upgrade with the keep option, and check saved values afterward. They can name the files that are frozen once shipped. In the lifecycle check, the Shop still reports branch
10, tax750and item{price: 125, stock: 5}after the upgrade. - Starting point Lesson 2 wrote the
restorecallback that rebuilds services. Lesson 5 introduced awaited calls. - Handoff Lesson 7 takes the case this film stops at, a change to the saved data itself.
- Pacing Three and a half minutes.
The worked example
The example lifecycle check builds Quickstart and the Shop with the canonical builder and installs them on PocketIC. For the Shop it opens branch 10, stocks item 20 at price 125 with stock 7, checks out 2 for 250, sets tax 750, and upgrades with wasm_memory_persistence set to keep. Afterward it reads branches [10], tax 750 and item {price: 125, stock: 5}, then closes the branch. Quickstart does the same with one score, 7 → 99. Lifecycle check · Shop root · Settings data V1 · Build an application
That check upgrades each app to the same build. Two other fixtures supply the cases around it. The Nested native lifecycle fixture upgrades to a separately built image whose business code differs, with the same memory declarations, and its saved values are unchanged. The Root authoring probe tries two images whose declared contract differs without a declared conversion, and both are rejected while the old program keeps serving. Nested native lifecycle fixture · Business-change assertions · Root authoring probe · Rejection assertions
The upgrade guide is the authority for what is admitted and what the application must do first. Root upgrades
The story in one pass
Start with a populated Shop and plan a service-only change. Identify the versioned files the release keeps and build the candidate. Use the Shop's exact-artifact lifecycle test to walk through the keep option, rebinding through restore and reading the saved values back. Then switch to the Root authoring probe for a rejected upgrade and read the old program's response.
Views and visual objects
Use the shared visual language and the Shop AppTree and module cards from lessons 2 and 3. The new object is the ReleaseStrip. It shows a code card labeled shop · build A sitting above the Shop's state cards. Each state card carries its version badge, settings v1, branches v1, inventory v1 and root v1. An upgrade slides a new code card in from the left. The state cards do not move.
In the AppTree, frozen files get a small lock. Files that are free to edit have none.
| View | Framing and anchor |
|---|---|
release |
ReleaseStrip across the middle, client at left. |
tree_groups |
The Shop AppTree with locks, and one EditorPane. |
upgrade_call |
ReleaseStrip with a TestPanel on sdk-lifecycle.mjs at left. |
rejected |
ReleaseStrip with a TestPanel on root-authoring-lifecycle.mjs, using the probe's cards. |
Scene what_changes — Sort the change
Enters with The Shop from lesson 3 as a ReleaseStrip. State cards show branch 10, item 20 · price 125 · stock 5, and tax 750.
Beat populated_shop — A running canister with data
View and focus upgrade_call.
Show tests/pocketic/sdk-lifecycle.mjs lines 55 to 61.
const shop=await install('shop');
assert.equal(await shop.actor.open_branch(10n),true);
assert.equal(await shop.actor.put(10n,20n,125n,7),true);
assert.deepEqual(await shop.actor.checkout(10n,20n,2),{some:250n});
assert.deepEqual(await shop.actor.item(10n,20n),{some:{price:125n,stock:5}});
assert.equal(await shop.actor.checkout(10n,20n,99),null);
assert.equal(await shop.actor.set_tax(750),true);
Narration
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.
Cue and animation On “installed and in use”, run the seven lines quickly, filling the state cards. On “five left”, pulse stock 5. On “seven hundred and fifty”, pulse tax 750. On “release new code”, show a second code card shop · build B waiting at the left edge.
Leaves with A populated Shop and a waiting build.
Beat two_groups — Free to edit, and frozen
View and focus tree_groups.
Show The Shop AppTree. Locks appear on root/memory/v1.rs, settings/memory/v1.rs, branches/memory/v1.rs and inventory/memory/v1.rs. The pane shows src/settings/memory/v1.rs line 9.
#[cdk_rsx_store::memory(owner = crate::settings)]
Narration
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.
Cue and animation On “business code”, tint lib.rs, checkout.rs and each mod.rs. On “The versioned memory files”, place locks on the four v1.rs files. On “exactly as it is”, band the whole pane. On “expects the same contract”, draw an equals sign between the v1 badges on the state cards and the v1 files in the tree.
Hold or transition Hold after the locks appear. Balloon on a lock reads shipped vN.rs files are frozen. To change saved data, add a new version.
Leaves with A tree with four locked files.
Scene release — Build, upgrade, check
Enters with The sorted tree and the waiting build.
Beat build — Use the canonical builder
View and focus release, with a terminal-style pane.
Show The command from the build guide, with the Shop's manifest path from the test registry.
python3 cdk-rsx/tools/build-app.py \
--manifest-path tests/canisters/shop/Cargo.toml \
--name shop --out-dir "$run/artifacts" --offline --locked
Narration
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.
Cue and animation On “the SDK's builder”, band build-app.py. On “the prepared Wasm”, drop a file chip shop.wasm onto the build B card. On “never the raw Cargo output”, show a second chip shop.raw.wasm set aside and dimmed. On “the same compiler setup”, show three matching tags under both code cards, pinned compiler, wasm64, same options.
Leaves with build B ready with its prepared Wasm.
Beat quiet_first — Let work finish
View and focus release.
Show One line from the upgrade guide's opening, paraphrased as an on-screen note. Logical and transport work drains before the upgrade.
Narration
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.
Cue and animation On “still waiting on another canister”, show one dashed call arrow from lesson 5 still out, then returning. On “refuses an upgrade”, bounce the build B card off the strip once while the arrow is out. On “its own readiness check”, show a small empty checkbox on the old code card labeled app check.
Leaves with No outstanding calls.
Beat keep — Upgrade with the keep option
View and focus upgrade_call.
Show tests/pocketic/sdk-lifecycle.mjs lines 37 to 40, the keep helper, with wasm_memory_persistence:[{keep:null}] banded. Then tests/canisters/shop/src/root/mod.rs lines 69 to 75.
async function keep(cell) {
await pic.upgradeCanister({canisterId:cell.id,wasm:gzipSync(cell.artifact.wasm),arg:cell.arg,
upgradeModeOptions:{skip_pre_upgrade:[],wasm_memory_persistence:[{keep:null}]}});
}
let store = unsafe { Store::reopen() }?;
save(App::open(&store)?);
Narration
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.
Cue and animation On “persistence to keep”, band the option. On “the same build on both sides”, focus the test panel and send a copy of build A into the ReleaseStrip. On “binds the services again”, band App::open and redraw the method edges of Branches, Settings and Checkout. On “saved values are still available”, pulse their state cards.
Leaves with The tested Shop serving again after an exact-artifact upgrade.
Beat read_back — Check the values
View and focus upgrade_call.
Show tests/pocketic/sdk-lifecycle.mjs lines 62 to 67.
await keep(shop);
assert.deepEqual(await shop.actor.branches(),[10n]);
assert.equal(await shop.actor.tax(),750);
assert.deepEqual(await shop.actor.item(10n,20n),{some:{price:125n,stock:5}});
assert.equal(await shop.actor.close_branch(10n),true);
assert.deepEqual(await shop.actor.branches(),[]);
Narration
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.
Cue and animation Pass each assertion on its phrase. On “closing the branch succeeds”, remove the Inventory card for branch 10 and pass the last two lines.
Leaves with A passed TestPanel.
Scene refused — When the contract does not match
Enters with The ReleaseStrip, switched to the Root authoring probe's single Scores card with 7 → 91 and 8 → 92.
Beat rejected_upgrade — Refused, and the old program keeps running
View and focus rejected.
Show tests/pocketic/root-authoring-lifecycle.mjs, the loop over bad-policy and undeclared-source and its assertOld helper.
for(const name of ['bad-policy','undeclared-source']) {
await rejected(name,/root catalog rejected[\s\S]*CanisterCalledTrap/,()=>upgrade(id,artifacts[name]));
await assertOld();
}
Narration
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.
Cue and animation On “tries two builds”, bring the two candidate build cards to the strip in turn. On “is refused”, show the rejection on the TestPanel and keep the old build in place. On “still answers”, send get(7) to the old code card and return Some(91).
Hold or transition Hold on the returned Some(91). Balloon reads a refused upgrade leaves the canister as it was.
Leaves with The old build still running with 7 → 91 and 8 → 92.
Beat handoff — Changing the data on purpose
View and focus tree_groups, zoomed on one locked v1.rs.
Show A locked root/memory/v1.rs with a dashed outline for v2.rs beside it.
Narration
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.
Cue and animation On “keeps the contract”, pulse the lock. On “a new version next to the old one”, draw the dashed v2.rs. On “how to convert”, draw a dashed arrow from v1.rs to v2.rs.
Leaves with v1.rs locked and an empty place for v2.rs.
Try it
Verify With PocketIC configured, run the example lifecycle task from the newstack root. Testing
export POCKET_IC_BIN=/absolute/path/to/pocket-ic
python3 tools/test.py run --task 'integration:examples' --cargo-home tmp/dependencies
Expected app-level result. The run prints quickstart map operations and exact-artifact KEEP preserve typed values and shop nested modules preserve stock and tax through KEEP and release a removed child.
Change, adapted exercise Make a business-only release of Quickstart. Add the has(player) query from lesson 1 to a copy of Quickstart, leaving every memory/v1.rs untouched, and build it as a second artifact. In a copy of the lifecycle script, install the original build, call put(7, 99), upgrade to your build with the keep option, and assert get(7) is {some: 99} and has(7) is true. This script and endpoint are not in the checkout. The Nested native lifecycle fixture is the checked-in precedent for a business-only upgrade between two different builds. Then add a comment line to scores/memory/v1.rs in your copy, rebuild, and try the upgrade again. Record whether it is admitted. The upgrade guide says owner source files are captured as raw text, so treat any edit to a shipped version file as a contract change.
Source and review notes
| Item shown | Status |
|---|---|
cdk-rsx/tools/build-app.py, prepared .wasm, generated client files |
Shipped SDK build tool. Building |
wasm_memory_persistence = keep |
Internet Computer upgrade option that the SDK requires. Building, final sections |
restore = reopen, Store::reopen, App::open |
SDK attribute and API, with the app's own open. Written in lesson 2. |
tests/pocketic/*.mjs runners and their helpers |
Repository test tooling. The testing guide says the checkout does not provide a universal canister runner, so an application supplies its own deployment and client code. Application lifecycle qualification |
bad-policy, undeclared-source builds |
Probe-only feature variants that exist to be rejected. |
- The film says “contract” for what the upgrade compares. Do not show digests, catalogs or what the comparison reads. The author-facing facts are that shipped version files are frozen, that the same compiler setup, target and options are required, and that a mismatch is refused before the new code uses the data.
- Besides the
vN.rsfiles, a module can declare extrasources,dependenciesand rootpoliciesas part of its contract. Quickstart and the Shop declare none. Mention this only in notes. Contract admission - Changing the Wasm target from wasm64 to wasm32 is never an upgrade. The build guide says to start a fresh canister.
- The native
drop(app)andApp::opentest from lessons 1 and 2 checks service rebuilding only. The upgrade guide states that native tests do not establish upgrade behavior on the Internet Computer. Keep the two kinds of test visually distinct. - A message type change from lesson 5 is a client-facing change and is separate from the saved-data contract. Clients are rebuilt with the release.
- The lifecycle check upgrades to the identical build. The business-change claim rests on the Nested native lifecycle fixture, which asserts different Wasm hashes for its
business-twoimage and unchanged saved values after the upgrade.