Skip to content

Commit 77ed7e2

Browse files
Potentially update local config on wrangler deploy --x-remote-diff-check invocations (#11033)
--------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent 14f60e8 commit 77ed7e2

File tree

6 files changed

+511
-17
lines changed

6 files changed

+511
-17
lines changed

.changeset/cyan-tools-show.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Offer to update the local Wrangler configuration file to match remote configuration when running `wrangler deploy`
6+
7+
When running `wrangler deploy`, with `--x-remote-diff-check`, Wrangler will display the difference between local and remote configuration.
8+
If there would be a destructive change to the remote configuration, the user is given the option to cancel the deployment.
9+
In the case where the user does cancel deployment, Wrangler will now also offer to update the local Wrangler configuration file to match the remote configuration.
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
import { getConfigPatch } from "../../deploy/config-diffs";
2+
3+
// Note: __old (as well as *__deleted) is the value in the remote config, __new is the value in the local one, so we do want the
4+
// __old one to override the __new
5+
6+
describe("getConfigPatch", () => {
7+
test("top level config updated", () => {
8+
expect(
9+
getConfigPatch({
10+
preview_urls: {
11+
__old: false,
12+
__new: true,
13+
},
14+
})
15+
).toEqual({
16+
preview_urls: false,
17+
});
18+
});
19+
20+
test("env var present remotely but deleted locally", () => {
21+
expect(
22+
getConfigPatch({
23+
vars: {
24+
MY_VAR__deleted: "ABC",
25+
},
26+
})
27+
).toEqual({
28+
vars: {
29+
MY_VAR: "ABC",
30+
},
31+
});
32+
});
33+
34+
test("updated value of env var", () => {
35+
expect(
36+
getConfigPatch({
37+
vars: {
38+
MY_VAR: {
39+
__old: "ABC",
40+
__new: "123",
41+
},
42+
},
43+
})
44+
).toEqual({
45+
vars: {
46+
MY_VAR: "ABC",
47+
},
48+
});
49+
});
50+
51+
test("env var renamed", () => {
52+
expect(
53+
getConfigPatch({
54+
vars: {
55+
MY_VAR__deleted: "ABC",
56+
VAR__added: "ABC",
57+
},
58+
})
59+
).toEqual({
60+
vars: {
61+
MY_VAR: "ABC",
62+
},
63+
});
64+
});
65+
66+
test("deleted version metadata binding", () => {
67+
expect(
68+
getConfigPatch({
69+
version_metadata: {
70+
__old: {
71+
binding: "VERSION_METADATA",
72+
},
73+
__new: undefined,
74+
},
75+
})
76+
).toEqual({
77+
version_metadata: {
78+
binding: "VERSION_METADATA",
79+
},
80+
});
81+
});
82+
83+
test("deleted KV binding (only one KV)", () => {
84+
expect(
85+
getConfigPatch({
86+
kv_namespaces: [
87+
[
88+
"-",
89+
{
90+
id: "<kv-id>",
91+
binding: "MY_KV",
92+
},
93+
],
94+
],
95+
})
96+
).toEqual({
97+
kv_namespaces: [
98+
{
99+
id: "<kv-id>",
100+
binding: "MY_KV",
101+
},
102+
],
103+
});
104+
});
105+
106+
test("deleted second KV binding in the kv_namespaces array", () => {
107+
expect(
108+
getConfigPatch({
109+
kv_namespaces: [
110+
[" "],
111+
[
112+
"-",
113+
{
114+
id: "<my-kv-a>",
115+
binding: "MY_KV_A",
116+
},
117+
],
118+
],
119+
})
120+
).toEqual({
121+
kv_namespaces: [
122+
{
123+
/* unmodified kv */
124+
},
125+
{
126+
id: "<my-kv-a>",
127+
binding: "MY_KV_A",
128+
},
129+
],
130+
});
131+
});
132+
133+
test("modified KV binding", () => {
134+
expect(
135+
getConfigPatch({
136+
kv_namespaces: [
137+
[
138+
"~",
139+
{
140+
id: {
141+
__old: "<old-kv-id>",
142+
__new: "<new-kv-id>",
143+
},
144+
},
145+
],
146+
],
147+
})
148+
).toEqual({
149+
kv_namespaces: [
150+
{
151+
id: "<old-kv-id>",
152+
},
153+
],
154+
});
155+
});
156+
157+
test("deleted second KV binding in the kv_namespaces array and modified first one", () => {
158+
expect(
159+
getConfigPatch({
160+
kv_namespaces: [
161+
[" "],
162+
[
163+
"-",
164+
{
165+
id: "<my-kv-a>",
166+
binding: "MY_KV_A",
167+
},
168+
],
169+
],
170+
})
171+
).toEqual({
172+
kv_namespaces: [
173+
{
174+
/* unmodified kv */
175+
},
176+
{
177+
id: "<my-kv-a>",
178+
binding: "MY_KV_A",
179+
},
180+
],
181+
});
182+
});
183+
184+
test("deleted KV binding from the middle of the kv_namespaces array", () => {
185+
expect(
186+
getConfigPatch({
187+
kv_namespaces: [
188+
[" "],
189+
[
190+
"-",
191+
{
192+
id: "<my-kv-a>",
193+
binding: "MY_KV_A",
194+
},
195+
],
196+
[" "],
197+
],
198+
})
199+
).toEqual({
200+
kv_namespaces: [
201+
{
202+
/* unmodified kv */
203+
},
204+
{
205+
/* unmodified kv */
206+
},
207+
{
208+
/* deleted kv put back */
209+
id: "<my-kv-a>",
210+
binding: "MY_KV_A",
211+
},
212+
],
213+
});
214+
});
215+
216+
test("flipped observability.logs.invocation_logs off (nested field)", () => {
217+
expect(
218+
getConfigPatch({
219+
observability: {
220+
logs: {
221+
invocation_logs: {
222+
__old: true,
223+
__new: false,
224+
},
225+
},
226+
},
227+
})
228+
).toEqual({
229+
observability: {
230+
logs: {
231+
invocation_logs: true,
232+
},
233+
},
234+
});
235+
});
236+
237+
test("renamed version metadata binding", () => {
238+
expect(
239+
getConfigPatch({
240+
version_metadata: {
241+
binding: {
242+
__old: "VERSION_METADATA",
243+
__new: "VERSION_META",
244+
},
245+
},
246+
})
247+
).toEqual({
248+
version_metadata: {
249+
binding: "VERSION_METADATA",
250+
},
251+
});
252+
});
253+
});

packages/wrangler/src/__tests__/deploy/config-diffs.test.ts renamed to packages/wrangler/src/__tests__/deploy/get-remote-config-diff.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ describe("getRemoteConfigsDiff", () => {
4747
expect(nonDestructive).toBe(true);
4848
});
4949

50+
it("should handle a very simple diffing scenario where there is only an addition to an array (specifically in `kv_namespaces`)", () => {
51+
const { diff, nonDestructive } = getRemoteConfigDiff(
52+
{
53+
name: "silent-firefly-dbe3",
54+
main: "/tmp/src/index.js",
55+
workers_dev: true,
56+
kv_namespaces: [{ binding: "MY_KV", id: "<kv-id>" }],
57+
preview_urls: true,
58+
},
59+
{
60+
name: "silent-firefly-dbe3",
61+
main: "/tmp/src/index.js",
62+
workers_dev: true,
63+
preview_urls: true,
64+
kv_namespaces: [],
65+
} as unknown as Config
66+
);
67+
68+
assert(diff);
69+
expect(normalizeDiff(diff.toString())).toMatchInlineSnapshot(`
70+
" {
71+
kv_namespaces: [
72+
- {
73+
- binding: \\"MY_KV\\"
74+
- id: \\"<kv-id>\\"
75+
- }
76+
]
77+
}
78+
"
79+
`);
80+
expect(nonDestructive).toBe(false);
81+
});
82+
5083
it("should handle a very simple diffing scenario (some diffs, random order)", () => {
5184
const { diff, nonDestructive } = getRemoteConfigDiff(
5285
{

0 commit comments

Comments
 (0)