Migrations
Migrations allow modules to upgrade their state and data structures between versions without breaking the blockchain. The SDK provides a structured migration system tied to consensus versions.Consensus Versions
Each module has a consensus version that tracks breaking changes:types/module/module.go
Version Tracking
Consensus versions are stored in the module version map:Migrator Pattern
Creating a Migrator
x/auth/keeper/migrations.go
Migration Methods
x/auth/keeper/migrations.go
Registering Migrations
Module Setup
Migration Implementation
Example Migration
Complex Migration
Chain Upgrades
Upgrade Handler
Define upgrade handlers in app.go:Upgrade Proposal
Version Map Handling
Migration Types
Store Migrations
Modify key-value store structure:Param Migrations
Move from legacy params to module params:Protobuf Migrations
Update message definitions:Testing Migrations
Unit Tests
Integration Tests
Best Practices
- Increment consensus version for all breaking changes
- Test migrations thoroughly - data corruption is permanent
- Make migrations idempotent - safe to run multiple times
- Validate state after migration
- Keep old code temporarily for reference
- Document breaking changes clearly
- Provide upgrade documentation for node operators
- Test on testnet first before mainnet upgrade
- Have rollback plan for failed upgrades
- Use semantic versioning for clarity
Migration Checklist
When creating a migration:- Increment
ConsensusVersion()in AppModule - Create migration function (e.g.,
Migrate5to6) - Register migration in
RegisterServices - Write migration logic
- Add unit tests for migration
- Add integration tests
- Document breaking changes
- Update CHANGELOG
- Create upgrade proposal (for mainnet)
- Test on testnet
- Prepare upgrade documentation
Common Migration Patterns
Adding New Field
Changing Field Type
Removing Field
Related Concepts
- Upgrades - Chain upgrade process
- Modules - Module structure and lifecycle
- Governance - Upgrade proposal governance