State Migrations
State migrations allow you to transform stored state data when the structure changes between versions. This is critical for maintaining compatibility as the application evolves.
Migration System
Bitwarden uses a versioned migration system located in libs/common/src/state-migrations/.
Migration Structure
Each migration:
- Has a version number
- Transforms state from version N to N+1
- Runs automatically on application startup
MigrationHelper
The MigrationHelper provides methods for state manipulation during migrations:
Migration Best Practices
Always test migrations with real data from previous versions. Data loss during migration can be catastrophic.
1. Preserve Data
Never delete data without preserving it elsewhere first:
2. Handle Missing Data
Always handle cases where expected data doesn’t exist:
3. Test Incrementally
Migrations run sequentially, so test each migration independently:
Example Migration
Here’s a real example migrating vault timeout settings: