migrating-motoko

Migrate Motoko actor state across canister upgrades with migration functions.

588|127|Updated May 11, 2018
One-click install
npx skills add https://github.com/caffeinelabs/motoko --skill migrating-motoko
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: migrating-motoko
Source: https://github.com/caffeinelabs/motoko/tree/main/.agents/skills/migrating-motoko
Command: npx skills add https://github.com/caffeinelabs/motoko --skill migrating-motoko

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves the problem of upgrading an Internet Computer canister written in Motoko without losing or corrupting actor state when the actor’s fields or types change.

Core Features & Use Cases

  • Inline actor migration: Attach a (with migration = ...) expression to an actor to define how old state transforms into new state during upgrades.
  • Implicit compatibility migration: Rely on runtime compatibility for safe changes like adding/removing fields, changing mutability, and widening types.
  • Explicit migration rules: Use a migration function with persistable, non-generic record input/output types, with clear guidance for renames, type changes, restructuring, and collection/value transformations.

Quick Start

Use (with migration = Migration.run) immediately before your actor definition, and implement run(old : { ... }) : { ... } to convert your previous record shape into the new record shape for the fields that changed.

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 actor state safely when changing fields or types during a canister upgrade?

To migrate Motoko actor state safely during a canister upgrade, use a `(with migration = ...)` expression with a function that transforms old records into new persistable record shapes.

When do I need an explicit migration function for an Internet Computer canister upgrade?

You need an explicit migration function for canister upgrades when performing schema evolution like field renames, type conversions, or restructuring stored collection values beyond implicit runtime compatibility.

How does the migration function handle upgrade failures and traps in Motoko?

The Motoko migration function runs only on upgrade and automatically aborts the canister upgrade process if the state transformation encounters a trap, preventing state corruption.

Can I use multiple migration rules for complex schema evolution in Motoko?

Yes, you can handle complex schema evolution in Motoko by using a multi-migration `migrations/` directory structure instead of a single inline `(with migration = ...)` expression.

What type restrictions apply to the input and output of a Motoko state migration function?

The Motoko state migration function requires non-generic, persistable record types for both input and output to ensure actor state serialization works correctly during the upgrade process.

Does Motoko support implicit compatibility migration for minor actor state changes?

Yes, Motoko supports implicit compatibility migration for safe changes like adding or removing fields, changing mutability, and widening types without requiring an explicit migration function.