skills/migrating-motoko-enhanced/SKILL.md
Enhanced multi-step migration for Motoko actors using a migrations/ directory and --enhanced-migration flag. Use when upgrading canister state across multiple deployments, writing migration files, changing actor field types, or managing a migration chain. For a single one-shot migration, use migrating-motoko instead.
npx skillsauth add dfinity/icskills migrating-motoko-enhancedInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Manage canister state evolution through a chain of migration modules. Each migration captures one logical change (add, rename, drop, transform a field) and the compiler verifies the entire chain is consistent.
[canisters.<name>.migrations] configured in mops.tomlstable keyword, preupgrade/postupgrade, or inline (with migration = ...)<system> calls like timers)public func migration({...}) : {...}backend/
├── main.mo
├── types.mo
├── lib/
├── mixins/
└── migrations/
├── 20250101_000000_Init.mo
├── 20250315_120000_AddProfile.mo
└── 20250601_090000_RenameField.mo
With enhanced migration, actor variables have no initializer:
actor {
var name : Text; // value comes from migration chain
var balance : Nat; // likewise
let frozen : Bool; // let bindings can also be uninitialized
public func greet() : async Text {
"Hello, " # name # "! Balance: " # debug_show balance;
};
};
Each migration module takes a record of input fields and returns a record of output fields:
// migrations/20250101_000000_Init.mo
module {
public func migration(_ : {}) : { name : Text; balance : Nat } {
{ name = ""; balance = 0 }
}
}
| Field appears in | Effect | | ---------------- | ------ | | Input and output | Field is transformed (old value read, new value produced) | | Output only | New field added to state | | Input only | Field consumed and removed from state | | Neither | Field carried through unchanged |
Given state {a : Nat; b : Text; c : Bool} and migration:
module {
public func migration(old : { a : Nat; b : Text }) : { a : Int; d : Float } {
{ a = old.a; d = 1.0 }
}
}
a: transformed Nat → Intb: consumed (removed)c: carried through unchangedd: newly introduced{a : Int; c : Bool; d : Float}// migrations/20250101_000000_Init.mo
module {
public func migration(_ : {}) : { count : Nat; header : Text } {
{ count = 0; header = "default" }
}
}
// migrations/20250201_000000_AddEmail.mo
module {
public func migration(_ : {}) : { email : Text } {
{ email = "" }
}
}
module {
public func migration(_ : {}) : { assignee : ?Principal } {
{ assignee = null }
}
}
// migrations/20250301_000000_CountToInt.mo
module {
public func migration(old : { count : Nat }) : { count : Int } {
{ count = old.count }
}
}
// migrations/20250401_000000_RenameHeader.mo
module {
public func migration(old : { header : Text }) : { title : Text } {
{ title = old.header }
}
}
// migrations/20250501_000000_DropEmail.mo
module {
public func migration(_ : { email : Text }) : {} {
{}
}
}
// migrations/20250601_000000_SplitName.mo
import Text "mo:core/Text";
module {
public func migration(old : { name : Text }) : { firstName : Text; lastName : Text } {
let parts = old.name.split(#char ' ');
let first = switch (parts.next()) { case (?f) f; case (null) "" };
let last = switch (parts.next()) { case (?l) l; case (null) "" };
{ firstName = first; lastName = last }
}
}
module {
public func migration(old : { var completed : Bool }) : { var status : { #pending; #completed } } {
{ var status = if (old.completed) { #completed } else { #pending } }
}
}
import Map "mo:core/Map";
module {
type OldTask = { id : Nat; title : Text; var completed : Bool };
type NewTask = { id : Nat; title : Text; var status : { #pending; #completed } };
public func migration(old : { var tasks : Map.Map<Nat, OldTask> })
: { var tasks : Map.Map<Nat, NewTask> } {
let tasks = old.tasks.map<Nat, OldTask, NewTask>(
func(_, task) {
{
id = task.id;
title = task.title;
var status = if (task.completed) { #completed } else { #pending };
}
}
);
{ var tasks }
}
}
import Map "mo:core/Map";
module {
type OldUser = { name : Text; email : Text };
type NewUser = { name : Text; email : Text; bio : Text };
public func migration(old : { users : Map.Map<Nat, OldUser> })
: { users : Map.Map<Nat, NewUser> } {
let users = old.users.map<Nat, OldUser, NewUser>(
func(_, u) { { u with bio = "" } }
);
{ users }
}
}
Migrations form a chain. The compiler verifies each migration's input is compatible with the state produced by all preceding migrations.
| Migration | Input | Output | Effect |
| ------------- | ---------------- | -------------------------------- | ------------------------- |
| Init | {} | {name : Text; balance : Nat} | Initializes both fields |
| AddProfile | {} | {profile : Text} | Adds a new field |
| RenameField | {name : Text} | {displayName : Text} | Renames name → displayName|
After the full chain: {displayName : Text; balance : Nat; profile : Text}. The actor must declare fields compatible with this final state.
Shows how patterns combine across four deployments.
// migrations/20250101_000000_Init.mo
module {
public func migration(_ : {}) : { var nextId : Nat } {
{ var nextId = 0 }
}
}
// migrations/20250201_000000_AddTasks.mo
import Map "mo:core/Map";
module {
type Task = { id : Nat; text : Text; completed : Bool };
public func migration(_ : {}) : { tasks : Map.Map<Nat, Task> } {
{ tasks = Map.empty<Nat, Task>() }
}
}
// migrations/20250301_000000_TaskStatus.mo — transform Bool → variant
import Map "mo:core/Map";
module {
type OldTask = { id : Nat; text : Text; completed : Bool };
type NewTask = { id : Nat; text : Text; status : { #pending; #inProgress; #completed } };
public func migration(old : { tasks : Map.Map<Nat, OldTask> })
: { tasks : Map.Map<Nat, NewTask> } {
let tasks = old.tasks.map<Nat, OldTask, NewTask>(
func(_, task) {
{ id = task.id; text = task.text;
status = if (task.completed) #completed else #pending }
}
);
{ tasks }
}
}
// migrations/20250401_000000_AddDueDate.mo — add field to each record
import Map "mo:core/Map";
module {
type Status = { #pending; #inProgress; #completed };
type OldTask = { id : Nat; text : Text; status : Status };
type NewTask = { id : Nat; text : Text; status : Status; due : Int };
public func migration(old : { tasks : Map.Map<Nat, OldTask> })
: { tasks : Map.Map<Nat, NewTask> } {
let tasks = old.tasks.map<Nat, OldTask, NewTask>(
func(_, task) { { task with due = 0 } }
);
{ tasks }
}
}
Final state: { var nextId : Nat; tasks : Map.Map<Nat, { id : Nat; text : Text; status : { #pending; #inProgress; #completed }; due : Int }> }
[moc]
args = ["--default-persistent-actors"]
[canisters.backend]
main = "src/backend/main.mo"
[canisters.backend.migrations]
chain = "src/backend/migrations"
When [canisters.<name>.migrations] is configured, mops auto-injects --enhanced-migration into check/build/check-stable. Do not add --enhanced-migration to [canisters.<name>].args — mops will error.
--enhanced-orthogonal-persistence is on by default.
Then mops check --fix and mops build work as usual. Add new migration files directly under migrations/ with timestamp prefixes.
--enhanced-migration with inline (with migration = ...)<system> calls)migrations/ directory exists next to actor sourceInit.mo with empty input)public func migration({...}) : {...}[canisters.<name>.migrations] configured in mops.toml (mops injects --enhanced-migration)mops check --fix to verify chain consistencymops build to compilemotoko for general Motoko language reference and mo:core APIsmigrating-motoko for inline migration without --enhanced-migrationmops-cli for mops check, mops build, and toolchain setupdevelopment
One-time installer that makes a Claude Code project keep its Internet Computer skills up to date automatically. Sets up a SessionStart hook plus a sync script so .claude/skills/ always mirrors the latest skills published at skills.internetcomputer.org. Use when a user wants to install, bootstrap, or enable "always-latest" Internet Computer / IC / ICP / Motoko skills in a project, or pastes the link to this skill. This is a one-time setup action, not ongoing IC knowledge — after it runs, the installed hook keeps skills current on every session. Do NOT use for IC coding questions themselves — this only configures auto-updating skills.
development
Integrate Internet Identity authentication. Covers passkey and OpenID sign-in flows, delegation handling, and principal-per-app isolation. Use when adding sign-in, login, auth, passkeys, or Internet Identity to a frontend or canister. Do NOT use for wallet integration or ICRC signer flows — use wallet-integration instead.
tools
Guides use of the icp command-line tool for building and deploying Internet Computer applications. Covers project configuration (icp.yaml), recipes, environments, canister lifecycle, and identity management. Use when building, deploying, or managing any IC project. Use when the user mentions icp, dfx, canister deployment, local network, or project setup. Do NOT use for canister-level programming patterns like access control, inter-canister calls, or stable memory — use domain-specific skills instead.
development
Deploy frontend assets to the IC. Covers certified assets, SPA routing with .ic-assets.json5, content encoding, and programmatic uploads. Use when hosting a frontend, deploying static files, or setting up SPA routing on IC. Do NOT use for canister-level code patterns or custom domain setup — use custom-domains instead.