Skip to content

Commit 53ba97d

Browse files
Fix d1 info command showing read_replication: [object Object] (#9161)
* Fix d1 info command showing read_replication: [object Object] Also add a fallback to unexpected fields to be JSON serialized at least. * Create four-cherries-deny.md
1 parent e55f489 commit 53ba97d

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

.changeset/four-cherries-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix d1 info command showing read_replication: [object Object]

packages/wrangler/src/__tests__/d1/info.test.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ describe("info", () => {
7979
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
8080
name: "northwind",
8181
created_at: "2023-05-23T08:33:54.590Z",
82-
version: "beta",
82+
version: "production",
8383
num_tables: 13,
8484
file_size: 33067008,
8585
running_in_region: "WEUR",
86+
read_replication: {
87+
mode: "disabled",
88+
},
8689
},
8790
success: true,
8891
errors: [],
@@ -113,6 +116,9 @@ describe("info", () => {
113116
\\"created_at\\": \\"2023-05-23T08:33:54.590Z\\",
114117
\\"num_tables\\": 13,
115118
\\"running_in_region\\": \\"WEUR\\",
119+
\\"read_replication\\": {
120+
\\"mode\\": \\"disabled\\"
121+
},
116122
\\"database_size\\": 33067008,
117123
\\"read_queries_24h\\": 0,
118124
\\"write_queries_24h\\": 0,
@@ -131,10 +137,16 @@ describe("info", () => {
131137
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
132138
name: "northwind",
133139
created_at: "2023-05-23T08:33:54.590Z",
134-
version: "beta",
140+
version: "production",
135141
num_tables: 13,
136142
file_size: 33067008,
137143
running_in_region: "WEUR",
144+
read_replication: {
145+
mode: "auto",
146+
},
147+
unexpected_object: {
148+
iron: "man",
149+
},
138150
},
139151
success: true,
140152
errors: [],
@@ -164,27 +176,31 @@ describe("info", () => {
164176
⛅️ wrangler x.x.x
165177
------------------
166178
167-
┌───────────────────┬──────────────────────────────────────┐
168-
│ DB │ d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06 │
169-
├───────────────────┼──────────────────────────────────────┤
170-
│ name │ northwind │
171-
├───────────────────┼──────────────────────────────────────┤
172-
│ created_at │ 2023-05-23T08:33:54.590Z │
173-
├───────────────────┼──────────────────────────────────────┤
174-
│ num_tables │ 13 │
175-
├───────────────────┼──────────────────────────────────────┤
176-
│ running_in_region │ WEUR │
177-
├───────────────────┼──────────────────────────────────────┤
178-
│ database_size │ 33.1 MB │
179-
├───────────────────┼──────────────────────────────────────┤
180-
│ read_queries_24h │ 0 │
181-
├───────────────────┼──────────────────────────────────────┤
182-
│ write_queries_24h │ 0 │
183-
├───────────────────┼──────────────────────────────────────┤
184-
│ rows_read_24h │ 0 │
185-
├───────────────────┼──────────────────────────────────────┤
186-
│ rows_written_24h │ 0 │
187-
└───────────────────┴──────────────────────────────────────┘"
179+
┌───────────────────────┬──────────────────────────────────────┐
180+
│ DB │ d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06 │
181+
├───────────────────────┼──────────────────────────────────────┤
182+
│ name │ northwind │
183+
├───────────────────────┼──────────────────────────────────────┤
184+
│ created_at │ 2023-05-23T08:33:54.590Z │
185+
├───────────────────────┼──────────────────────────────────────┤
186+
│ num_tables │ 13 │
187+
├───────────────────────┼──────────────────────────────────────┤
188+
│ running_in_region │ WEUR │
189+
├───────────────────────┼──────────────────────────────────────┤
190+
│ unexpected_object │ {\\"iron\\":\\"man\\"} │
191+
├───────────────────────┼──────────────────────────────────────┤
192+
│ database_size │ 33.1 MB │
193+
├───────────────────────┼──────────────────────────────────────┤
194+
│ read_queries_24h │ 0 │
195+
├───────────────────────┼──────────────────────────────────────┤
196+
│ write_queries_24h │ 0 │
197+
├───────────────────────┼──────────────────────────────────────┤
198+
│ rows_read_24h │ 0 │
199+
├───────────────────────┼──────────────────────────────────────┤
200+
│ rows_written_24h │ 0 │
201+
├───────────────────────┼──────────────────────────────────────┤
202+
│ read_replication.mode │ auto │
203+
└───────────────────────┴──────────────────────────────────────┘"
188204
`);
189205
});
190206
});

packages/wrangler/src/d1/info.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const d1InfoCommand = createCommand({
4242

4343
const result = await getDatabaseInfoFromIdOrName(accountId, db.uuid);
4444

45-
const output: Record<string, string | number> = { ...result };
45+
const output: Record<string, string | number | object> = { ...result };
4646
if (output["file_size"]) {
4747
output["database_size"] = output["file_size"];
4848
delete output["file_size"];
@@ -118,6 +118,12 @@ export const d1InfoCommand = createCommand({
118118
if (json) {
119119
logger.log(JSON.stringify(output, null, 2));
120120
} else {
121+
// Selectively bring the nested read_replication info at the top level.
122+
if (result.read_replication) {
123+
output["read_replication.mode"] = result.read_replication.mode;
124+
delete output["read_replication"];
125+
}
126+
121127
// Snip off the "uuid" property from the response and use those as the header
122128
const entries = Object.entries(output).filter(
123129
// also remove any version that isn't "alpha"
@@ -134,6 +140,10 @@ export const d1InfoCommand = createCommand({
134140
k === "rows_written_24h"
135141
) {
136142
value = v.toLocaleString();
143+
} else if (typeof v === "object") {
144+
// There shouldn't be any, but in the worst case we missed something
145+
// or added a nested object in the response, serialize it instead of showing `[object Object]`.
146+
value = JSON.stringify(v);
137147
} else {
138148
value = String(v);
139149
}

packages/wrangler/src/d1/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ export type DatabaseCreationResult = {
1818
export type DatabaseInfo = {
1919
uuid: string;
2020
name: string;
21-
version: "alpha" | "beta";
21+
version: "alpha" | "beta" | "production";
2222
num_tables: number;
2323
file_size: number;
2424
running_in_region?: string;
25+
read_replication?: {
26+
mode: "auto" | "disabled";
27+
};
2528
};
2629

2730
export type Backup = {

0 commit comments

Comments
 (0)