(env: Env, cutoff: string)
| 237 | * keyset subquery stays a cheap index range scan on (day, event). |
| 238 | */ |
| 239 | export async function purgeOldEvents(env: Env, cutoff: string): Promise<PurgeResult> { |
| 240 | const del = env.DB.prepare( |
| 241 | `DELETE FROM events WHERE id IN (SELECT id FROM events WHERE day < ? LIMIT ${PURGE_BATCH_ROWS})`, |
| 242 | ); |
| 243 | let deleted = 0; |
| 244 | for (let batch = 1; batch <= PURGE_MAX_BATCHES; batch++) { |
| 245 | const { meta } = await del.bind(cutoff).run(); |
| 246 | const removed = meta?.changes ?? 0; |
| 247 | deleted += removed; |
| 248 | if (removed < PURGE_BATCH_ROWS) return { cutoff, deleted, batches: batch, capped: false }; |
| 249 | } |
| 250 | return { cutoff, deleted, batches: PURGE_MAX_BATCHES, capped: true }; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * The cron body: roll up the completed day and the two before it, then purge. |
no test coverage detected