Skip to content

Commit f3c2f69

Browse files
authoredJan 9, 2025··
Default D1 export to --local (#7694)
1 parent 773bda8 commit f3c2f69

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed
 

‎.changeset/young-icons-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Default wrangler d1 export to --local rather than failing

‎packages/wrangler/src/__tests__/d1/export.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ describe("export", () => {
1818
const { setIsTTY } = useMockIsTTY();
1919

2020
it("should throw if output is missing", async () => {
21-
await expect(runWrangler("d1 export db --local")).rejects.toThrowError(
21+
await expect(runWrangler("d1 export db")).rejects.toThrowError(
2222
`Missing required argument: output`
2323
);
2424
});
2525

26+
it("should throw if local and remote are both set", async () => {
27+
await expect(
28+
runWrangler("d1 export db --local --remote --output test-local.sql")
29+
).rejects.toThrowError("Arguments local and remote are mutually exclusive");
30+
});
31+
2632
it("should handle local", async () => {
2733
setIsTTY(false);
2834
writeWranglerConfig({
@@ -32,7 +38,7 @@ describe("export", () => {
3238
});
3339

3440
// Verify the basic command works with an empty DB
35-
await runWrangler("d1 export db --local --output test-local.sql");
41+
await runWrangler("d1 export db --output test-local.sql");
3642
expect(fs.readFileSync("test-local.sql", "utf8")).toBe(
3743
"PRAGMA defer_foreign_keys=TRUE;"
3844
);
@@ -47,7 +53,7 @@ describe("export", () => {
4753
INSERT INTO bar (value) VALUES ('aaa'),('bbb'),('ccc');
4854
`
4955
);
50-
await runWrangler("d1 execute db --local --file data.sql");
56+
await runWrangler("d1 execute db --file data.sql");
5157

5258
// SQL output expectations
5359
const create_foo = "CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);";
@@ -64,7 +70,7 @@ describe("export", () => {
6470
];
6571

6672
// Full export
67-
await runWrangler("d1 export db --local --output test-full.sql");
73+
await runWrangler("d1 export db --output test-full.sql");
6874
expect(fs.readFileSync("test-full.sql", "utf8")).toBe(
6975
[
7076
"PRAGMA defer_foreign_keys=TRUE;",
@@ -76,27 +82,21 @@ describe("export", () => {
7682
);
7783

7884
// Schema only
79-
await runWrangler(
80-
"d1 export db --local --output test-schema.sql --no-data"
81-
);
85+
await runWrangler("d1 export db --output test-schema.sql --no-data");
8286
expect(fs.readFileSync("test-schema.sql", "utf8")).toBe(
8387
["PRAGMA defer_foreign_keys=TRUE;", create_foo, create_bar].join("\n")
8488
);
8589

8690
// Data only
87-
await runWrangler(
88-
"d1 export db --local --output test-data.sql --no-schema"
89-
);
91+
await runWrangler("d1 export db --output test-data.sql --no-schema");
9092
expect(fs.readFileSync("test-data.sql", "utf8")).toBe(
9193
["PRAGMA defer_foreign_keys=TRUE;", ...insert_foo, ...insert_bar].join(
9294
"\n"
9395
)
9496
);
9597

9698
// Foo only
97-
await runWrangler(
98-
"d1 export db --local --output test-data.sql --table foo"
99-
);
99+
await runWrangler("d1 export db --output test-data.sql --table foo");
100100
expect(fs.readFileSync("test-data.sql", "utf8")).toBe(
101101
["PRAGMA defer_foreign_keys=TRUE;", create_foo, ...insert_foo].join("\n")
102102
);

‎packages/wrangler/src/d1/export.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ export function Options(yargs: CommonYargsArgv) {
7272

7373
type HandlerOptions = StrictYargsOptionsToInterface<typeof Options>;
7474
export const Handler = async (args: HandlerOptions): Promise<void> => {
75-
const { local, remote, name, output, schema, data, table } = args;
75+
const { remote, name, output, schema, data, table } = args;
7676
await printWranglerBanner();
7777
const config = readConfig(args);
7878

79-
if (!local && !remote) {
80-
throw new UserError(`You must specify either --local or --remote`);
81-
}
82-
8379
if (!schema && !data) {
8480
throw new UserError(`You cannot specify both --no-schema and --no-data`);
8581
}
@@ -91,10 +87,10 @@ export const Handler = async (args: HandlerOptions): Promise<void> => {
9187
: [table]
9288
: [];
9389

94-
if (local) {
95-
return await exportLocal(config, name, output, tables, !schema, !data);
96-
} else {
90+
if (remote) {
9791
return await exportRemotely(config, name, output, tables, !schema, !data);
92+
} else {
93+
return await exportLocal(config, name, output, tables, !schema, !data);
9894
}
9995
};
10096

0 commit comments

Comments
 (0)
Please sign in to comment.