Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default D1 export to --local #7694

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/young-icons-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Default wrangler d1 export to --local rather than failing
26 changes: 13 additions & 13 deletions packages/wrangler/src/__tests__/d1/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;"
);
Expand All @@ -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);";
Expand All @@ -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;",
Expand All @@ -76,27 +82,21 @@ 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"
)
);

// 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")
);
Expand Down
12 changes: 4 additions & 8 deletions packages/wrangler/src/d1/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,10 @@ export function Options(yargs: CommonYargsArgv) {

type HandlerOptions = StrictYargsOptionsToInterface<typeof Options>;
export const Handler = async (args: HandlerOptions): Promise<void> => {
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`);
}
Expand All @@ -91,10 +87,10 @@ export const Handler = async (args: HandlerOptions): Promise<void> => {
: [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);
}
};

Expand Down
Loading