Overview
Durable Objects migrations are managed through your Worker’s configuration file. Thewrangler deploy command automatically handles migrations based on the migrations array in your configuration.
Configuration
Define Durable Objects and migrations in your Worker configuration:wrangler.json
Migration Types
Migrations allow you to make changes to your Durable Objects over time. Each migration must have a uniquetag and can include one or more of the following operations:
new_classes
Declare a new Durable Object class.wrangler.json
renamed_classes
Rename an existing Durable Object class.wrangler.json
deleted_classes
Delete an existing Durable Object class.wrangler.json
new_sqlite_classes
Declare a new Durable Object class with SQLite storage.wrangler.json
Migration Rules
- Sequential Tags: Migrations are applied in order based on their position in the array. Each migration must have a unique tag.
- Cumulative: Once a migration is applied to a deployed Worker, it cannot be removed or modified. New migrations must be added to the end of the array.
-
Automatic Application: When you run
wrangler deploy, Wrangler automatically determines which migrations need to be applied based on the current migration tag of the deployed Worker. -
Tag Format: Tags can be any string, but it’s recommended to use a versioning scheme like
v1,v2, etc.
Example: Complete Migration Flow
Here’s an example showing the evolution of a Durable Object through multiple migrations:wrangler.json
Deployment
Migrations are automatically applied when you deploy your Worker:- Check the current migration tag of the deployed Worker
- Identify which migrations need to be applied
- Apply migrations in sequence
- Update the Worker’s migration tag
Best Practices
-
Test migrations locally: Use
wrangler devto test your Durable Objects before deploying migrations to production. - Plan migrations carefully: Since migrations cannot be undone, plan your class structure changes in advance.
-
Use descriptive tags: Use clear, sequential tags like
v1,v2,v3or date-based tags like2024-01-15. - Never remove migrations: Once a migration is deployed, it must remain in the configuration file. Only add new migrations to the end.
- Coordinate renames: When renaming a class, ensure your Worker code is updated to use the new class name before or at the same time as the migration.
Troubleshooting
Migration tag mismatch
If you see an error about a migration tag mismatch, it means:- The deployed Worker has a migration tag that doesn’t exist in your local configuration
- You may have removed or modified a previous migration
Class not found
If you see an error about a class not being found:- Ensure the class is exported from your Worker script
- Verify the class name in the migration matches the actual class name
- Check that the class extends
DurableObject
Migration order issues
Migrations must be applied in order. If you need to make multiple changes:- Add each change as a separate migration with a new tag
- Deploy after each migration to ensure proper sequencing