Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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/eight-melons-rule.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a bit more detail to this change log. Maybe some kind of example showing how the previous setup of one --table flag still works like before, but now also supporting adding multiple --table flags to get multiple tables.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Fix `wrangler d1 export` failing when multiple tables are exported.
40 changes: 40 additions & 0 deletions packages/wrangler/src/__tests__/d1/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,46 @@ describe("export", () => {
`[Error: Found a database with name or binding D1 but it is missing a database_id, which is needed for operations on remote resources. Please create the remote D1 database by deploying your project or running 'wrangler d1 create D1'.]`
);
});

it("should handle multiple tables", async ({ expect }) => {
setIsTTY(false);
writeWranglerConfig({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});

// Fill with data
fs.writeFileSync(
"data.sql",
`
CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);
CREATE TABLE bar(id INTEGER PRIMARY KEY, value TEXT);
CREATE TABLE baz(id INTEGER PRIMARY KEY, value TEXT);
INSERT INTO foo (value) VALUES ('xxx'),('yyy'),('zzz');
INSERT INTO bar (value) VALUES ('aaa'),('bbb'),('ccc');
INSERT INTO baz (value) VALUES ('111'),('222'),('333');
`
);
await runWrangler("d1 execute db --file data.sql");

await runWrangler(
"d1 export db --output test-multiple.sql --table foo --table baz"
);
expect(fs.readFileSync("test-multiple.sql", "utf8")).toBe(
[
"PRAGMA defer_foreign_keys=TRUE;",
"CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);",
"INSERT INTO \"foo\" VALUES(1,'xxx');",
"INSERT INTO \"foo\" VALUES(2,'yyy');",
"INSERT INTO \"foo\" VALUES(3,'zzz');",
"CREATE TABLE baz(id INTEGER PRIMARY KEY, value TEXT);",
"INSERT INTO \"baz\" VALUES(1,'111');",
"INSERT INTO \"baz\" VALUES(2,'222');",
"INSERT INTO \"baz\" VALUES(3,'333');",
].join("\n")
);
});
});

function mockResponses() {
Expand Down
7 changes: 2 additions & 5 deletions packages/wrangler/src/d1/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const d1ExportCommand = createCommand({
table: {
type: "string",
description: "Specify which tables to include in export",
array: true,
},
"no-schema": {
type: "boolean",
Expand Down Expand Up @@ -91,11 +92,7 @@ export const d1ExportCommand = createCommand({
}

// Allow multiple --table x --table y flags or none
const tables: string[] = table
? Array.isArray(table)
? table
: [table]
: [];
const tables = table ?? [];

if (remote) {
return await exportRemotely(config, name, output, tables, !schema, !data);
Expand Down
Loading