Add storage migration framework with versioned schema keys - #568
Merged
Kingsman-99 merged 2 commits intoJul 30, 2026
Merged
Conversation
Introduce a schema_version (u32, instance storage) that tracks the contract's on-chain storage shape, plus a SuperAdmin-only migrate() entry point that runs any pending migration_vN steps in order and bumps the version once each transformation completes. Every entry point that funnels through the shared guard helpers (require_admin, require_admin_role, require_not_paused, check_not_paused, require_not_frozen) now panics with "MigrationRequired" while schema_version is behind the version the current Wasm build expects. migrate() is the only entry point exempt, since it is what performs the upgrade; it authenticates via a new schema-guard-free require_admin_role_unguarded helper to avoid a bootstrapping deadlock. pause/unpause/add_allowed_token/ remove_allowed_token inline their own admin check rather than calling the shared helpers, so they get an explicit guard call too. Fresh deployments stamp schema_version at CURRENT_SCHEMA_VERSION during initialize(), since there is no legacy data to migrate. Two concrete migrations demonstrate the mechanics end-to-end: v1->v2 backfills a new per-invoice InvoiceMeta note record for every existing invoice, and v2->v3 renames that record's storage key. Added integration tests simulate a contract rolled back to v1 (as a stand-in for one upgraded from an older Wasm build) and walk it through migrate() to the current schema, checking that guarded entry points are blocked beforehand and that invoice data survives the upgrade.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce a schema_version (u32, instance storage) that tracks the contract's on-chain storage shape, plus a SuperAdmin-only migrate() entry point that runs any pending migration_vN steps in order and bumps the version once each transformation completes.
Every entry point that funnels through the shared guard helpers (require_admin, require_admin_role, require_not_paused, check_not_paused, require_not_frozen) now panics with "MigrationRequired" while schema_version is behind the version the current Wasm build expects. migrate() is the only entry point exempt, since it is what performs the upgrade; it authenticates via a new schema-guard-free require_admin_role_unguarded helper to avoid a bootstrapping deadlock. pause/unpause/add_allowed_token/ remove_allowed_token inline their own admin check rather than calling the shared helpers, so they get an explicit guard call too. Fresh deployments stamp schema_version at CURRENT_SCHEMA_VERSION during initialize(), since there is no legacy data to migrate.
Two concrete migrations demonstrate the mechanics end-to-end: v1->v2 backfills a new per-invoice InvoiceMeta note record for every existing invoice, and v2->v3 renames that record's storage key. Added integration tests simulate a contract rolled back to v1 (as a stand-in for one upgraded from an older Wasm build) and walk it through migrate() to the current schema, checking that guarded entry points are blocked beforehand and that invoice data survives the upgrade.
Closes #350