Skip to content

Commit 258e52e

Browse files
committed
Docs enhancement part 1
1 parent 29b43d9 commit 258e52e

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/content/docs/durable-objects/reference/durable-objects-migrations.mdx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ sidebar:
55
order: 2
66
---
77

8-
import { GlossaryTooltip } from "~/components";
8+
import { GlossaryTooltip, WranglerConfig } from "~/components";
99

10-
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`.
1116

1217
You must initiate a migration process when you:
1318

@@ -16,27 +21,25 @@ You must initiate a migration process when you:
1621
- Delete a Durable Object class.
1722
- Transfer an existing Durable Objects class.
1823

19-
This process informs the Workers runtime of the changes and provides it with instructions on how to deal with those changes.
20-
2124
:::note
2225

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.
2427

2528
:::
2629

2730
## Create migration
2831

2932
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.
3033

31-
import { WranglerConfig } from "~/components";
34+
To apply a create migration, edit your `wrangler.json` file in the following way:
3235

3336
<WranglerConfig>
3437

3538
```toml
3639
[[migrations]]
37-
tag = "<v1>" # Should be unique for each entry
40+
tag = "<v1>" # Migration identifier. This should be unique for each migration entry
3841
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
4043
```
4144

4245
</WranglerConfig>
@@ -47,13 +50,15 @@ Running a delete migration will delete all Durable Objects associated with the d
4750

4851
- 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.
4952
- 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:
5156

5257
<WranglerConfig>
5358

5459
```toml
5560
[[migrations]]
56-
tag = "<v2>"
61+
tag = "<v2>" # Migration identifier. This should be unique for each migration entry
5762
deleted_classes = ["<ClassToDelete>"] # Array of deleted class names
5863
```
5964

@@ -63,6 +68,8 @@ deleted_classes = ["<ClassToDelete>"] # Array of deleted class names
6368

6469
Rename migrations are used to transfer stored Durable Objects between two Durable Object classes in the same Worker code file.
6570

71+
To apply a rename migration, edit your `wrangler.json` file in the following way:
72+
6673
<WranglerConfig>
6774

6875
```toml
@@ -71,15 +78,15 @@ name = "<MY_DURABLE_OBJECT>"
7178
class_name = "<UpdatedDurableObject>" # Update the class name to the new class name
7279

7380
[[migrations]]
74-
tag = "<v2>"
81+
tag = "<v2>" # Migration identifier. This should be unique for each migration entry
7582
renamed_classes = [{from = "<OldDurableObject>", to = "<UpdatedDurableObject>" }] # Array of rename directives
7683
```
7784

7885
</WranglerConfig>
7986

8087
## Transfer migration
8188

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.
8390

8491
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.
8592

@@ -89,6 +96,8 @@ Do not run a [Create migration](#create-migration) for the destination class bef
8996

9097
:::
9198

99+
To apply a transfer migration, edit your `wrangler.json` file in the following way:
100+
92101
<WranglerConfig>
93102

94103
```toml
@@ -97,7 +106,7 @@ name = "<MY_DURABLE_OBJECT>"
97106
class_name = "<DestinationDurableObjectClass>"
98107

99108
[[migrations]]
100-
tag = "<v1>"
109+
tag = "<v1>" # Migration identifier. This should be unique for each migration entry
101110
transferred_classes = [{from = "<SourceDurableObjectClass>", from_script = "<SourceWorkerScript>", to = "<DestinationDurableObjectClass>" }]
102111
```
103112

@@ -133,20 +142,24 @@ Note that `.toml` files do not allow line breaks in inline tables (the `{key = "
133142

134143
:::
135144

145+
## Examples of Durable Object migration
146+
136147
To illustrate an example migrations workflow, the `DurableObjectExample` class can be initially defined with:
137148

138149
<WranglerConfig>
139150

140151
```toml
141152
# Creating a new Durable Object class
142153
[[migrations]]
143-
tag = "v1" # Should be unique for each entry
154+
tag = "v1" # Migration identifier. This should be unique for each migration entry
144155
new_classes = ["DurableObjectExample"] # Array of new classes
145156
```
146157

147158
</WranglerConfig>
148159

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
150163

151164
To create new classes, add the following to your `wrangler` configuration file and deploy the Worker:
152165

@@ -170,6 +183,8 @@ new_classes = ["DeprecatedClass","DurableObjectExample"] # Array of new classes
170183

171184
</WranglerConfig>
172185

186+
### Delete migration example
187+
173188
To run the Delete migration, first remove the binding for the `DeprecatedClass` and deploy the Worker.
174189

175190
<WranglerConfig>
@@ -190,6 +205,8 @@ new_classes = ["DeprecatedClass","DurableObjectExample"] # Array of new classes
190205

191206
If you just want to run the Delete migration, add the Delete migration configuration and deploy the Worker.
192207

208+
### Rename migration example
209+
193210
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.
194211

195212
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
216233

217234
</WranglerConfig>
218235

236+
### Transfer migration example
237+
219238
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:
220239

221240
<WranglerConfig>
@@ -254,4 +273,6 @@ new_sqlite_classes = ["MyDurableObject"] # Array of new classes
254273

255274
</WranglerConfig>
256275

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+
257278
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

Comments
 (0)