Skip to content

Commit 4462bc1

Browse files
authored
CFSQL-1371 reconcile dumpSql changes from D1 and document how to keep them in sync (#10686)
1 parent 42e256f commit 4462bc1

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

.changeset/plain-women-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
Document that dumpSql is shared with d1-worker, and incorporate some D1 internal stats gathering machinery (which is currently unused by miniflare)

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// NOTE: this file is duplicated between miniflare and d1-workers, and should be kept in sync by hand.
2+
// Please tag someone from the D1 team as a reviewer and they will do this for you.
3+
//
14
// NOTE: this function duplicates the logic inside SQLite's shell.c.in as close
25
// as possible, with any deviations noted.
36
import { SqlStorage } from "@cloudflare/workers-types/experimental";
@@ -8,8 +11,11 @@ export function* dumpSql(
811
noSchema?: boolean;
912
noData?: boolean;
1013
tables?: string[];
11-
}
14+
},
15+
/** Optional stats tracking. Will be mutated in place if provided */
16+
stats?: { rows_read: number; rows_written: number }
1217
) {
18+
// WARNING: the caller in D1 assumes non-empty exports, so think carefully before removing this initial yield.
1319
yield `PRAGMA defer_foreign_keys=TRUE;`;
1420

1521
// Empty set means include all tables
@@ -27,6 +33,10 @@ export function* dumpSql(
2733
`)();
2834
const tables: { name: string; type: string; sql: string }[] =
2935
Array.from(tables_cursor);
36+
if (stats) {
37+
stats.rows_read += tables_cursor.rowsRead;
38+
stats.rows_written += tables_cursor.rowsWritten;
39+
}
3040

3141
for (const { name: table, sql } of tables) {
3242
if (filterTables.size > 0 && !filterTables.has(table)) continue;
@@ -65,6 +75,10 @@ export function* dumpSql(
6575
dflt_val: string | null;
6676
pk: number;
6777
}[];
78+
if (stats) {
79+
stats.rows_read += columns_cursor.rowsRead;
80+
stats.rows_written += columns_cursor.rowsWritten;
81+
}
6882

6983
const select = `SELECT ${columns.map((c) => escapeId(c.name)).join(", ")} FROM ${escapeId(table)};`;
7084
const rows_cursor = db.exec(select);
@@ -83,25 +97,45 @@ export function* dumpSql(
8397
.call(new Uint8Array(cell), (b) => b.toString(16).padStart(2, "0"))
8498
.join("")}'`;
8599
} else {
86-
console.log({ colType, cellType, cell, column: columns[i] });
100+
console.error({
101+
message: "dumpSql: unexpected cell type",
102+
colType,
103+
cellType,
104+
cell,
105+
column: columns[i],
106+
});
87107
return "ERROR";
88108
}
89109
});
90110

91111
yield `INSERT INTO ${escapeId(table)} VALUES(${formattedCells.join(",")});`;
92112
}
113+
if (stats) {
114+
stats.rows_read += rows_cursor.rowsRead;
115+
stats.rows_written += rows_cursor.rowsWritten;
116+
}
93117
}
94118

95119
if (!noSchema) {
96120
// Taken from SQLite shell.c.in https://github.com/sqlite/sqlite/blob/105c20648e1b05839fd0638686b95f2e3998abcb/src/shell.c.in#L8473-L8478
97121
const rest_of_schema = db.exec(
98-
`SELECT name, sql FROM sqlite_schema AS o WHERE (true) AND sql NOT NULL AND type IN ('index', 'trigger', 'view') ORDER BY type COLLATE NOCASE /* DESC */;`
122+
[
123+
"SELECT name, sql",
124+
"FROM sqlite_schema AS o",
125+
"WHERE (true) AND sql NOT NULL",
126+
" AND type IN ('index', 'trigger', 'view')",
127+
// 'DESC' appears in the code linked above but the observed behaviour of SQLite appears otherwise
128+
"ORDER BY type COLLATE NOCASE",
129+
].join(" ")
99130
);
100-
// 'DESC' appears in the code linked above but the observed behaviour of SQLite appears otherwise
101131
for (const { name, sql } of rest_of_schema) {
102132
if (filterTables.size > 0 && !filterTables.has(name as string)) continue;
103133
yield `${sql};`;
104134
}
135+
if (stats) {
136+
stats.rows_read += rest_of_schema.rowsRead;
137+
stats.rows_written += rest_of_schema.rowsWritten;
138+
}
105139
}
106140
}
107141

0 commit comments

Comments
 (0)