Repository navigation
feat(cloud): move storage to D1; Durable Object becomes broadcast-only - #28
Merged
Merged
Conversation
…-only The Worker now reads and writes D1 through drizzle-orm/d1. The TimeClock Durable Object keeps its name (so its legacy storage stays addressable for the data migration) but holds no tables; the Worker notifies it after each successful D1 write and it fans out to hibernated WebSockets. - Partial unique index enforces one open session; concurrent starts retry so a device flip never gets a 4xx the firmware would drop. - Unique (company, period start) rejects duplicate concurrent retrievals. - Access service tokens may open only the read-only /api/v1/live stream. - time-clock.ts split into feature modules; device API unchanged. - Deploy applies D1 migrations before uploading the Worker. Refs #20 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The DO export endpoint is dropped here: after the cutover the DO holds no tables, and the export only has to run against the pre-cutover deploy. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warning
Merge only right after the final data pass. A merge to
maindeploys production. On 2026-10-12 production D1 got its schema and a full copy of the DO data (2 companies, 1 device, 47 sessions;VERIFY OK: 12 checks match). Anything clocked after that copy is only in the DO. Right before merging, pause the switch and dashboard, re-run export → import → verify (about a minute), then merge.What
drizzle-orm/d1.time-clock.tswent from 1,330 lines to a 50-line hub. Queries and routes now live in feature modules (sessions/,companies/,devices/,reports/,retrievals/,live/).TimeClockDurable Object holds no tables and only broadcasts over hibernated WebSockets. The Worker callsbroadcast()on it after each successful D1 write. The class name is unchanged, so its legacy SQLite storage stays reachable for 1B: Migrate DO data into D1 #21.idx_sessions_one_open(ended_at IS NULL) enforces one open session. Concurrent starts retry, so a device flip never gets a 4xx that firmware would drop. A unique index on (company, period_start) blocks duplicate concurrent retrievals.CF-Access-Client-Id/Secret) can open only the read-onlyGET /api/v1/livestream. Every other route returns 403service_token_read_only. Browser Access is unchanged./device/v1/*, HMAC contract) is unchanged, so firmware 0.2.5 needs no reflash.0005was generated with drizzle-kit. Deploy now runswrangler d1 migrations apply DB --remotebeforewrangler deploy.Why
Refs #20 (part of #18). Data was locked inside the DO, and the one-open-session rule only held because the DO handled requests one at a time.
How verified
Tests run against D1 (miniflare):
cd cloud && pnpm test: 7 files, 33 passed. That's the 25 existing tests unchanged, plus 8 new ones:UNIQUE constraint failed: index 'idx_sessions_one_open'.[201, 409 session_already_active], leaving one open row.201, leaving one open row.session.stopped, thensession.started, over WebSocket. The hub'ssqlite_masterhas no tables./api/v1/live, and a 403 onGET /companies,GET /statusandPOST /sessions. A user JWT still gets a 200. A token with no identity gets a 401.Mutation checks: I dropped the index from
0005, and 3 of the 4 D1 tests failed. I set the start retry to 1 attempt, and the concurrent device-flip test failed.Local
wrangler devrepro of a clock-in:Result: device start
201. The WebSocket receivedsnapshot(active: null), thensession.started(session = the new id, active = the new id), thendevice.changed. Reading local D1 withwrangler d1 execute DB --local --command "SELECT id, ended_at FROM sessions WHERE ended_at IS NULL"returned[{"id":"1c6d710d-…","ended_at":null}].Full cutover rehearsal, run locally on 2026-10-12 (current
main, i.e. the production code, against the code in this PR):Then I started this branch on
wrangler dev:8792 against that D1, and diffed its API output with main's, ignoring only the server clock:Signed with the device secret carried over in the export (no re-provisioning), a device start returned
201, then stop200, then heartbeat200. The WebSocket receivedsnapshot, session.stopped, session.started, device.changed, session.stopped, device.changed, …. D1 now holds the 3 migrated sessions plus the new one; the open session the switch flipped away from was closed at the flip moment.The merge commit drops the DO export endpoint from #27 on this branch: after the cutover the DO has no tables, and the export only has to run against the pre-cutover deploy.
Other gates:
pnpm format:check✅,pnpm check✅,pnpm migrate:generatereports no schema changes ✅,pnpm deploy:checkdry run ✅ (bindings includeenv.DB (time-switch) D1 Database).Not verified:
Go-live (approved by Jaren 2026-10-12; steps 1–2 done)
Run from
cloud/on this branch. Production keeps serving from the DO until step 4.wrangler d1 migrations apply time-switch --remote: schema only, into the new empty database.cloudflared access login https://time.jarenkempton.dev, thencloudflared access curl https://time.jarenkempton.dev/api/v1/admin/export -o do-export.json,node scripts/d1-migrate.mjs import do-export.json time-switch --remote, and… verify …. It must printVERIFY OK.shred -u do-export.json.Rollback: revert the merge. The DO's SQLite tables are untouched, so the old code serves them again; writes made on D1 in between would need copying back.
Risk / rollout
CLOUDFLARE_API_TOKENsecret needs D1 Edit permission, or the new migration step fails before the deploy (safe failure: nothing ships)./apiroute now returns 404 instead of 405. The device routes still return 405.time-clock.ts. It can't be split cleanly, because the storage swap touches every query.🤖 Generated with Claude Code