Skip to content

Commit ed2695e

Browse files
committed
fix(wrangler): handle d1 export with multiple tables
this was clearly intended to work, and did work in earlier version, but has been broken. added a test and fixed the command args.
1 parent 150ef7b commit ed2695e

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,46 @@ describe("export", () => {
191191
`[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'.]`
192192
);
193193
});
194+
195+
it("should handle multiple tables", async ({ expect }) => {
196+
setIsTTY(false);
197+
writeWranglerConfig({
198+
d1_databases: [
199+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
200+
],
201+
});
202+
203+
// Fill with data
204+
fs.writeFileSync(
205+
"data.sql",
206+
`
207+
CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);
208+
CREATE TABLE bar(id INTEGER PRIMARY KEY, value TEXT);
209+
CREATE TABLE baz(id INTEGER PRIMARY KEY, value TEXT);
210+
INSERT INTO foo (value) VALUES ('xxx'),('yyy'),('zzz');
211+
INSERT INTO bar (value) VALUES ('aaa'),('bbb'),('ccc');
212+
INSERT INTO baz (value) VALUES ('111'),('222'),('333');
213+
`
214+
);
215+
await runWrangler("d1 execute db --file data.sql");
216+
217+
await runWrangler(
218+
"d1 export db --output test-multiple.sql --table foo --table baz"
219+
);
220+
expect(fs.readFileSync("test-multiple.sql", "utf8")).toBe(
221+
[
222+
"PRAGMA defer_foreign_keys=TRUE;",
223+
"CREATE TABLE foo(id INTEGER PRIMARY KEY, value TEXT);",
224+
"INSERT INTO \"foo\" VALUES(1,'xxx');",
225+
"INSERT INTO \"foo\" VALUES(2,'yyy');",
226+
"INSERT INTO \"foo\" VALUES(3,'zzz');",
227+
"CREATE TABLE baz(id INTEGER PRIMARY KEY, value TEXT);",
228+
"INSERT INTO \"baz\" VALUES(1,'111');",
229+
"INSERT INTO \"baz\" VALUES(2,'222');",
230+
"INSERT INTO \"baz\" VALUES(3,'333');",
231+
].join("\n")
232+
);
233+
});
194234
});
195235

196236
function mockResponses() {

packages/wrangler/src/d1/export.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const d1ExportCommand = createCommand({
5050
table: {
5151
type: "string",
5252
description: "Specify which tables to include in export",
53+
array: true,
5354
},
5455
"no-schema": {
5556
type: "boolean",
@@ -91,11 +92,7 @@ export const d1ExportCommand = createCommand({
9192
}
9293

9394
// Allow multiple --table x --table y flags or none
94-
const tables: string[] = table
95-
? Array.isArray(table)
96-
? table
97-
: [table]
98-
: [];
95+
const tables = table ?? [];
9996

10097
if (remote) {
10198
return await exportRemotely(config, name, output, tables, !schema, !data);

0 commit comments

Comments
 (0)