Migrate the inner layout of a Sui Versioned container
A package upgrade can introduce a V2 type while existing objects still hold V1 data. The versioned container lets a module replace the inner value while preserving the outer object identity.
Transform the old value in one transaction
In the module defining V1 and V2, take a mutable Versioned reference and require its current version to be 1. The removal returns both the old value and a version-change capability. Consume that capability when storing the transformed value at the higher version.
let (old, change) = state.remove_value_for_upgrade<V1>();
let V1 { count } = old;
let next = V2 { count, paused: false };
state.upgrade(2, next, change);
Test preservation and repeat calls
If count was 18 before migration, loading V2 must produce count 18 and paused false. The capability is bound to its container and the new version must exceed its old version. For a batch migration, record which objects reached version 2 and handle repeats without trying to decode V2 as V1.