You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A migration is a mapping process from a class name to a runtime state.
10
+
A migration is a mapping process from a class name to a runtime state. This process communicates the changes to the Workers runtime and provides the runtime with instructions on how to deal with those changes.
11
+
12
+
To apply a migration, you need to:
13
+
14
+
1. Edit your `wrangler.json` file, as explained below.
15
+
2. Re-deploy your Worker using `npx wrangler deploy`.
11
16
12
17
You must initiate a migration process when you:
13
18
@@ -16,27 +21,25 @@ You must initiate a migration process when you:
16
21
- Delete a Durable Object class.
17
22
- Transfer an existing Durable Objects class.
18
23
19
-
This process informs the Workers runtime of the changes and provides it with instructions on how to deal with those changes.
20
-
21
24
:::note
22
25
23
-
Updating code for an existing Durable Object class does not require a migration. To update code for an existing Durable Object class, run [`npx wrangler deploy`](/workers/wrangler/commands/#deploy). This is true even for changes to how code interacts with persistent storage. Because of [global uniqueness](/durable-objects/platform/known-issues/#global-uniqueness), you do not have to be concerned about old and new code interacting with the same storage simultaneously. However, it is your responsibility to ensure that new code is backwards compatible with existing stored data.
26
+
Updating the code for an existing Durable Object class does not require a migration. To update the code for an existing Durable Object class, run [`npx wrangler deploy`](/workers/wrangler/commands/#deploy). This is true even for changes to how the code interacts with persistent storage. Because of [global uniqueness](/durable-objects/platform/known-issues/#global-uniqueness), you do not have to be concerned about old and new code interacting with the same storage simultaneously. However, it is your responsibility to ensure that the new code is backwards compatible with existing stored data.
24
27
25
28
:::
26
29
27
30
## Create migration
28
31
29
32
The most common migration performed is a new class migration, which informs the runtime that a new Durable Object class is being uploaded. This is also the migration you need when creating your first Durable Object class.
30
33
31
-
import { WranglerConfig } from"~/components";
34
+
To apply a create migration, edit your `wrangler.json` file in the following way:
32
35
33
36
<WranglerConfig>
34
37
35
38
```toml
36
39
[[migrations]]
37
-
tag = "<v1>"#Should be unique for each entry
40
+
tag = "<v1>"#Migration identifier. This should be unique for each migration entry
38
41
new_classes = ["<NewDurableObjectClass>"] # Array of new classes
39
-
# For SQLite storage backend use new_sqlite_classes=["<NewDurableObjectClass>"]
42
+
# For SQLite storage backend use new_sqlite_classes=["<NewDurableObjectClass>"] instead
40
43
```
41
44
42
45
</WranglerConfig>
@@ -47,13 +50,15 @@ Running a delete migration will delete all Durable Objects associated with the d
47
50
48
51
- Do not run a delete migration on a class without first ensuring that you are not relying on the Durable Objects within that Worker anymore, that is, first remove the binding from the Worker.
49
52
- Copy any important data to some other location before deleting.
50
-
- No need to run a delete migration on a class that was renamed or transferred.
53
+
- You do not have to run a delete migration on a class that was renamed or transferred.
54
+
55
+
To apply a delete migration, edit your `wrangler.json` file in the following way:
51
56
52
57
<WranglerConfig>
53
58
54
59
```toml
55
60
[[migrations]]
56
-
tag = "<v2>"
61
+
tag = "<v2>"# Migration identifier. This should be unique for each migration entry
57
62
deleted_classes = ["<ClassToDelete>"] # Array of deleted class names
58
63
```
59
64
@@ -63,6 +68,8 @@ deleted_classes = ["<ClassToDelete>"] # Array of deleted class names
63
68
64
69
Rename migrations are used to transfer stored Durable Objects between two Durable Object classes in the same Worker code file.
65
70
71
+
To apply a rename migration, edit your `wrangler.json` file in the following way:
72
+
66
73
<WranglerConfig>
67
74
68
75
```toml
@@ -71,15 +78,15 @@ name = "<MY_DURABLE_OBJECT>"
71
78
class_name = "<UpdatedDurableObject>"# Update the class name to the new class name
72
79
73
80
[[migrations]]
74
-
tag = "<v2>"
81
+
tag = "<v2>"# Migration identifier. This should be unique for each migration entry
75
82
renamed_classes = [{from = "<OldDurableObject>", to = "<UpdatedDurableObject>" }] # Array of rename directives
76
83
```
77
84
78
85
</WranglerConfig>
79
86
80
87
## Transfer migration
81
88
82
-
Migrations can also be used for transferring stored data between two Durable Object classes. Transfer migrations are used to transfer stored Durable Objects between two Durable Object classes in different Worker code files.
89
+
Transfer migrations are used to transfer stored Durable Objects between two Durable Object classes in different Worker code files.
83
90
84
91
If you want to transfer stored Durable Objects between two Durable Object classes in the same Worker code file, use [rename migrations](#rename-migration) instead.
85
92
@@ -89,6 +96,8 @@ Do not run a [Create migration](#create-migration) for the destination class bef
89
96
90
97
:::
91
98
99
+
To apply a transfer migration, edit your `wrangler.json` file in the following way:
100
+
92
101
<WranglerConfig>
93
102
94
103
```toml
@@ -97,7 +106,7 @@ name = "<MY_DURABLE_OBJECT>"
97
106
class_name = "<DestinationDurableObjectClass>"
98
107
99
108
[[migrations]]
100
-
tag = "<v1>"
109
+
tag = "<v1>"# Migration identifier. This should be unique for each migration entry
@@ -133,20 +142,24 @@ Note that `.toml` files do not allow line breaks in inline tables (the `{key = "
133
142
134
143
:::
135
144
145
+
## Examples of Durable Object migration
146
+
136
147
To illustrate an example migrations workflow, the `DurableObjectExample` class can be initially defined with:
137
148
138
149
<WranglerConfig>
139
150
140
151
```toml
141
152
# Creating a new Durable Object class
142
153
[[migrations]]
143
-
tag = "v1"#Should be unique for each entry
154
+
tag = "v1"#Migration identifier. This should be unique for each migration entry
144
155
new_classes = ["DurableObjectExample"] # Array of new classes
145
156
```
146
157
147
158
</WranglerConfig>
148
159
149
-
You can rename the `DurableObjectExample` class to `UpdatedName` and delete an outdated `DeprecatedClass` entirely. You can create seprate migrations for each operation, or combine them into a single migration as shown below.
160
+
You can rename the `DurableObjectExample` class to `UpdatedName` and delete an outdated `DeprecatedClass` entirely. You can create separate migrations for each operation, or combine them into a single migration as shown below.
161
+
162
+
### Create migration example
150
163
151
164
To create new classes, add the following to your `wrangler` configuration file and deploy the Worker:
152
165
@@ -170,6 +183,8 @@ new_classes = ["DeprecatedClass","DurableObjectExample"] # Array of new classes
170
183
171
184
</WranglerConfig>
172
185
186
+
### Delete migration example
187
+
173
188
To run the Delete migration, first remove the binding for the `DeprecatedClass` and deploy the Worker.
174
189
175
190
<WranglerConfig>
@@ -190,6 +205,8 @@ new_classes = ["DeprecatedClass","DurableObjectExample"] # Array of new classes
190
205
191
206
If you just want to run the Delete migration, add the Delete migration configuration and deploy the Worker.
192
207
208
+
### Rename migration example
209
+
193
210
To run Rename migration, update the binding for the `DurableObjectExample` to the new class name `UpdatedName`. Add the Rename migration configuration and deploy the Worker.
194
211
195
212
To apply both migrations in the same deploy, add the migrations configuration and deploy the Worker.
@@ -216,6 +233,8 @@ deleted_classes = ["DeprecatedClass"] # Array of deleted class names
216
233
217
234
</WranglerConfig>
218
235
236
+
### Transfer migration example
237
+
219
238
You can transfer stored Durable Objects from `DurableObjectExample` to `TransferredClass` from a Worker script named `OldWorkerScript`. The configuration of the `wrangler` file for your new Worker code (destination Worker code) would look like this:
220
239
221
240
<WranglerConfig>
@@ -254,4 +273,6 @@ new_sqlite_classes = ["MyDurableObject"] # Array of new classes
254
273
255
274
</WranglerConfig>
256
275
276
+
For an example of a new class migration, refer to [Get started: Configure Durable Object class with SQLite storage backend](/durable-objects/get-started/tutorial-with-sql-api/#6-configure-durable-object-class-with-sqlite-storage-backend).
277
+
257
278
You cannot enable a SQLite storage backend on an existing, deployed Durable Object class, so setting `new_sqlite_classes` on later migrations will fail with an error. Automatic migration of deployed classes from their key-value storage backend to SQLite storage backend will be available in the future.
0 commit comments