diff --git a/.changeset/young-icons-rescue.md b/.changeset/young-icons-rescue.md new file mode 100644 index 000000000000..a1e3851c1bdf --- /dev/null +++ b/.changeset/young-icons-rescue.md @@ -0,0 +1,5 @@ +--- +"wrangler": minor +--- + +Default wrangler d1 export to --local rather than failing diff --git a/packages/wrangler/src/__tests__/d1/export.test.ts b/packages/wrangler/src/__tests__/d1/export.test.ts index 4b9809f936e2..2e60d524f5bb 100644 --- a/packages/wrangler/src/__tests__/d1/export.test.ts +++ b/packages/wrangler/src/__tests__/d1/export.test.ts @@ -18,11 +18,17 @@ describe("export", () => { const { setIsTTY } = useMockIsTTY(); it("should throw if output is missing", async () => { - await expect(runWrangler("d1 export db --local")).rejects.toThrowError( + await expect(runWrangler("d1 export db")).rejects.toThrowError( `Missing required argument: output` ); }); + it("should throw if local and remote are both set", async () => { + await expect( + runWrangler("d1 export db --local --remote --output test-local.sql") + ).rejects.toThrowError("Arguments local and remote are mutually exclusive"); + }); + it("should handle local", async () => { setIsTTY(false); writeWranglerConfig({ @@ -32,7 +38,7 @@ describe("export", () => { }); // Verify the basic command works with an empty DB - await runWrangler("d1 export db --local --output test-local.sql"); + await runWrangler("d1 export db --output test-local.sql"); expect(fs.readFileSync("test-local.sql", "utf8")).toBe( "PRAGMA defer_foreign_keys=TRUE;" ); @@ -47,7 +53,7 @@ describe("export", () => { INSERT INTO bar (value) VALUES ('aaa'),('bbb'),('ccc'); ` ); - await runWrangler("d1 execute db --local --file data.sql"); + await runWrangler("d1 execute db --file data.sql"); // SQL output expectations const create_foo = "CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);"; @@ -64,7 +70,7 @@ describe("export", () => { ]; // Full export - await runWrangler("d1 export db --local --output test-full.sql"); + await runWrangler("d1 export db --output test-full.sql"); expect(fs.readFileSync("test-full.sql", "utf8")).toBe( [ "PRAGMA defer_foreign_keys=TRUE;", @@ -76,17 +82,13 @@ describe("export", () => { ); // Schema only - await runWrangler( - "d1 export db --local --output test-schema.sql --no-data" - ); + await runWrangler("d1 export db --output test-schema.sql --no-data"); expect(fs.readFileSync("test-schema.sql", "utf8")).toBe( ["PRAGMA defer_foreign_keys=TRUE;", create_foo, create_bar].join("\n") ); // Data only - await runWrangler( - "d1 export db --local --output test-data.sql --no-schema" - ); + await runWrangler("d1 export db --output test-data.sql --no-schema"); expect(fs.readFileSync("test-data.sql", "utf8")).toBe( ["PRAGMA defer_foreign_keys=TRUE;", ...insert_foo, ...insert_bar].join( "\n" @@ -94,9 +96,7 @@ describe("export", () => { ); // Foo only - await runWrangler( - "d1 export db --local --output test-data.sql --table foo" - ); + await runWrangler("d1 export db --output test-data.sql --table foo"); expect(fs.readFileSync("test-data.sql", "utf8")).toBe( ["PRAGMA defer_foreign_keys=TRUE;", create_foo, ...insert_foo].join("\n") ); diff --git a/packages/wrangler/src/d1/export.ts b/packages/wrangler/src/d1/export.ts index 98476c7be9e7..536285c94146 100644 --- a/packages/wrangler/src/d1/export.ts +++ b/packages/wrangler/src/d1/export.ts @@ -72,14 +72,10 @@ export function Options(yargs: CommonYargsArgv) { type HandlerOptions = StrictYargsOptionsToInterface; export const Handler = async (args: HandlerOptions): Promise => { - const { local, remote, name, output, schema, data, table } = args; + const { remote, name, output, schema, data, table } = args; await printWranglerBanner(); const config = readConfig(args); - if (!local && !remote) { - throw new UserError(`You must specify either --local or --remote`); - } - if (!schema && !data) { throw new UserError(`You cannot specify both --no-schema and --no-data`); } @@ -91,10 +87,10 @@ export const Handler = async (args: HandlerOptions): Promise => { : [table] : []; - if (local) { - return await exportLocal(config, name, output, tables, !schema, !data); - } else { + if (remote) { return await exportRemotely(config, name, output, tables, !schema, !data); + } else { + return await exportLocal(config, name, output, tables, !schema, !data); } };