Skip to content

Commit 3ce5eea

Browse files
committed
fix(wrangler): include column names in D1 SQL export INSERT statements
1 parent 86c6515 commit 3ce5eea

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Include column names in D1 SQL export INSERT statements
6+
7+
D1 SQL exports now include column names in INSERT statements (e.g., `INSERT INTO "table" ("col1","col2") VALUES(...)`). This ensures that exported SQL can be successfully imported even when the target table has columns in a different order than the original, which commonly occurs during iterative development when schemas evolve.

packages/miniflare/src/workers/d1/dumpSql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function* dumpSql(
8383

8484
const select = `SELECT ${columns.map((c) => escapeId(c.name)).join(", ")} FROM ${escapeId(table)};`;
8585
const rows_cursor = db.exec(select);
86+
const columnNames = columns.map((c) => escapeId(c.name)).join(",");
8687
for (const dataRow of rows_cursor.raw()) {
8788
const formattedCells = dataRow.map((cell: unknown, i: number) => {
8889
const colType = columns[i].type;
@@ -109,7 +110,7 @@ export function* dumpSql(
109110
}
110111
});
111112

112-
yield `INSERT INTO ${escapeId(table)} VALUES(${formattedCells.join(",")});`;
113+
yield `INSERT INTO ${escapeId(table)} (${columnNames}) VALUES(${formattedCells.join(",")});`;
113114
}
114115
if (stats) {
115116
stats.rows_read += rows_cursor.rowsRead;

0 commit comments

Comments
 (0)