migrating-motoko

Migrate Motoko canister actor state across upgrades using inline migration expressions.

Updated Apr 3, 2026
One-click install
npx skills add https://github.com/phukrit7171/Relationship-Smart-Contract-ICP --skill migrating-motoko-phukrit7171
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: migrating-motoko
Source: https://github.com/phukrit7171/Relationship-Smart-Contract-ICP/tree/main/.agents/skills/migrating-motoko
Command: npx skills add https://github.com/phukrit7171/Relationship-Smart-Contract-ICP --skill migrating-motoko-phukrit7171

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Upgrading a Motoko canister often requires renaming fields, changing field types, or restructuring actor state, and doing this incorrectly can abort the upgrade or lose on-chain data. This Skill guides you through writing a single inline migration function attached to the actor so state transforms safely during upgrades. ## Core Features & Use Cases - Inline migration syntax: Attach a migration function with (with migration = Migration.run) directly before the actor declaration. - Migration module pattern: Define old actor types inline in a separate migration.mo module and map them to new types. - Common transformations: Add fields with defaults, convert Bool to variant, rename fields, drop fields, and transform collection values. - Use Case: You need to change a completed : Bool field into a status variant across all tasks in stable storage. The Skill shows how to write a migration function that maps old records to new ones and verifies the result with mops check and mops build. ## Quick Start Ask the AI to write a Motoko inline migration that renames an actor field and converts a Bool flag into a variant type using the (with migration = ...) syntax.

Frequently Asked Questions about migrating-motoko

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate Motoko canister state during an upgrade?

Attach a migration function to the actor using the (with migration = Migration.run) syntax. The function takes the old actor fields as a record and returns the new actor fields, running only during the upgrade.

When does Motoko require an explicit migration function?

Explicit migration is required when renaming fields, changing a field's type, restructuring state, or transforming collection values. Adding or removing fields, changing mutability, and widening types are handled implicitly by the runtime.

What happens if a Motoko migration function traps during upgrade?

If the migration function traps, the upgrade is aborted and the canister stays on the old version. No state is lost because the new code never takes effect.

Can I use preupgrade and postupgrade hooks for Motoko data migration?

No, the Skill explicitly advises against using preupgrade or postupgrade for data migration. Use the inline migration expression instead, which the compiler type-checks against both old and new actor state.

What is the difference between inline migration and enhanced migration in Motoko?

Inline migration supports a single migration function per upgrade attached to the actor. For multi-step migration chains with a migrations/ directory and the --enhanced-migration flag, use the migrating-motoko-enhanced approach instead.