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.
12
11
13
-
You must initiate a migration process when you:
12
+
To apply a migration, you need to:
14
13
15
-
* Create a new <GlossaryTooltipterm="Durable Object class">Durable Object class</GlossaryTooltip>.
16
-
* Rename a Durable Object class.
17
-
* Delete a Durable Object class.
18
-
* Transfer an existing Durable Objects class.
14
+
1. Edit your `wrangler.toml / wrangler.json` file, as explained below.
15
+
2. Re-deploy your Worker using `npx wrangler deploy`.
19
16
20
-
This process informs the Workers runtime of the changes and provides it with instructions on how to deal with those changes.
17
+
You must initiate a migration process when you:
18
+
19
+
- Create a new <GlossaryTooltipterm="Durable Object class">Durable Object class</GlossaryTooltip>.
20
+
- Rename a Durable Object class.
21
+
- Delete a Durable Object class.
22
+
- Transfer an existing Durable Objects class.
21
23
22
24
:::note
23
25
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
-
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.
28
+
:::
26
29
30
+
## Create migration
27
31
28
-
:::
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.
29
33
30
-
The most common migration performed is a new class migration, which informs the runtime that a new Durable Object class is being uploaded.
34
+
To apply a Create migration:
31
35
32
-
Migrations can also be used for transferring stored data between two Durable Object classes:
36
+
<Steps>
37
+
1. Add the following lines to your `wrangler.toml / wrangler.json` file:
33
38
34
-
* Rename migrations are used to transfer stored Durable Objects between two Durable Object classes in the same Worker code file.
35
-
* Transfer migrations are used to transfer stored Durable Objects between two Durable Object classes in different Worker code files.
39
+
<WranglerConfig>
40
+
```toml
41
+
[[migrations]]
42
+
tag = "<v1>"# Migration identifier. This should be unique for each migration entry
43
+
new_classes = ["<NewDurableObjectClass>"] # Array of new classes
44
+
# For SQLite storage backend use new_sqlite_classes=["<NewDurableObjectClass>"] instead
45
+
```
46
+
</WranglerConfig>
47
+
The Create migration contains:
36
48
37
-
The destination class (the class that stored Durable Objects are being transferred to) for a rename or transfer migration must be exported by the deployed Worker.
49
+
- A `tag` to identify the migration.
50
+
- The array `new_classes`, which contains the new Durable Object class.
38
51
39
-
:::caution[Important]
52
+
2. Ensure you reference the correct name of the Durable Object class in your Worker code.
53
+
3. Deploy the Worker.
54
+
</Steps>
40
55
56
+
<Detailsheader="Create migration example">
41
57
42
-
After a rename or transfer migration, requests to the destination Durable Object class will have access to the source Durable Object's stored data.
58
+
To create a new Durable Object binding `DURABLE_OBJECT_A`, your `wrangler.toml / wrangler.json` file should look like the following:
43
59
44
-
After a migration, any existing bindings to the original Durable Object class (for example, from other Workers) will automatically forward to the updated destination class. However, any Workers bound to the updated Durable Object class must update their Durable Object binding configuration in the Wrangler file for their next deployment.
60
+
<WranglerConfig>
61
+
```toml
62
+
# Creating a new Durable Object class
63
+
[[durable_objects.bindings]]
64
+
name = "DURABLE_OBJECT_A"
65
+
class_name = "DurableObjectAClass"
45
66
67
+
# Add the lines below for a Create migration.
68
+
[[migrations]]
69
+
tag = "v1"
70
+
new_classes = ["DurableObjectAClass"]
71
+
```
72
+
</WranglerConfig>
46
73
47
-
:::
74
+
</Details>
48
75
49
-
Migrations can also be used to delete a Durable Object class and its stored Durable Objects.
76
+
## Delete migration
50
77
51
-
:::caution[Delete migrations]
78
+
Running a Delete migration will delete all Durable Objects associated with the deleted class, including all of their stored data.
52
79
80
+
- 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.
81
+
- Copy any important data to some other location before deleting.
82
+
- You do not have to run a Delete migration on a class that was renamed or transferred.
53
83
54
-
Running a delete migration will delete all Durable Objects associated with the deleted class, including all of their stored data. Do not run a delete migration on a class without first ensuring that you are not relying on the Durable Objects within that class anymore. Copy any important data to some other location before deleting.
84
+
To apply a Delete migration:
55
85
86
+
<Steps>
87
+
1. Remove the binding for the class you wish to delete from the `wrangler.toml / wrangler.json` file.
88
+
2. Remove references for the class you wish to delete from your Worker code.
89
+
3. Add the following lines to your `wrangler.toml / wrangler.json` file.
56
90
57
-
:::
91
+
<WranglerConfig>
92
+
```toml
93
+
[[migrations]]
94
+
tag = "<v2>"# Migration identifier. This should be unique for each migration entry
95
+
deleted_classes = ["<ClassToDelete>"] # Array of deleted class names
96
+
```
97
+
</WranglerConfig>
98
+
The Delete migration contains:
99
+
100
+
- A `tag` to identify the migration.
101
+
- The array `deleted_classes`, which contains the deleted Durable Object classes.
102
+
4. Deploy the Worker.
103
+
</Steps>
104
+
105
+
<Detailsheader="Delete migration example">
106
+
To delete a Durable Object binding `DEPRECATED_OBJECT`, your `wrangler.toml / wrangler.json` file should look like the following:
107
+
108
+
<WranglerConfig>
109
+
```toml
110
+
# Remove the binding for the DeprecatedObjectClass DO
111
+
#[[durable_objects.bindings]]
112
+
#name = "DEPRECATED_OBJECT"
113
+
#class_name = "DeprecatedObjectClass"
58
114
59
-
### Durable Object migrations in the Wrangler configuration file
115
+
[[migrations]]
116
+
tag = "v3"# Should be unique for each entry
117
+
deleted_classes = ["DeprecatedObjectClass"] # Array of new classes
118
+
```
119
+
</WranglerConfig>
120
+
</Details>
60
121
61
-
Migrations are performed through the `[[migrations]]` configurations key in your Wrangler file.
122
+
## Rename migration
62
123
63
-
Migrations require a migration tag, which is defined by the `tag` property in each migration entry.
124
+
Rename migrations are used to transfer stored Durable Objects between two Durable Object classes in the same Worker code file.
64
125
65
-
Migration tags are treated like unique names and are used to determine which migrations have already been applied. Once a given Worker code has a migration tag set on it, all future Worker code deployments must include a migration tag.
126
+
To apply a Rename migration:
66
127
67
-
The migration list is an ordered array of tables, specified as a top-level key in your Wrangler file. The migration list is inherited by all environments and cannot be overridden by a specific environment.
128
+
<Steps>
129
+
1. Update the previous class name to the new class name by editing your `wrangler.toml / wrangler.json` file in the following way:
68
130
69
-
All migrations are applied at deployment. Each migration can only be applied once per [environment](/durable-objects/reference/environments/).
131
+
<WranglerConfig>
132
+
```toml
133
+
[[durable_objects.bindings]]
134
+
name = "<MY_DURABLE_OBJECT>"
135
+
class_name = "<UpdatedDurableObject>"# Update the class name to the new class name
70
136
71
-
To illustrate an example migrations workflow, the `DurableObjectExample` class can be initially defined with:
137
+
[[migrations]]
138
+
tag = "<v3>"# Migration identifier. This should be unique for each migration entry
139
+
renamed_classes = [{from = "<OldDurableObject>", to = "<UpdatedDurableObject>" }] # Array of rename directives
140
+
```
141
+
</WranglerConfig>
72
142
73
-
<WranglerConfig>
143
+
The Rename migration contains:
144
+
- A `tag` to identify the migration.
145
+
- The `renamed_classes` array, which contains objects with `from` and `to` properties.
146
+
- `from` property is the old Durable Object class name.
147
+
- `to` property is the renamed Durable Object class name.
148
+
2. Reference the new Durable Object class name in your Worker code.
149
+
3. Deploy the Worker.
150
+
</Steps>
74
151
152
+
<Detailsheader="Rename migration example">
153
+
154
+
To rename a Durable Object class, from `OldName` to `UpdatedName`, your `wrangler.toml / wrangler.json` file should look like the following:
155
+
156
+
<WranglerConfig>
75
157
```toml
158
+
# Before deleting the `DeprecatedClass` remove the binding for the `DeprecatedClass`.
159
+
# Update the binding for the `DurableObjectExample` to the new class name `UpdatedName`.
160
+
[[durable_objects.bindings]]
161
+
name = "MY_DURABLE_OBJECT"
162
+
class_name = "UpdatedName"
163
+
164
+
# Renaming classes
76
165
[[migrations]]
77
-
tag = "v1"# Should be unique for each entry
78
-
new_classes = ["DurableObjectExample"] # Array of new classes
166
+
tag = "v3"
167
+
renamed_classes = [{from = "OldName", to = "UpdatedName" }] # Array of rename directives
79
168
```
80
-
81
169
</WranglerConfig>
82
170
83
-
Each migration in the list can have multiple directives, and multiple migrations can be specified as your project grows in complexity. For example, you may want to rename the `DurableObjectExample` class to `UpdatedName` and delete an outdated `DeprecatedClass` entirely.
171
+
</Details>
84
172
173
+
## Transfer migration
85
174
175
+
Transfer migrations are used to transfer stored Durable Objects between two Durable Object classes in different Worker code files.
86
176
87
-
<WranglerConfig>
177
+
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.
178
+
179
+
:::note
180
+
Do not run a [Create migration](#create-migration) for the destination class before running a Transfer migration. The Transfer migration will create the destination class for you.
181
+
:::
182
+
183
+
To apply a Transfer migration:
184
+
185
+
<Steps>
186
+
1. Edit your `wrangler.toml / wrangler.json` file in the following way:
187
+
188
+
<WranglerConfig>
189
+
```toml
190
+
[[durable_objects.bindings]]
191
+
name = "<MY_DURABLE_OBJECT>"
192
+
class_name = "<DestinationDurableObjectClass>"
88
193
194
+
[[migrations]]
195
+
tag = "<v4>"# Migration identifier. This should be unique for each migration entry
- The `transferred_class` array, which contains objects with `from`, `from_script`, and `to` properties.
202
+
- `from` property is the name of the source Durable Object class.
203
+
- `from_script` property is the name of the source Worker script.
204
+
- `to` property is the name of the destination Durable Object class.
205
+
2. Ensure you reference the name of the new, destination Durable Object class in your Worker code.
206
+
3. Deploy the Worker.
207
+
</Steps>
208
+
209
+
<Detailsheader="Transfer migration example">
210
+
211
+
You can transfer stored Durable Objects from `DurableObjectExample` to `TransferredClass` from a Worker script named `OldWorkerScript`. The configuration of the `wrangler.toml / wrangler.json` file for your new Worker code (destination Worker code) would look like this:
212
+
213
+
<WranglerConfig>
89
214
```toml
90
-
[[migrations]]
91
-
tag = "v1"# Should be unique for each entry
92
-
new_classes = ["DurableObjectExample"] # Array of new classes
215
+
# destination worker
216
+
[[durable_objects.bindings]]
217
+
name = "MY_DURABLE_OBJECT"
218
+
class_name = "TransferredClass"
93
219
220
+
# Transferring class
94
221
[[migrations]]
95
-
tag = "v2"
96
-
renamed_classes = [{from = "DurableObjectExample", to = "UpdatedName" }] # Array of rename directives
97
-
deleted_classes = ["DeprecatedClass"] # Array of deleted class names
- Migrations are performed through the `[[migrations]]` configurations key in your `wrangler.toml` file or `migration` key in your `wrangler.json` file.
104
231
105
-
Note that `.toml` files do not allow line breaks in inline tables (the `{key = "value"}` syntax), but line breaks in the surrounding inline array are acceptable.
232
+
- Migrations require a migration tag, which is defined by the `tag` property in each migration entry.
106
233
234
+
- Migration tags are treated like unique names and are used to determine which migrations have already been applied. Once a given Worker code has a migration tag set on it, all future Worker code deployments must include a migration tag.
107
235
236
+
- The migration list is an ordered array of tables, specified as a top-level key in your `wrangler` configuration file. The migration list is inherited by all environments and cannot be overridden by a specific environment.
237
+
238
+
- All migrations are applied at deployment. Each migration can only be applied once per [environment](/durable-objects/reference/environments/).
239
+
240
+
- Each migration in the list can have multiple directives, and multiple migrations can be specified as your project grows in complexity.
241
+
242
+
:::caution[Important]
243
+
- The destination class (the class that stored Durable Objects are being transferred to) for a Rename or Transfer migration must be exported by the deployed Worker.
244
+
245
+
- You should not create the destination Durable Object class before running a Rename or Transfer migration. The migration will create the destination class for you.
246
+
247
+
- After a Rename or Transfer migration, requests to the destination Durable Object class will have access to the source Durable Object's stored data.
248
+
249
+
- After a migration, any existing bindings to the original Durable Object class (for example, from other Workers) will automatically forward to the updated destination class. However, any Workers bound to the updated Durable Object class must update their Durable Object binding configuration in the `wrangler` configuration file for their next deployment.
108
250
:::
109
251
110
-
### Enable SQLite storage backend on new Durable Object class migration
252
+
:::note
253
+
Note that `.toml` files do not allow line breaks in inline tables (the `{key = "value"}` syntax), but line breaks in the surrounding inline array are acceptable.
254
+
:::
255
+
256
+
{/* ## Examples of Durable Object migration
257
+
258
+
To illustrate an example migrations workflow, the `DurableObjectExample` class can be initially defined with:
259
+
260
+
<WranglerConfig>
261
+
262
+
```toml
263
+
# Creating a new Durable Object class
264
+
[[migrations]]
265
+
tag = "v1" # Migration identifier. This should be unique for each migration entry
266
+
new_classes = ["DurableObjectExample"] # Array of new classes
267
+
```
268
+
269
+
</WranglerConfig>
270
+
271
+
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. */}
272
+
273
+
## Enable SQLite storage backend on new Durable Object class migration
111
274
112
275
:::note[SQLite in Durable Objects Beta]
113
276
114
277
The new beta version of Durable Objects is available where each Durable Object has a private, embedded SQLite database. When deploying a new Durable Object class, users can opt-in to a SQLite storage backend in order to access new [SQL API](/durable-objects/api/sql-storage/#exec). Otherwise, a Durable Object class has a key-value storage backend.
115
278
116
279
:::
117
280
118
-
To allow a new Durable Object class to use a SQLite storage backend, use `new_sqlite_classes` on the migration in your Worker's Wrangler file:
119
-
120
-
281
+
To allow a new Durable Object class to use a SQLite storage backend, use `new_sqlite_classes` on the migration in your Worker's `wrangler` configuration file:
121
282
122
283
<WranglerConfig>
123
284
@@ -129,4 +290,6 @@ new_sqlite_classes = ["MyDurableObject"] # Array of new classes
129
290
130
291
</WranglerConfig>
131
292
293
+
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).
294
+
132
295
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