starbasedb icon indicating copy to clipboard operation
starbasedb copied to clipboard

fix(export): stream database dump with LIMIT/OFFSET to fix large-DB failure (#59)

Open Vinzz2303 opened this issue 6 months ago • 4 comments

Fixes

Closes #59 — Database dumps do not work on large databases

Problem

The previous implementation loaded the entire database into a single string in memory before sending any HTTP response:

let dumpContent = 'SQLite format 3\0'
// ... all rows appended ...
const blob = new Blob([dumpContent], ...)
return new Response(blob, { headers })

This caused two failure modes on large databases:

  • Memory exhaustion — Durable Objects have a 1 GB memory cap. Large databases cause OOM crashes mid-request.
  • Gateway timeout — The response body is not sent until the entire dump is assembled. Cloudflare's 30-second limit fires before a single byte is delivered to the client.

Solution

1. Pre-flight metadata collection

Table names and CREATE TABLE schemas are collected before the stream opens. Any DB error returns a clean HTTP 500 (not a broken HTTP 200 with truncated body).

2. ReadableStream with LIMIT/OFFSET batching

Rows are fetched in pages of 1 000 rows and enqueued to a ReadableStream<Uint8Array> immediately. The HTTP response starts flowing after the first enqueue() call — eliminating the gateway-timeout problem entirely.

3. Event-loop yielding

await new Promise(r => setTimeout(r, 0)) is called between batches and between tables, preventing the Durable Object's single JS thread from being monopolised.

4. Correct value escaping

Type Output
null / undefined NULL
boolean 1 / 0
number bare literal
Uint8Array (BLOB) X'hexstring'
string 'single-quoted' with '' escaping

5. Exclude sqlite_ internal tables

The pre-flight query filters out sqlite_sequence, sqlite_stat1, etc. to produce a clean, importable dump.

Tests

9 tests — all passing (npx vitest run src/export/dump.test.ts):

  • ✅ Streaming response has correct Content-Type and Transfer-Encoding: chunked headers
  • ✅ Body contains CREATE TABLE + INSERT statements
  • ✅ Empty database produces valid SQL preamble/epilogue only
  • ✅ Tables with no rows are schema-only (no INSERT)
  • ✅ Single quotes in strings are properly escaped
  • ✅ NULL columns render as SQL NULL (new)
  • ✅ Pagination: full 1 000-row batch followed by partial batch = 1 001 INSERT statements (new)
  • ✅ Pre-flight DB error returns HTTP 500 (new)
  • ✅ sqlite_ internal tables excluded from dump (new)

/claim #59

Vinzz2303 avatar Jun 04 '26 16:06 Vinzz2303

Hi team 👋 This PR is fully ready for review, featuring a streaming implementation with batching and 9 functional tests. Could you please check it out or approve the workflow to let tests run? Thank you!

Vinzz2303 avatar Jun 08 '26 09:06 Vinzz2303

Hi! Friendly follow-up on this PR. I noticed the other competing PR was closed. The implementation is ready and I'd appreciate it if you could approve the CI run and review it when you have a moment. Thanks!

Vinzz2303 avatar Jun 09 '26 18:06 Vinzz2303

Validation update: I reran the targeted dump test locally with Vitest: src/export/dump.test.ts passes (9 tests). The PR is still ready for review/CI approval from my side. Thanks!

Vinzz2303 avatar Jun 10 '26 09:06 Vinzz2303

Hi team! Friendly follow-up on this PR. I've validated the targeted dump functionality locally - all 9 tests pass cleanly. The workflow run is pending maintainer approval before CI can run on GitHub. Could someone please approve the workflow and take a look at the PR when you have a moment? I'm happy to make any adjustments needed. Thank you!

Vinzz2303 avatar Jun 12 '26 12:06 Vinzz2303

@Brayden Hello! Just following up on this PR. The workflows are currently awaiting approval to run. Could you please authorize them when you have a moment?

Vinzz2303 avatar Jul 07 '26 15:07 Vinzz2303

@Vinzz2303 Wrong mention?

joshuabaker avatar Jul 07 '26 15:07 joshuabaker