Skip to content

Commit b817136

Browse files
polish: Give a copy-paste config when [migrations] are missing (#1152)
This gives a slightly better message when migrations are missing for declared durable objcts. Specifically, it gives a copy-pastable section to add to wrangler.toml, and doesn't show the warning at all for invalid class names anymore. Partially makes #1076 better.
1 parent a8c509a commit b817136

File tree

5 files changed

+64
-19
lines changed

5 files changed

+64
-19
lines changed

.changeset/rare-chicken-cross.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
polish: Give a copy-paste config when `[migrations]` are missing
6+
7+
This gives a slightly better message when migrations are missing for declared durable objcts. Specifically, it gives a copy-pastable section to add to wrangler.toml, and doesn't show the warning at all for invalid class names anymore.
8+
9+
Partially makes https://github.com/cloudflare/wrangler2/issues/1076 better.

packages/wrangler/src/__tests__/configuration.test.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,15 @@ describe("normalizeAndValidateConfig()", () => {
678678
"Processing wrangler configuration:
679679
- \\"unsafe\\" fields are experimental and may change or break at any time.
680680
- \\"services\\" fields are experimental and may change or break at any time.
681-
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS1), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
681+
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS1), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
682+
683+
\`\`\`
684+
[[migrations]]
685+
tag = \\"v1\\" # Should be unique for each entry
686+
new_classes = [\\"CLASS1\\"]
687+
\`\`\`
688+
689+
Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
682690
`);
683691
});
684692

@@ -1157,12 +1165,9 @@ describe("normalizeAndValidateConfig()", () => {
11571165
durable_objects: { bindings: expect.anything },
11581166
})
11591167
);
1160-
expect(diagnostics.hasWarnings()).toBe(true);
1168+
expect(diagnostics.hasWarnings()).toBe(false);
1169+
11611170
expect(diagnostics.hasErrors()).toBe(true);
1162-
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1163-
"Processing wrangler configuration:
1164-
- In wrangler.toml, you have configured [durable_objects] exported by this Worker ((unnamed), (unnamed), 1666, SomeClass, 1883), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
1165-
`);
11661171
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
11671172
"Processing wrangler configuration:
11681173
@@ -2474,12 +2479,8 @@ describe("normalizeAndValidateConfig()", () => {
24742479
durable_objects: { bindings: expect.anything },
24752480
})
24762481
);
2477-
expect(diagnostics.hasWarnings()).toBe(true);
2482+
expect(diagnostics.hasWarnings()).toBe(false);
24782483
expect(diagnostics.hasErrors()).toBe(true);
2479-
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2480-
"Processing wrangler configuration:
2481-
- In wrangler.toml, you have configured [durable_objects] exported by this Worker ((unnamed), (unnamed), 1666), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
2482-
`);
24832484
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
24842485
"Processing wrangler configuration:
24852486

packages/wrangler/src/__tests__/dev.test.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,15 @@ describe("wrangler dev", () => {
704704
705705
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS_1,
706706
CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
707-
section to your wrangler.toml. Refer to
707+
section to your wrangler.toml. Add this configuration to your wrangler.toml:
708+
709+
\`\`\`
710+
[[migrations]]
711+
tag = \\"v1\\" # Should be unique for each entry
712+
new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
713+
\`\`\`
714+
715+
Refer to
708716
https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
709717
for more details.
710718

packages/wrangler/src/__tests__/publish.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -2895,7 +2895,15 @@ addEventListener('fetch', event => {});`
28952895
28962896
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass),
28972897
but no [migrations] for them. This may not work as expected until you add a [migrations] section
2898-
to your wrangler.toml. Refer to
2898+
to your wrangler.toml. Add this configuration to your wrangler.toml:
2899+
2900+
\`\`\`
2901+
[[migrations]]
2902+
tag = \\"v1\\" # Should be unique for each entry
2903+
new_classes = [\\"SomeClass\\"]
2904+
\`\`\`
2905+
2906+
Refer to
28992907
https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
29002908
for more details.
29012909

packages/wrangler/src/config/validation.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,32 @@ function normalizeAndValidateMigrations(
453453
(binding) => !binding.script_name
454454
);
455455
if (exportedDurableObjects.length > 0 && rawMigrations.length === 0) {
456-
diagnostics.warnings.push(
457-
`In wrangler.toml, you have configured [durable_objects] exported by this Worker (${exportedDurableObjects
458-
.map((durable) => durable.class_name || "(unnamed)")
459-
.join(
456+
if (
457+
!exportedDurableObjects.some(
458+
(exportedDurableObject) =>
459+
typeof exportedDurableObject.class_name !== "string"
460+
)
461+
) {
462+
const durableObjectClassnames = exportedDurableObjects.map(
463+
(durable) => durable.class_name
464+
);
465+
466+
diagnostics.warnings.push(
467+
`In wrangler.toml, you have configured [durable_objects] exported by this Worker (${durableObjectClassnames.join(
460468
", "
461-
)}), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details.`
462-
);
469+
)}), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
470+
471+
\`\`\`
472+
[[migrations]]
473+
tag = "v1" # Should be unique for each entry
474+
new_classes = [${durableObjectClassnames
475+
.map((name) => `"${name}"`)
476+
.join(", ")}]
477+
\`\`\`
478+
479+
Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details.`
480+
);
481+
}
463482
}
464483
}
465484

0 commit comments

Comments
 (0)