(
env: Env,
day: string,
opts: { cutoff: string; reset?: boolean },
)
| 201 | * real day is the one irreversible thing this file could do. |
| 202 | */ |
| 203 | export async function rollupDay( |
| 204 | env: Env, |
| 205 | day: string, |
| 206 | opts: { cutoff: string; reset?: boolean }, |
| 207 | ): Promise<DayResult> { |
| 208 | const pastRetention = day < opts.cutoff; |
| 209 | const statements: D1PreparedStatement[] = []; |
| 210 | |
| 211 | if (opts.reset && !pastRetention) { |
| 212 | for (const table of EVENT_DERIVED_TABLES) { |
| 213 | statements.push(env.DB.prepare(`DELETE FROM ${table} WHERE day = ?`).bind(day)); |
| 214 | } |
| 215 | } |
| 216 | for (const sql of ROLLUP_STATEMENTS) { |
| 217 | statements.push(env.DB.prepare(sql).bind(day)); |
| 218 | } |
| 219 | |
| 220 | const results = await env.DB.batch(statements); |
| 221 | const rows = results.reduce((total, r) => total + (r.meta?.changes ?? 0), 0); |
| 222 | return { day, rows, pastRetention }; |
| 223 | } |
| 224 | |
| 225 | export interface PurgeResult { |
| 226 | /** Everything strictly before this day was deleted. */ |
no test coverage detected