amber-backend/pb_hooks
Claude c3635f0e7c client_logs retention: delete every row past 14 days, not 500 a night
The nightly trimClientLogs cron fetched one page of 500 records older than
14 days and deleted those, while about 1275 rows arrive a day. Measured on
production (read-only) on 2026-09-13: 30213 rows, 14420 of them past the
cutoff, the oldest from 2026-08-19, so the backlog grew by roughly 775 rows
a day and the documented 14-day retention never held.

The job now deletes in batches of 2000 with plain SQL until a batch comes
back short, so one run clears everything past the cutoff. Plain SQL instead
of $app.delete() per record is safe here: client_logs has no file fields, no
collection has a relation pointing at it and no hook watches its deletes, so
the record path would only add a transaction and a cascade lookup per row.
Batches keep each write lock short. The run logs "trimClientLogs" with the
deleted count and the cutoff to PocketBase's logs, and a failure is logged
instead of swallowed silently as before.

scripts/test_client_logs_trim.py starts a throwaway PocketBase 0.39.6 with
this repo's migrations and hooks, seeds stale and fresh rows, fires the job
through POST /api/crons/trimClientLogs and checks what is left. Against the
old hook it failed: 800 of 1300 stale rows left, no log line. With the fix:
0 of 1300 left, all 40 in-window rows kept (one just inside the cutoff), and
0 of 15000 stale rows left in 0.4 s at production scale.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 22:32:34 +02:00
..
client_logs.pb.js client_logs retention: delete every row past 14 days, not 500 a night 2026-09-13 22:32:34 +02:00
device_auth.pb.js fix(device-auth): inline helpers into each hook handler (JSVM scope) 2026-07-19 19:48:03 +02:00
device_page.pb.js device approve page: manual code-entry for the QR-less path (#55) 2026-07-20 11:06:51 +02:00
onboarding.pb.js The code has to choose the template, not the clock 2026-08-12 21:35:46 +02:00
profile_pin.pb.js Add /api/amber/set-pin: hidden pinHash rejects client writes (#13/#14) 2026-07-21 21:29:02 +02:00
status.pb.js Show which provider's bytes were slow 2026-09-07 22:26:28 +02:00
update.pb.js Stop choosing an update variant, and drop the 18+ controls 2026-08-30 04:09:10 +02:00
web.pb.js Shadow the stale pb_public pages the deploy leaves behind 2026-08-07 02:13:31 +02:00
whoami.pb.js Stop choosing an update variant, and drop the 18+ controls 2026-08-30 04:09:10 +02:00