#!/usr/bin/env bun /** * Database cleanup script * Removes expired OAuth states and optionally old sessions * Run via cron: 0 * * * * /home/exedev/.bun/bin/bun /home/exedev/std.pub/scripts/cleanup.ts */ import { Database } from "bun:sqlite"; import * as path from "path"; const DATA_DIR = process.env.DATA_DIR || "./data"; const DB_PATH = path.join(DATA_DIR, "oauth.db"); try { const db = new Database(DB_PATH); // Clean up OAuth states older than 1 hour const statesResult = db.run( `DELETE FROM oauth_states WHERE created_at < strftime('%s', 'now') - 3600`, ); // Clean up sessions older than 30 days (optional - sessions may still be valid) const sessionsResult = db.run( `DELETE FROM oauth_sessions WHERE updated_at < strftime('%s', 'now') - 2592000`, ); // Vacuum the database to reclaim space db.run("VACUUM"); const timestamp = new Date().toISOString(); console.log( `[${timestamp}] Cleanup complete: removed old states and sessions, vacuumed database`, ); db.close(); } catch (error) { console.error("Cleanup failed:", error); process.exit(1); }