Skip to content

feat: move the database from Turso to Cloudflare D1 - #78

Merged
ephraimduncan merged 2 commits into
mainfrom
migrate-to-d1
Oct 30, 2026
Merged

ephraimduncan merged 2 commits into
mainfrom
migrate-to-d1

Conversation

@ephraimduncan

@ephraimduncan ephraimduncan commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

minimal.so already ran as a Cloudflare Worker, but its database was on Turso. This change moves the database to Cloudflare D1. After it merges, the app, database, avatars, email, rate limits, and deploys all run on Cloudflare. Polar (billing) and Google (sign-in) stay external.

Cutover status

The data cutover is complete. On 2026-10-29 at 21:26 UTC, scripts/turso-to-d1.ts copied the Turso data into D1 (1,149 users, 15,001 bookmarks). Every table matched in rows and bytes. Then wrangler deploy put the first commit of this PR live as version 52a9794c. The Turso version before it is 5dc61054.

Merge this PR next. Workers Builds then deploys the same code and the rate-limit fix below. This merge has no new migration, so the default Workers Builds deploy command works.

A rollback to 5dc61054 serves the Turso data from before 21:26 UTC and loses rows written to D1 after that time. To recover data, use D1 Time Travel instead (DEPLOY.md section 2).

D1 limits

D1 has limits that Turso did not have. Without this change, these actions return a 500 on D1:

Action D1 limit
Move, delete, or change visibility on about 97 or more selected bookmarks 100 bound values per query
Browser import through the extension (500 rows per insert) 100 bound values per query
Extension check of a window with 100 tabs 100 bound values per query
REST GET /api/bookmarks?search= with more than 48 bytes 50-byte LIKE pattern

A long ID list now goes to D1 as one JSON array through json_each. A short list keeps a plain IN list, so SQLite can still use the primary key. The import sends its inserts in one db.batch, thus an import is all or nothing. The REST search uses instr, so % and _ in a search are literal characters.

D1 has no interactive transactions. The REST create used a transaction to stop duplicate URLs. It now uses one INSERT ... WHERE NOT EXISTS statement, so parallel requests for one URL save one bookmark.

D1 also caps a row at 2 MB, and a bookmark can store its URL three times. Thus, the app now rejects URLs longer than 64 KiB with a 400. Only one production bookmark is that long: a 1.2 MB javascript: bookmarklet. The import kept its URL but removed its normalizedUrl key, because D1 cannot hold the second copy.

Auth rate limits

better-auth reads the client IP from X-Forwarded-For, but a request to a Worker does not have that header. After the move to Workers, better-auth found no IP and skipped its limits on sign-in, sign-up, and password reset. The production log shows Rate limiting skipped: could not determine client IP address. better-auth now reads CF-Connecting-IP, so three failed sign-ins from one IP in 10 seconds get a 429.

Data copy and schema

scripts/turso-to-d1.ts reads a Turso SQL dump with bun:sqlite and applies the Drizzle migrations to D1. Then it copies the rows with explicit column lists. The script appends long values in chunks, so each statement stays under the 100 KB limit of D1. At the end, it compares the row count and byte total of each table.

lib/db/schema.ts stays the source of the schema, and drizzle/0000_baseline.sql holds all of it. The Turso tables came from Prisma. They use other type names and a different column order, but they hold the same columns, so the import copied data only.

bun run deploy applies remote migrations before wrangler deploy. Workers Builds does not do this yet. Before the next schema change, set its deploy command to bun run db:migrate:remote && npx wrangler deploy and give its build token the D1 Edit permission (DEPLOY.md section 7).

Production builds had no VITE_CHROME_EXTENSION_ID, so the import button in settings did not work on minimal.so. .env.production now holds this public ID. The change also adds the favicon again, because the move from Next.js did not keep it.

Outside this repository

Vercel still serves www.minimal.so and sends a redirect to the apex. A Cloudflare redirect rule replaces it (DEPLOY.md section 8). The Vercel project also serves the old Next.js app at minimal-ephraim.vercel.app, and that app reads and writes Turso. Delete the Vercel project after the redirect works.

When a rollback to Turso is no longer necessary, delete the TURSO_* Worker secrets. Then invalidate the Turso tokens and delete the Turso database.

The Worker reads and writes the `minimal-so` D1 database through the `DB`
binding. Drizzle migrations in `drizzle/` hold the schema, and wrangler
applies them.

D1 binds at most 100 values per query and rejects LIKE patterns longer than
50 bytes. Bulk actions, the extension import, the extension URL check, and the
REST search do not go above these limits. D1 has no interactive transactions,
thus the REST create uses one INSERT ... WHERE NOT EXISTS statement.

`scripts/turso-to-d1.ts` copies the rows of a Turso dump into D1. Then it
compares the rows and bytes of each table. DEPLOY.md gives the cutover and
rollback steps.

Production builds include the public Chrome extension id from
`.env.production`. This commit also adds the favicon again and removes Redis,
Vercel, and Next.js files that the app does not use.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
minimal-so 0fb269b Oct 29 2026, 09:30 PM

better-auth reads the client IP from X-Forwarded-For. A request to a Worker
does not have that header, so better-auth did not find an IP and skipped its
rate limits on sign-in, sign-up, and password reset. CF-Connecting-IP holds the
client IP on Cloudflare, and better-auth now reads it.
@ephraimduncan
ephraimduncan merged commit 89e5d0e into main Oct 30, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant