From efb37e65fd97de729cd31208d306e4d3684bd91e Mon Sep 17 00:00:00 2001 From: Michael Burr Date: Sun, 1 Nov 2026 22:05:45 +0000 Subject: [PATCH 1/3] perf(admin): seekable keyset cursor and audit action skip-scan Emit the keyset predicate as a row-value comparison so SQLite range-seeks the created_at index instead of scanning it, make a single-action audit filter an equality that seeks the (action, created_at, id) index, and list distinct audit actions with a recursive skip-scan rather than SELECT DISTINCT over the whole log. --- packages/admin/src/lib/cursor.test.ts | 45 +++++++++++++- packages/admin/src/lib/cursor.ts | 11 +++- packages/admin/src/routes/audit.test.ts | 78 ++++++++++++++++++++++--- packages/admin/src/routes/audit.ts | 29 +++++++-- 4 files changed, 145 insertions(+), 18 deletions(-) diff --git a/packages/admin/src/lib/cursor.test.ts b/packages/admin/src/lib/cursor.test.ts index 5882013e..6a09c1e2 100644 --- a/packages/admin/src/lib/cursor.test.ts +++ b/packages/admin/src/lib/cursor.test.ts @@ -1,4 +1,5 @@ import { describe, it, expect } from "vitest"; +import { DatabaseSync } from "node:sqlite"; import { encodeCursor, decodeCursor, keysetClause } from "./cursor"; describe("keyset cursor codec", () => { @@ -30,7 +31,47 @@ describe("keysetClause", () => { it("emits the predicate + binds with the given column names", () => { const cursor = { ts: "2026-05-17T12:34:56.000Z", id: "row-9" }; const { sql, binds } = keysetClause(cursor, "p.created_at", "p.id"); - expect(sql).toBe("(p.created_at < ? OR (p.created_at = ? AND p.id < ?))"); - expect(binds).toEqual([cursor.ts, cursor.ts, cursor.id]); + expect(sql).toBe("(p.created_at, p.id) < (?, ?)"); + expect(binds).toEqual([cursor.ts, cursor.id]); + }); + + // Real SQLite: walking pages of 2 over rows with tied timestamps must visit + // every row exactly once, in (created_at DESC, id DESC) order, and the + // predicate must seek the created_at index rather than scan it. + function seeded() { + const db = new DatabaseSync(":memory:"); + db.exec("CREATE TABLE t (id TEXT PRIMARY KEY, created_at TEXT NOT NULL); CREATE INDEX t_created_at ON t(created_at DESC);"); + const ins = db.prepare("INSERT INTO t (id, created_at) VALUES (?, ?)"); + for (const [id, ts] of [["a", "2026-01-01"], ["b", "2026-01-02"], ["c", "2026-01-02"], ["d", "2026-01-02"], ["e", "2026-01-03"]]) { + ins.run(id, ts); + } + return db; + } + + it("pages through tied timestamps without skipping or repeating rows", () => { + const db = seeded(); + const seen: string[] = []; + let cursor: { ts: string; id: string } | null = null; + for (;;) { + const k = keysetClause(cursor, "created_at", "id"); + const rows = db.prepare( + `SELECT id, created_at FROM t ${k.sql ? `WHERE ${k.sql}` : ""} ORDER BY created_at DESC, id DESC LIMIT 2`, + ).all(...k.binds) as Array<{ id: string; created_at: string }>; + if (rows.length === 0) break; + seen.push(...rows.map(r => r.id)); + const last = rows[rows.length - 1]; + cursor = { ts: last.created_at, id: last.id }; + } + expect(seen).toEqual(["e", "d", "c", "b", "a"]); + }); + + it("plans as a range seek on the created_at index", () => { + const db = seeded(); + const k = keysetClause({ ts: "2026-01-02", id: "c" }, "created_at", "id"); + const plan = db.prepare(`EXPLAIN QUERY PLAN SELECT id FROM t WHERE ${k.sql} ORDER BY created_at DESC, id DESC LIMIT 2`) + .all(...k.binds) + .map(r => String(r.detail)) + .join("\n"); + expect(plan).toContain("SEARCH t USING INDEX t_created_at (created_at DESC, DESC). Column names are // caller-supplied SQL identifiers (e.g. "u.created_at" / "u.id") and must // NEVER be user input. Returns an empty clause for a null cursor (first page). +// +// A row-value comparison rather than the equivalent +// `ts < ? OR (ts = ? AND id < ?)`: SQLite turns the row value into a range +// seek on a created_at index, whereas the OR form plans as a full index scan +// (or a MULTI-INDEX OR plus a sort of everything older), so page N would read +// every row before it. Both columns are NOT NULL wherever this is used, so +// the two forms select the same rows. export function keysetClause( cursor: KeysetCursor | null, tsCol: string, @@ -49,7 +56,7 @@ export function keysetClause( ): { sql: string; binds: string[] } { if (!cursor) return { sql: "", binds: [] }; return { - sql: `(${tsCol} < ? OR (${tsCol} = ? AND ${idCol} < ?))`, - binds: [cursor.ts, cursor.ts, cursor.id], + sql: `(${tsCol}, ${idCol}) < (?, ?)`, + binds: [cursor.ts, cursor.id], }; } diff --git a/packages/admin/src/routes/audit.test.ts b/packages/admin/src/routes/audit.test.ts index 5e4900d2..814db4f3 100644 --- a/packages/admin/src/routes/audit.test.ts +++ b/packages/admin/src/routes/audit.test.ts @@ -1,5 +1,9 @@ import { describe, it, expect } from "vitest"; -import { buildAuditListQuery } from "./audit"; +import { fileURLToPath } from "node:url"; +import { buildAuditListQuery, AUDIT_ACTIONS_SQL } from "./audit"; +import { createSqliteD1 } from "../../../api/src/test/sqliteD1"; + +const AUTH_MIGRATIONS = fileURLToPath(new URL("../../../auth/migrations", import.meta.url)); const CURSOR = { ts: "2026-05-17 12:34:56", id: "a1b2-c3d4" }; @@ -15,14 +19,14 @@ describe("buildAuditListQuery", () => { it("emits the keyset predicate and binds for a cursor", () => { const { sql, binds } = buildAuditListQuery(CURSOR, NO_FILTERS); - expect(sql).toContain("created_at < ? OR (created_at = ? AND id < ?)"); - expect(sql).not.toContain("action IN"); - expect(binds).toEqual([CURSOR.ts, CURSOR.ts, CURSOR.id]); + expect(sql).toContain("WHERE (created_at, id) < (?, ?)"); + expect(sql).not.toMatch(/action (=|IN)/); + expect(binds).toEqual([CURSOR.ts, CURSOR.id]); }); - it("emits a single-element IN list for one action", () => { + it("emits an equality (index seek) for one action", () => { const { sql, binds } = buildAuditListQuery(null, { actions: ["user.ink.grant"], q: null }); - expect(sql).toContain("WHERE action IN (?)"); + expect(sql).toContain("WHERE action = ?"); expect(binds).toEqual(["user.ink.grant"]); }); @@ -58,10 +62,9 @@ describe("buildAuditListQuery", () => { q: "bob", }); expect(sql).toContain( - "(created_at < ? OR (created_at = ? AND id < ?)) AND action IN (?) AND (actor_email LIKE ? ESCAPE '\\' OR actor_user_id LIKE ? ESCAPE '\\' OR target_id LIKE ? ESCAPE '\\')", + "(created_at, id) < (?, ?) AND action = ? AND (actor_email LIKE ? ESCAPE '\\' OR actor_user_id LIKE ? ESCAPE '\\' OR target_id LIKE ? ESCAPE '\\')", ); expect(binds).toEqual([ - CURSOR.ts, CURSOR.ts, CURSOR.id, "project.delete", @@ -71,3 +74,62 @@ describe("buildAuditListQuery", () => { ]); }); }); + +// Real SQLite (every auth migration applied): the list query must page an +// action-filtered log correctly, and the actions query must return exactly +// the distinct actions, sorted. +describe("audit queries against the real schema", () => { + function seed() { + const { sqlite } = createSqliteD1(AUTH_MIGRATIONS); + const ins = sqlite.prepare( + "INSERT INTO admin_audit_log (id, actor_user_id, actor_email, action, target_type, created_at) VALUES (?, 'a', 'a@x.com', ?, 'user', ?)", + ); + // Tied timestamps across page boundaries, interleaved actions. + const rows: Array<[string, string, string]> = [ + ["r1", "user.delete", "2026-01-01 00:00:00"], + ["r2", "project.delete", "2026-01-01 00:00:00"], + ["r3", "user.delete", "2026-01-02 00:00:00"], + ["r4", "user.delete", "2026-01-02 00:00:00"], + ["r5", "oauth_client.create", "2026-01-02 00:00:00"], + ["r6", "user.delete", "2026-01-03 00:00:00"], + ["r7", "project.delete", "2026-01-04 00:00:00"], + ]; + for (const r of rows) ins.run(...r); + return sqlite; + } + + function pageAll(sqlite: ReturnType, actions: string[], pageSize: number): string[] { + const seen: string[] = []; + let cursor: { ts: string; id: string } | null = null; + for (;;) { + const { sql, binds } = buildAuditListQuery(cursor, { actions, q: null }); + const rows = sqlite.prepare(sql).all(...(binds as string[]), pageSize) as Array<{ id: string; created_at: string }>; + if (rows.length === 0) return seen; + seen.push(...rows.map(r => r.id)); + const last = rows[rows.length - 1]; + cursor = { ts: last.created_at, id: last.id }; + } + } + + it("pages a single-action filter newest-first without skips or repeats", () => { + expect(pageAll(seed(), ["user.delete"], 2)).toEqual(["r6", "r4", "r3", "r1"]); + }); + + it("pages a multi-action filter newest-first without skips or repeats", () => { + expect(pageAll(seed(), ["project.delete", "oauth_client.create"], 2)).toEqual(["r7", "r5", "r2"]); + }); + + it("pages the unfiltered log newest-first", () => { + expect(pageAll(seed(), [], 3)).toEqual(["r7", "r6", "r5", "r4", "r3", "r2", "r1"]); + }); + + it("lists distinct actions in ascending order", () => { + const rows = seed().prepare(AUDIT_ACTIONS_SQL).all(); + expect(rows.map(r => r.action)).toEqual(["oauth_client.create", "project.delete", "user.delete"]); + }); + + it("lists no actions for an empty log", () => { + const { sqlite } = createSqliteD1(AUTH_MIGRATIONS); + expect(sqlite.prepare(AUDIT_ACTIONS_SQL).all()).toEqual([]); + }); +}); diff --git a/packages/admin/src/routes/audit.ts b/packages/admin/src/routes/audit.ts index 99da605f..6148c42b 100644 --- a/packages/admin/src/routes/audit.ts +++ b/packages/admin/src/routes/audit.ts @@ -45,7 +45,13 @@ export function buildAuditListQuery( where.push(keyset.sql); binds.push(...keyset.binds); } - if (filters.actions.length > 0) { + // A single action is an equality so it seeks the (action, created_at, id) + // index straight to the cursor position. Several stay one IN(...): exact, + // cursor-safe, and bounded by how many rows match the chosen actions. + if (filters.actions.length === 1) { + where.push("action = ?"); + binds.push(filters.actions[0]); + } else if (filters.actions.length > 1) { const placeholders = filters.actions.map(() => "?").join(", "); where.push(`action IN (${placeholders})`); binds.push(...filters.actions); @@ -65,6 +71,19 @@ export function buildAuditListQuery( return { sql, binds }; } +// Distinct actions, ascending, as a skip-scan over the action index +// (auth 0035): each step seeks to the next action greater than the last, so +// the cost is one index probe per distinct action instead of reading the +// whole ever-growing log as SELECT DISTINCT does. `action` is NOT NULL, so a +// NULL from MIN() only ever means "no further actions". +export const AUDIT_ACTIONS_SQL = `WITH RECURSIVE a(action) AS ( + SELECT MIN(action) FROM admin_audit_log + UNION ALL + SELECT (SELECT MIN(action) FROM admin_audit_log WHERE action > a.action) + FROM a WHERE a.action IS NOT NULL + ) + SELECT action FROM a WHERE action IS NOT NULL`; + const auditRouter = new Hono(); // GET /api/audit?cursor= @@ -110,13 +129,11 @@ auditRouter.get("/", async (c) => { // GET /api/audit/actions // // Distinct action values present in the log, sorted, for populating the -// filter dropdown. Cheap (small, slow-growing table) and re-derived on -// each load so newly introduced action types appear without code changes. +// filter dropdown. Re-derived on each load so newly introduced action types +// appear without code changes. auditRouter.get("/actions", async (c) => { const rows = ( - await c.env.AUTH_DB.prepare( - `SELECT DISTINCT action FROM admin_audit_log ORDER BY action ASC`, - ).all<{ action: string }>() + await c.env.AUTH_DB.prepare(AUDIT_ACTIONS_SQL).all<{ action: string }>() ).results; return c.json({ ok: true, data: { actions: rows.map((r) => r.action) } }); }); From e9976c61b23788361f54e927f8627f89b3cc7cb2 Mon Sep 17 00:00:00 2001 From: Michael Burr Date: Sun, 1 Nov 2026 22:05:45 +0000 Subject: [PATCH 2/3] perf(admin): index-friendly user search and one-read user details Add exact id/email fast paths (falling back to the substring search on a miss), let the disabled/suspended filters use the partial moderated-users index, drive the pending-deletion filter from user_deletions, collapse the three latest-moderation subqueries into one, and read the users row with its preference and billing satellites in a single query. --- packages/admin/src/routes/users.test.ts | 181 ++++++++++++++++- packages/admin/src/routes/users.ts | 257 +++++++++++++++--------- 2 files changed, 339 insertions(+), 99 deletions(-) diff --git a/packages/admin/src/routes/users.test.ts b/packages/admin/src/routes/users.test.ts index 6dd0a24c..2f5c1a40 100644 --- a/packages/admin/src/routes/users.test.ts +++ b/packages/admin/src/routes/users.test.ts @@ -13,6 +13,8 @@ import { getCurrentStatus, buildBillingDetails, buildUserStatusClause, + buildUserSearchQuery, + exactUserMatch, type BillingRow, } from "./users"; import { writeAdminAudit } from "../audit"; @@ -573,14 +575,187 @@ describe("PATCH /:id - pending deletion owns moderation", () => { }); describe("GET /search - pending deletion", () => { - it("joins user_deletions and supports the pending_deletion filter", async () => { + it("drives the pending_deletion filter from user_deletions", async () => { const env = makeEnv(); env.AUTH_DB.allQueue.push([]); const res = await makeUsersApp(env)("/search?q=&status=pending_deletion"); expect(res.status).toBe(200); - const call = env.AUTH_DB.calls.find(c => c.sql.includes("FROM users")); - expect(call?.sql).toContain("LEFT JOIN user_deletions d"); + const call = env.AUTH_DB.calls.find(c => c.sql.includes("FROM user_deletions d CROSS JOIN users u")); expect(call?.sql).toContain("d.user_id IS NOT NULL"); expect(call?.sql).toContain("deletion_execute_after"); }); + + it("left-joins user_deletions for every other status", async () => { + const env = makeEnv(); + env.AUTH_DB.allQueue.push([]); + const res = await makeUsersApp(env)("/search?q=&status=disabled"); + expect(res.status).toBe(200); + const call = env.AUTH_DB.calls.find(c => c.sql.includes("FROM users u LEFT JOIN user_deletions d")); + expect(call?.sql).toContain("deletion_execute_after"); + }); +}); + +describe("exactUserMatch", () => { + it("classifies UUIDs, full emails, and everything else", () => { + expect(exactUserMatch("3f2b8c4e-1a2b-4c3d-9e8f-0123456789ab")).toBe("id"); + expect(exactUserMatch(" 3F2B8C4E-1A2B-4C3D-9E8F-0123456789AB ")).toBe("id"); + expect(exactUserMatch("bob@example.com")).toBe("email"); + expect(exactUserMatch(" Bob@Example.com ")).toBe("email"); + expect(exactUserMatch("bob@")).toBe("substring"); + expect(exactUserMatch("example.com")).toBe("substring"); + expect(exactUserMatch("bob @example.com")).toBe("substring"); + expect(exactUserMatch("")).toBe("substring"); + }); +}); + +// Real SQLite (every auth migration applied) for the search + details reads: +// result sets, paging, the exact-match fallback, and the query plans that are +// the point of the exact/partial-index/pending-deletion paths. +describe("user search + details against the real schema", () => { + const NOW = Math.floor(Date.now() / 1000); + const U1 = "11111111-1111-4111-8111-111111111111"; + + function setup() { + const auth = createSqliteD1(AUTH_MIGRATIONS); + const env = { ...makeEnv(), AUTH_DB: auth.db }; + const addUser = (id: string, email: string, createdAt: string, moderation = 0) => + auth.sqlite.prepare("INSERT INTO users (id, email, name, password_hash, created_at, moderation) VALUES (?, ?, ?, 'x', ?, ?)") + .run(id, email, id, createdAt, moderation); + const addEvent = (id: string, userId: string, action: string, reason: string | null, createdAt: string) => + auth.sqlite.prepare("INSERT INTO user_moderation_events (id, user_id, action, moderation_value, reason, created_at) VALUES (?, ?, ?, -1, ?, ?)") + .run(id, userId, action, reason, createdAt); + addUser(U1, "alice@example.com", "2026-01-01 00:00:00"); + addUser("u-2", "bob@example.com", "2026-01-02 00:00:00", -1); + addUser("u-3", "xbob@example.com", "2026-01-02 00:00:00", NOW + 3600); + addUser("u-4", "carol@example.com", "2026-01-03 00:00:00", NOW - 3600); + addUser("u-5", "dave@example.com", "2026-01-04 00:00:00", -1); + auth.sqlite.prepare("INSERT INTO user_deletions (user_id, id, requested_by, requested_by_email, execute_after) VALUES ('u-5', 'del-1', 'admin-1', 'admin@example.com', ?)") + .run(NOW + 86400); + // Two events on one created_at: the later-inserted (higher rowid) wins. + addEvent("e-1", "u-2", "suspended", "first", "2026-02-01 00:00:00"); + addEvent("e-2", "u-2", "disabled", "spam", "2026-02-02 00:00:00"); + addEvent("e-3", "u-2", "re_enabled", null, "2026-01-15 00:00:00"); + addEvent("e-4", "u-5", "re_enabled", null, "2026-02-01 00:00:00"); + addEvent("e-5", "u-5", "disabled", null, "2026-02-01 00:00:00"); + return { auth, env, request: makeUsersApp(env) }; + } + + async function search(request: ReturnType, query: string) { + const res = await request(`/search?${query}`); + expect(res.status).toBe(200); + const body = await res.json() as { data: { users: Array>; nextCursor: string | null } }; + return body.data; + } + const ids = (data: { users: Array> }) => data.users.map(u => u.id); + + it("filters by status, newest first", async () => { + const { request } = setup(); + expect(ids(await search(request, "q="))).toEqual(["u-5", "u-4", "u-3", "u-2", U1]); + expect(ids(await search(request, "q=&status=disabled"))).toEqual(["u-5", "u-2"]); + expect(ids(await search(request, "q=&status=suspended"))).toEqual(["u-3"]); + expect(ids(await search(request, "q=&status=active"))).toEqual(["u-4", U1]); + const pending = await search(request, "q=&status=pending_deletion"); + expect(ids(pending)).toEqual(["u-5"]); + expect(pending.users[0]).toMatchObject({ deletion_requested_by_email: "admin@example.com", deletion_execute_after: NOW + 86400 }); + }); + + it("unpacks the latest moderation event (created_at, then rowid, newest wins)", async () => { + const { request } = setup(); + const { users } = await search(request, "q="); + const byId = Object.fromEntries(users.map(u => [u.id, u])); + expect(byId["u-2"]).toMatchObject({ + latest_moderation_action: "disabled", + latest_moderation_reason: "spam", + latest_moderation_created_at: "2026-02-02 00:00:00", + }); + expect(byId["u-5"]).toMatchObject({ latest_moderation_action: "disabled", latest_moderation_reason: null }); + expect(byId[U1]).toMatchObject({ + latest_moderation_action: null, + latest_moderation_reason: null, + latest_moderation_created_at: null, + }); + expect(byId[U1]).not.toHaveProperty("latest_moderation"); + }); + + it("matches a full email or UUID exactly, and falls back to substring on a miss", async () => { + const { request } = setup(); + // Exact: the substring search would also have returned xbob@example.com. + expect(ids(await search(request, "q=bob%40example.com"))).toEqual(["u-2"]); + expect(ids(await search(request, `q=${U1}`))).toEqual([U1]); + // Differently-cased input still hits the normalized address. + expect(ids(await search(request, "q=BOB%40Example.com"))).toEqual(["u-2"]); + // A full-looking address that isn't one falls back to the substring search. + expect(ids(await search(request, "q=bob%40example.co"))).toEqual(["u-3", "u-2"]); + // An exact hit excluded by the status filter falls back too. + expect(ids(await search(request, "q=xbob%40example.com&status=disabled"))).toEqual([]); + // Plain substring search. + expect(ids(await search(request, "q=bob"))).toEqual(["u-3", "u-2"]); + }); + + it("pages with the keyset cursor without skips or repeats across tied timestamps", async () => { + const { auth, request } = setup(); + for (let i = 0; i < 30; i++) { + auth.sqlite.prepare("INSERT INTO users (id, email, name, password_hash, created_at) VALUES (?, ?, 'n', 'x', '2026-03-01 00:00:00')") + .run(`t-${String(i).padStart(2, "0")}`, `t${i}@example.com`); + } + const seen: unknown[] = []; + let cursor: string | null = null; + do { + const page = await search(request, `q=${cursor ? `&cursor=${cursor}` : ""}`); + seen.push(...ids(page)); + cursor = page.nextCursor; + } while (cursor); + expect(seen).toHaveLength(35); + expect(new Set(seen).size).toBe(35); + expect(seen.slice(0, 2)).toEqual(["t-29", "t-28"]); + expect(seen.slice(-5)).toEqual(["u-5", "u-4", "u-3", "u-2", U1]); + }); + + function plan(auth: ReturnType, opts: Parameters[0]): string { + const { sql, binds } = buildUserSearchQuery(opts); + return auth.sqlite.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...(binds as never[]), 26).map(r => String(r.detail)).join("\n"); + } + + it("plans exact lookups as unique-index seeks and pending deletion from user_deletions", () => { + const { auth } = setup(); + const base = { status: null, cursor: null, nowSeconds: NOW }; + expect(plan(auth, { ...base, q: U1, match: "id" })).toMatch(/SEARCH u USING INDEX sqlite_autoindex_users_1 \(id=\?\)/); + expect(plan(auth, { ...base, q: "bob@example.com", match: "email" })).toMatch(/SEARCH u USING INDEX sqlite_autoindex_users_2 \(email=\?\)/); + const pending = plan(auth, { ...base, q: "", match: "substring", status: "pending_deletion" }); + expect(pending.split("\n")[0]).toMatch(/^(SCAN|SEARCH) d\b/); + expect(pending).toMatch(/SEARCH u USING INDEX sqlite_autoindex_users_1 \(id=\?\)/); + }); + + it("uses the partial moderated-users index for disabled/suspended when it exists (auth 0035)", () => { + const { auth } = setup(); + auth.sqlite.exec("CREATE INDEX IF NOT EXISTS users_moderated_created_at ON users(created_at DESC, id DESC) WHERE moderation <> 0"); + const base = { q: "", match: "substring" as const, cursor: { ts: "2026-01-03 00:00:00", id: "u-4" }, nowSeconds: NOW }; + expect(plan(auth, { ...base, status: "disabled" })).toContain("USING INDEX users_moderated_created_at"); + expect(plan(auth, { ...base, status: "suspended" })).toContain("USING INDEX users_moderated_created_at"); + }); + + it("GET /:id reads profile, TOTP, preferences and billing in one users query", async () => { + const { auth, env, request } = setup(); + auth.sqlite.prepare("UPDATE users SET totp_secret = 'S', email_verified = 1 WHERE id = 'u-2'").run(); + auth.sqlite.prepare("INSERT INTO user_preferences (user_id, badges) VALUES ('u-2', 3)").run(); + auth.sqlite.prepare("INSERT INTO user_billing (user_id, stripe_customer_id, granted_plan, granted_plan_reason, granted_plan_started_at) VALUES ('u-2', 'cus_1', 'ink', 'comp', ?)") + .run(Date.now() - 1000); + const prepare = vi.spyOn(env.AUTH_DB, "prepare"); + const res = await request("/u-2"); + expect(res.status).toBe(200); + const { data } = await res.json() as { data: Record }; + expect(prepare.mock.calls.filter(([sql]) => /FROM users\b/.test(sql))).toHaveLength(1); + expect(data.profile).toMatchObject({ id: "u-2", account_status: "disabled", email_verified: true, badges: 3 }); + expect(data.moderation.current_reason).toBe("spam"); + expect(data.moderation.history.map((e: { action: string }) => e.action)).toEqual(["disabled", "suspended", "re_enabled"]); + expect(data.security.totp_enabled).toBe(true); + expect(data.billing).toMatchObject({ resolved_plan: "ink", via: "granted", stripe: { customer_id: "cus_1" } }); + + const plain = await (await request(`/${U1}`)).json() as { data: Record }; + expect(plain.data.security.totp_enabled).toBe(false); + expect(plain.data.moderation.current_reason).toBeNull(); + expect(plain.data.billing).toMatchObject({ resolved_plan: "free", granted: null }); + + expect((await request("/nope")).status).toBe(404); + }); }); diff --git a/packages/admin/src/routes/users.ts b/packages/admin/src/routes/users.ts index 26d3cd9a..0c074495 100644 --- a/packages/admin/src/routes/users.ts +++ b/packages/admin/src/routes/users.ts @@ -137,7 +137,8 @@ export function getCurrentStatus(moderation: number): CurrentStatus { // Builds the optional account-status WHERE fragment for the user search. // `status` accepts exactly "active" | "disabled" | "suspended" | -// "pending_deletion" (needs the search's `user_deletions d` join); anything +// "pending_deletion" (needs the search's `user_deletions d` join, which +// buildUserSearchQuery makes the driving table for that status); anything // else (absent, empty, unknown) yields an empty clause => no status filter // (all accounts). Pure so it can be unit-tested in isolation, mirroring // buildAuditListQuery. `binds` are positional, in SQL order, to be spliced @@ -149,10 +150,13 @@ export function buildUserStatusClause( switch (status) { case "active": return { sql: "(u.moderation = 0 OR (u.moderation > 0 AND u.moderation <= ?))", binds: [nowSeconds] }; + // The redundant `u.moderation <> 0` lets SQLite use the partial + // users(created_at DESC, id DESC) WHERE moderation <> 0 index (auth 0035): + // a partial index only qualifies when the query repeats its WHERE term. case "disabled": - return { sql: "u.moderation = -1", binds: [] }; + return { sql: "u.moderation = -1 AND u.moderation <> 0", binds: [] }; case "suspended": - return { sql: "u.moderation > ?", binds: [nowSeconds] }; + return { sql: "u.moderation > ? AND u.moderation <> 0", binds: [nowSeconds] }; case "pending_deletion": return { sql: "d.user_id IS NOT NULL", binds: [] }; default: @@ -160,12 +164,109 @@ export function buildUserStatusClause( } } -function latestModerationFields(tableAlias = "u"): string { - return ` - (SELECT action FROM user_moderation_events e WHERE e.user_id = ${tableAlias}.id ORDER BY e.created_at DESC, rowid DESC LIMIT 1) AS latest_moderation_action, - (SELECT reason FROM user_moderation_events e WHERE e.user_id = ${tableAlias}.id ORDER BY e.created_at DESC, rowid DESC LIMIT 1) AS latest_moderation_reason, - (SELECT created_at FROM user_moderation_events e WHERE e.user_id = ${tableAlias}.id ORDER BY e.created_at DESC, rowid DESC LIMIT 1) AS latest_moderation_created_at - `; +// The user's latest moderation event as one correlated lookup returning +// json_array(action, reason, created_at) (NULL when there's none), instead of +// three identical subqueries; withLatestModeration unpacks it. +const LATEST_MODERATION_SQL = `( + SELECT json_array(e.action, e.reason, e.created_at) FROM user_moderation_events e + WHERE e.user_id = u.id ORDER BY e.created_at DESC, e.rowid DESC LIMIT 1 + ) AS latest_moderation`; + +export type UserSearchRaw = Omit< + UserRow, + "latest_moderation_action" | "latest_moderation_reason" | "latest_moderation_created_at" +> & { latest_moderation: string | null }; + +export function withLatestModeration(row: UserSearchRaw): UserRow { + const { latest_moderation, ...rest } = row; + const [action, reason, createdAt] = latest_moderation + ? (JSON.parse(latest_moderation) as [ModerationAction, string | null, string]) + : [null, null, null]; + return { + ...rest, + latest_moderation_action: action, + latest_moderation_reason: reason, + latest_moderation_created_at: createdAt, + }; +} + +const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; +const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + +// How the search text is matched. A UUID-shaped query is looked up by id and +// a full email address by email (both unique-index seeks); anything else is +// the email substring search, which has to scan. The handler falls back to +// "substring" when an exact lookup finds nothing, so a partial or +// differently-cased address still finds what the substring search would. +export type UserSearchMatch = "id" | "email" | "substring"; + +export function exactUserMatch(q: string): UserSearchMatch { + const t = q.trim(); + if (UUID_RE.test(t)) return "id"; + if (EMAIL_RE.test(t)) return "email"; + return "substring"; +} + +// The user-search list query. Search, status, and keyset cursor are AND-ed +// WHERE clauses; binds are positional in SQL order, excluding the trailing +// LIMIT bind (the caller appends it). Pure so it can run against real SQLite +// in tests. +export function buildUserSearchQuery(opts: { + q: string; + match: UserSearchMatch; + status: string | null; + cursor: KeysetCursor | null; + nowSeconds: number; +}): { sql: string; binds: unknown[] } { + const where: string[] = []; + const binds: unknown[] = []; + if (opts.match === "id") { + where.push("u.id = ?"); + binds.push(opts.q.trim()); + } else if (opts.match === "email") { + // Stored addresses are normalized (trimmed, lowercased) by the auth worker. + where.push("u.email = ?"); + binds.push(opts.q.trim().toLowerCase()); + } else if (opts.q) { + where.push(`(u.email LIKE ? ${LIKE_ESCAPE_CLAUSE} OR u.id = ?)`); + binds.push(`%${escapeLike(opts.q)}%`, opts.q); + } + const statusClause = buildUserStatusClause(opts.status, opts.nowSeconds); + if (statusClause.sql) { + where.push(statusClause.sql); + binds.push(...statusClause.binds); + } + const keyset = keysetClause(opts.cursor, "u.created_at", "u.id"); + if (keyset.sql) { + where.push(keyset.sql); + binds.push(...keyset.binds); + } + // Pending deletions are few, so that filter is driven from user_deletions + // (CROSS JOIN pins it as the outer loop) and sorted, instead of walking + // every user in created_at order looking for the scheduled ones. + const from = opts.status === "pending_deletion" + ? "FROM user_deletions d CROSS JOIN users u ON u.id = d.user_id" + : "FROM users u LEFT JOIN user_deletions d ON d.user_id = u.id"; + const sql = ` + SELECT + u.id, + u.email, + u.name, + u.created_at, + u.moderation, + u.force_password_change, + u.email_verified, + d.execute_after AS deletion_execute_after, + d.requested_by_email AS deletion_requested_by_email, + d.last_error AS deletion_error, + d.started_at AS deletion_started_at, + ${LATEST_MODERATION_SQL} + ${from} + ${where.length ? `WHERE ${where.join(" AND ")}` : ""} + ORDER BY u.created_at DESC, u.id DESC + LIMIT ? + `; + return { sql, binds }; } export interface BillingRow { @@ -213,44 +314,37 @@ export function buildBillingDetails(row: BillingRow): BillingDetails { }; } -async function loadUserDetails(env: Env, id: string): Promise { - const profile = await env.AUTH_DB.prepare( - ` - SELECT - u.id, - u.email, - u.name, - u.created_at, - u.moderation, - u.force_password_change, - u.email_verified, - p.badges, - ${latestModerationFields("u")} - FROM users u - LEFT JOIN user_preferences p ON p.user_id = u.id - WHERE u.id = ? - `, - ).bind(id).first(); - - if (!profile) return null; - - const billingRow = await env.AUTH_DB.prepare( - `SELECT p.personal_plan_style, p.personal_presence_color, p.personal_crit_sparkles, - b.stripe_customer_id, b.stripe_subscription_id, - b.personal_plan, b.personal_plan_status, b.personal_plan_started_at, - b.personal_plan_cancel_at, - b.granted_plan, b.granted_plan_expires_at, b.granted_plan_started_at, - b.granted_plan_reason - FROM users u - LEFT JOIN user_billing b ON b.user_id = u.id - LEFT JOIN user_preferences p ON p.user_id = u.id - WHERE u.id = ?`, - ).bind(id).first(); +type ProfileRow = BillingRow & { + id: string; + email: string; + name: string; + created_at: string; + moderation: number; + force_password_change: number; + email_verified: number; + has_totp: number; + badges: number | null; +}; - const [totpRow, webauthn, backupCodeSummary, moderationHistory, ownedProjects, memberships] = await Promise.all([ +async function loadUserDetails(env: Env, id: string): Promise { + // The users row (profile + TOTP flag) and its preferences/billing + // satellites in one read, in parallel with everything else; for an + // unknown id the other lookups just come back empty. + const [profile, webauthn, backupCodeSummary, moderationHistory, ownedProjects, memberships] = await Promise.all([ env.AUTH_DB.prepare( - "SELECT totp_secret IS NOT NULL AS has_totp FROM users WHERE id = ?", - ).bind(id).first<{ has_totp: number }>(), + `SELECT u.id, u.email, u.name, u.created_at, u.moderation, u.force_password_change, + u.email_verified, u.totp_secret IS NOT NULL AS has_totp, + p.badges, p.personal_plan_style, p.personal_presence_color, p.personal_crit_sparkles, + b.stripe_customer_id, b.stripe_subscription_id, + b.personal_plan, b.personal_plan_status, b.personal_plan_started_at, + b.personal_plan_cancel_at, + b.granted_plan, b.granted_plan_expires_at, b.granted_plan_started_at, + b.granted_plan_reason + FROM users u + LEFT JOIN user_preferences p ON p.user_id = u.id + LEFT JOIN user_billing b ON b.user_id = u.id + WHERE u.id = ?`, + ).bind(id).first(), env.AUTH_DB.prepare( "SELECT id, name, created_at FROM webauthn_credentials WHERE user_id = ? ORDER BY created_at DESC", ).bind(id).all<{ id: string; name: string; created_at: string }>(), @@ -286,8 +380,13 @@ async function loadUserDetails(env: Env, id: string): Promise(), ]); + if (!profile) return null; + const currentStatus = getCurrentStatus(profile.moderation); - const currentReason = currentStatus === "active" ? null : profile.latest_moderation_reason; + // The history is newest-first in the same (created_at DESC, rowid DESC) + // order as the user list's latest-moderation lookup, so its head is the + // latest event. + const currentReason = currentStatus === "active" ? null : (moderationHistory.results[0]?.reason ?? null); return { profile: { @@ -315,7 +414,7 @@ async function loadUserDetails(env: Env, id: string): Promise ({ id: k.id, name: k.name, registered_at: k.created_at })), backup_codes: { total: backupCodeSummary?.total ?? 0, @@ -327,12 +426,7 @@ async function loadUserDetails(env: Env, id: string): Promise ({ id: p.id, name: p.name, created_at: p.created_at })), project_memberships: memberships.results.map(m => ({ project_id: m.id, project_name: m.name, role: m.role, joined_at: m.created_at })), }, - billing: buildBillingDetails(billingRow ?? { - stripe_customer_id: null, stripe_subscription_id: null, - personal_plan: null, personal_plan_status: null, personal_plan_started_at: null, personal_plan_cancel_at: null, - personal_plan_style: null, personal_presence_color: null, personal_crit_sparkles: null, - granted_plan: null, granted_plan_expires_at: null, granted_plan_started_at: null, granted_plan_reason: null, - }), + billing: buildBillingDetails(profile), }; } @@ -340,7 +434,7 @@ async function loadUserDetails(env: Env, id: string): Promise { const q = c.req.query("q") ?? ""; const nowSeconds = Math.floor(Date.now() / 1000); - const statusClause = buildUserStatusClause(c.req.query("status") ?? null, nowSeconds); + const status = c.req.query("status") ?? null; const rawCursor = c.req.query("cursor"); let cursor: KeysetCursor | null = null; @@ -348,50 +442,21 @@ usersRouter.get("/search", async (c) => { cursor = decodeCursor(rawCursor); if (!cursor) return c.json({ ok: false, error: "Invalid cursor" }, 400); } - const keyset = keysetClause(cursor, "u.created_at", "u.id"); - - // The email/id search is always present; the optional status predicate and - // the keyset cursor are AND-ed onto it. Bind order follows clause order: - // the two search binds, then status binds, then the keyset binds. - const where = [`(u.email LIKE ? ${LIKE_ESCAPE_CLAUSE} OR u.id = ?)`]; - const binds: unknown[] = [`%${escapeLike(q)}%`, q]; - if (statusClause.sql) { - where.push(statusClause.sql); - binds.push(...statusClause.binds); - } - if (keyset.sql) { - where.push(keyset.sql); - binds.push(...keyset.binds); - } // One extra row tells us whether an older page exists without a count. - const rows = await c.env.AUTH_DB.prepare( - ` - SELECT - u.id, - u.email, - u.name, - u.created_at, - u.moderation, - u.force_password_change, - u.email_verified, - d.execute_after AS deletion_execute_after, - d.requested_by_email AS deletion_requested_by_email, - d.last_error AS deletion_error, - d.started_at AS deletion_started_at, - ${latestModerationFields("u")} - FROM users u - LEFT JOIN user_deletions d ON d.user_id = u.id - WHERE ${where.join(" AND ")} - ORDER BY u.created_at DESC, u.id DESC - LIMIT ? - `, - ) - .bind(...binds, USER_PAGE_SIZE + 1) - .all(); - - const hasMore = rows.results.length > USER_PAGE_SIZE; - const users = hasMore ? rows.results.slice(0, USER_PAGE_SIZE) : rows.results; + const run = async (match: UserSearchMatch) => { + const { sql, binds } = buildUserSearchQuery({ q, match, status, cursor, nowSeconds }); + return (await c.env.AUTH_DB.prepare(sql).bind(...binds, USER_PAGE_SIZE + 1).all()).results; + }; + const match = exactUserMatch(q); + let results = await run(match); + // An exact lookup that misses falls back to the substring search. Every + // request in a paging sequence takes the same path: an exact hit is at + // most one row (so it never yields a cursor), and a miss stays a miss. + if (results.length === 0 && match !== "substring") results = await run("substring"); + + const hasMore = results.length > USER_PAGE_SIZE; + const users = (hasMore ? results.slice(0, USER_PAGE_SIZE) : results).map(withLatestModeration); const last = users[users.length - 1]; const nextCursor = hasMore && last ? encodeCursor({ ts: last.created_at, id: last.id }) : null; From 3eae0c2f9a10c734971271f2fb59c9e11ba75f4a Mon Sep 17 00:00:00 2001 From: Michael Burr Date: Sun, 1 Nov 2026 22:05:45 +0000 Subject: [PATCH 3/3] fix(admin): drop redundant share deletes and clean up deleted users' rows Site delete: doc_shares already cascade from docs, so the chunked delete is gone; docs are deleted explicitly ahead of the project so foldered docs aren't SET NULL-updated and then deleted. Scheduled account deletion now also removes the user's doc_shares, user_favourite_projects and email_verification_tokens rows, none of which have a foreign key to cascade from. --- .../src/lib/projectDelete.sqlite.test.ts | 65 +++++++++++++++++++ packages/admin/src/lib/projectDelete.ts | 23 ++++--- .../admin/src/lib/userDeletion.sqlite.test.ts | 54 +++++++++++++++ packages/admin/src/lib/userDeletion.test.ts | 5 +- packages/admin/src/lib/userDeletion.ts | 8 ++- packages/admin/src/routes/projects.test.ts | 29 ++++----- 6 files changed, 158 insertions(+), 26 deletions(-) create mode 100644 packages/admin/src/lib/projectDelete.sqlite.test.ts diff --git a/packages/admin/src/lib/projectDelete.sqlite.test.ts b/packages/admin/src/lib/projectDelete.sqlite.test.ts new file mode 100644 index 00000000..fc5934cc --- /dev/null +++ b/packages/admin/src/lib/projectDelete.sqlite.test.ts @@ -0,0 +1,65 @@ +// Site deletion against REAL SQLite (every API migration applied, foreign +// keys on as in D1): the cascades are what remove most dependent rows, and a +// queue-based mock can't tell whether they actually fire. +import { describe, it, expect, vi } from "vitest"; +import { createSqliteD1 } from "../../../api/src/test/sqliteD1"; +import { deleteProjectCascade } from "./projectDelete"; + +function setup() { + const api = createSqliteD1(); + const s = api.sqlite; + const now = "2026-01-01 00:00:00"; + for (const p of ["p-1", "p-2"]) { + s.prepare("INSERT INTO projects (id, name, owner_id, created_at) VALUES (?, ?, 'u-1', ?)").run(p, p, now); + s.prepare("INSERT INTO folders (id, name, project_id, created_at) VALUES (?, 'F', ?, ?)").run(`${p}-f`, p, now); + s.prepare("INSERT INTO folders (id, name, project_id, parent_id, created_at) VALUES (?, 'Sub', ?, ?, ?)").run(`${p}-sub`, p, `${p}-f`, now); + // A root doc, a foldered doc, and one in a nested folder. + for (const [doc, folder] of [[`${p}-d1`, null], [`${p}-d2`, `${p}-f`], [`${p}-d3`, `${p}-sub`]] as const) { + s.prepare("INSERT INTO docs (id, title, project_id, folder_id, author_id, created_at, updated_at) VALUES (?, 'T', ?, ?, 'u-1', ?, ?)") + .run(doc, p, folder, now, now); + s.prepare("INSERT INTO docs_fts (title, body, doc_id, project_id) VALUES ('T', 'body', ?, ?)").run(doc, p); + s.prepare("INSERT INTO doc_shares (id, doc_id, user_id, project_id, granted_by, created_at) VALUES (?, ?, 'u-2', ?, 'u-1', ?)") + .run(`${doc}-share`, doc, p, now); + s.prepare("INSERT INTO doc_ai_summaries (doc_id, summary, version) VALUES (?, 'S', 1)").run(doc); + s.prepare("INSERT INTO asset_revisions (id, asset_type, asset_id, project_id, editor_id, editor_name, created_at) VALUES (?, 'doc', ?, ?, 'u-1', 'U', ?)") + .run(`${doc}-rev`, doc, p, now); + } + s.prepare("INSERT INTO doc_links (source_doc_id, target_doc_id, project_id) VALUES (?, ?, ?)").run(`${p}-d1`, `${p}-d2`, p); + s.prepare("INSERT INTO files (id, name, mime_type, size, project_id, folder_id, uploaded_by, created_at) VALUES (?, 'f.txt', 'text/plain', 1, ?, ?, 'u-1', ?)") + .run(`${p}-file`, p, `${p}-f`, now); + s.prepare("INSERT INTO site_reports (id, project_id, doc_id, note) VALUES (?, ?, ?, 'n')").run(`${p}-report`, p, `${p}-d2`); + } + const assets = { + list: vi.fn(async () => ({ objects: [], truncated: false })), + delete: vi.fn(async () => undefined), + }; + const env = { DB: api.db, ASSETS: assets }; + const count = (table: string, where = "1") => + (s.prepare(`SELECT COUNT(*) AS n FROM ${table} WHERE ${where}`).get() as { n: number }).n; + return { env, count }; +} + +describe("deleteProjectCascade (real SQLite)", () => { + it("removes the site and every dependent row, and leaves other sites alone", async () => { + const { env, count } = setup(); + expect(await deleteProjectCascade(env as never, "p-1")).toEqual({ docs: 3, files: 1 }); + + for (const [table, col] of [ + ["projects", "id"], ["docs", "project_id"], ["folders", "project_id"], ["files", "project_id"], + ["doc_shares", "project_id"], ["doc_links", "project_id"], ["docs_fts", "project_id"], + ["asset_revisions", "project_id"], ["site_reports", "project_id"], + ]) { + expect(count(table, `${col} = 'p-1'`), table).toBe(0); + } + expect(count("doc_ai_summaries", "doc_id LIKE 'p-1-%'")).toBe(0); + + // p-2 is untouched. + expect(count("docs", "project_id = 'p-2'")).toBe(3); + expect(count("doc_shares", "project_id = 'p-2'")).toBe(3); + expect(count("docs_fts", "project_id = 'p-2'")).toBe(3); + expect(count("asset_revisions", "project_id = 'p-2'")).toBe(3); + expect(count("doc_ai_summaries", "doc_id LIKE 'p-2-%'")).toBe(3); + expect(count("folders", "project_id = 'p-2'")).toBe(2); + expect(count("site_reports", "project_id = 'p-2' AND doc_id = 'p-2-d2'")).toBe(1); + }); +}); diff --git a/packages/admin/src/lib/projectDelete.ts b/packages/admin/src/lib/projectDelete.ts index 7d2787fe..99d744e3 100644 --- a/packages/admin/src/lib/projectDelete.ts +++ b/packages/admin/src/lib/projectDelete.ts @@ -14,9 +14,9 @@ import type { Env } from "../index"; // prefix, so one batched prefix sweep removes them all (including objects // orphaned by failed inserts) in ~1 subrequest per 1000 keys, and the // per-doc-id SQL deletes are chunked to stay under D1's ~100 bound- -// parameter limit. The final batch (satellite rows + FTS + the project -// row, which cascades docs/files/members/domains) is a single D1 batch -// so the row deletes can't half-apply. +// parameter limit. The final batch (FTS rows, docs, and the project row, +// which cascades files/folders/members/domains) is a single D1 batch so +// the row deletes can't half-apply. // // The caller owns the existence check and the audit row. export async function deleteProjectCascade( @@ -47,16 +47,15 @@ export async function deleteProjectCascade( await env.ASSETS.delete(otherKeys.slice(i, i + 1000)); } - // asset_revisions and doc_shares have no ON DELETE CASCADE from docs; - // delete them in 50-id chunks so a large project never exceeds D1's + // asset_revisions has no foreign key to docs, so it doesn't cascade; + // delete it in 50-id chunks so a large project never exceeds D1's // bound-parameter limit (an unchunked IN(...) 500s past ~100 docs). + // (doc_shares, doc_links and doc_ai_summaries do cascade from docs.) for (let i = 0; i < docIds.length; i += 50) { const chunk = docIds.slice(i, i + 50); const ph = chunk.map(() => "?").join(","); - await env.DB.batch([ - env.DB.prepare(`DELETE FROM asset_revisions WHERE asset_type = 'doc' AND asset_id IN (${ph})`).bind(...chunk), - env.DB.prepare(`DELETE FROM doc_shares WHERE doc_id IN (${ph})`).bind(...chunk), - ]); + await env.DB.prepare(`DELETE FROM asset_revisions WHERE asset_type = 'doc' AND asset_id IN (${ph})`) + .bind(...chunk).run(); } // Release the Cloudflare custom hostname (if any) before the batch deletes @@ -64,8 +63,14 @@ export async function deleteProjectCascade( // when CF isn't configured on the admin worker. await releaseCustomDomain(env, projectId); + // Docs go explicitly before the project: left to the project's cascade, + // the folders cascade would first SET NULL docs.folder_id on every + // foldered doc (a row update each) only for the docs cascade to delete + // them. docs_fts is a standalone FTS table (no triggers), so its order + // relative to the docs delete doesn't matter. await env.DB.batch([ env.DB.prepare("DELETE FROM docs_fts WHERE project_id = ?").bind(projectId), + env.DB.prepare("DELETE FROM docs WHERE project_id = ?").bind(projectId), env.DB.prepare("DELETE FROM projects WHERE id = ?").bind(projectId), ]); diff --git a/packages/admin/src/lib/userDeletion.sqlite.test.ts b/packages/admin/src/lib/userDeletion.sqlite.test.ts index 110f81f6..25a3d126 100644 --- a/packages/admin/src/lib/userDeletion.sqlite.test.ts +++ b/packages/admin/src/lib/userDeletion.sqlite.test.ts @@ -89,6 +89,60 @@ describe("delayed deletion (real SQLite)", () => { expect(audit).toEqual([{ action: "user.delete", actor_user_id: "admin-1" }]); }); + it("removes the account's FK-less rows in both DBs, and only that account's", async () => { + const t = setup(); + t.addUser("u-1"); + t.addUser("u-2"); + t.addSite("p-other", "u-2"); + const now = "2026-10-01 00:00:00"; + t.api.sqlite.prepare("INSERT INTO docs (id, title, project_id, author_id, created_at, updated_at) VALUES ('d-1', 'T', 'p-other', 'u-2', ?, ?)").run(now, now); + for (const uid of ["u-1", "u-2"]) { + t.api.sqlite.prepare("INSERT INTO doc_shares (id, doc_id, user_id, project_id, granted_by, created_at) VALUES (?, 'd-1', ?, 'p-other', 'u-2', ?)") + .run(`share-${uid}`, uid, now); + t.api.sqlite.prepare("INSERT INTO user_favourite_projects (user_id, project_id, created_at) VALUES (?, 'p-other', 1)").run(uid); + t.auth.sqlite.prepare("INSERT INTO email_verification_tokens (id, user_id, created_at, expires_at, email) VALUES (?, ?, 1, 2, ?)") + .run(`tok-${uid}`, uid, `new-${uid}@x.com`); + } + at(T0); + await t.scheduleDeletion("u-1"); + expect(await t.runAt(T0 + 7 * DAY + 60)).toEqual({ deleted: 1, failed: 0 }); + expect(t.userExists("u-1")).toBe(false); + + const users = (db: typeof t.api.sqlite, table: string) => + db.prepare(`SELECT user_id FROM ${table} ORDER BY user_id`).all().map(r => r.user_id); + expect(users(t.api.sqlite, "doc_shares")).toEqual(["u-2"]); + expect(users(t.api.sqlite, "user_favourite_projects")).toEqual(["u-2"]); + expect(users(t.auth.sqlite, "email_verification_tokens")).toEqual(["u-2"]); + }); + + it("a retry after the API-side cleanup already ran still finishes the account", async () => { + const t = setup(); + t.addUser("u-1"); + t.auth.sqlite.prepare("INSERT INTO email_verification_tokens (id, user_id, created_at, expires_at) VALUES ('tok-1', 'u-1', 1, 2)").run(); + at(T0); + await t.scheduleDeletion("u-1"); + // First attempt dies on the final auth batch (after the API deletes ran). + const realBatch = t.auth.db.batch.bind(t.auth.db); + let failNext = true; + (t.auth.db as unknown as { batch: typeof realBatch }).batch = async (stmts) => { + if (failNext && stmts.some(s => (s as unknown as { sql: string }).sql.includes("DELETE FROM users"))) { + failNext = false; + throw new Error("simulated auth batch failure"); + } + return realBatch(stmts); + }; + const run1 = T0 + 7 * DAY + 60; + expect(await t.runAt(run1)).toEqual({ deleted: 0, failed: 1 }); + expect(t.userExists("u-1")).toBe(true); + // The batch rolled back as a whole: the token is still there too. + expect(t.auth.sqlite.prepare("SELECT COUNT(*) AS n FROM email_verification_tokens").get()).toEqual({ n: 1 }); + expect(t.schedule("u-1")!.started_at).not.toBeNull(); + + expect(await t.runAt(run1 + 3600)).toEqual({ deleted: 1, failed: 0 }); + expect(t.userExists("u-1")).toBe(false); + expect(t.auth.sqlite.prepare("SELECT COUNT(*) AS n FROM email_verification_tokens").get()).toEqual({ n: 0 }); + }); + it("cancel restores the account and the cron leaves it alone", async () => { const t = setup(); t.addUser("u-1"); diff --git a/packages/admin/src/lib/userDeletion.test.ts b/packages/admin/src/lib/userDeletion.test.ts index 4ab595dd..91af497e 100644 --- a/packages/admin/src/lib/userDeletion.test.ts +++ b/packages/admin/src/lib/userDeletion.test.ts @@ -151,12 +151,15 @@ describe("executeUserDeletion", () => { expect.stringContaining("DELETE FROM project_members"), expect.stringContaining("DELETE FROM organization_members"), expect.stringContaining("DELETE FROM api_keys"), + expect.stringContaining("DELETE FROM doc_shares"), + expect.stringContaining("DELETE FROM user_favourite_projects"), ]); expect(env.ASSETS.delete).toHaveBeenCalledWith(["avatars/u-1-dark", "avatars/u-1-light", "avatars/u-1"]); const finalBatch = env.AUTH_DB.batches.at(-1)!; expect(finalBatch[0].sql).toContain("INSERT INTO admin_audit_log"); expect(finalBatch[0].binds[0]).toBe("user.delete"); - expect(finalBatch[1].sql).toContain("DELETE FROM users"); + expect(finalBatch[1].sql).toContain("DELETE FROM email_verification_tokens"); + expect(finalBatch[2].sql).toContain("DELETE FROM users"); expect(writeAdminAudit).not.toHaveBeenCalled(); expect(adminAuditStatement).toHaveBeenCalledTimes(1); }); diff --git a/packages/admin/src/lib/userDeletion.ts b/packages/admin/src/lib/userDeletion.ts index 0ca3201e..5a89bdcc 100644 --- a/packages/admin/src/lib/userDeletion.ts +++ b/packages/admin/src/lib/userDeletion.ts @@ -289,17 +289,22 @@ export async function executeUserDeletion( await env.DB.prepare(`DELETE FROM organizations WHERE owner_id = ? AND id IN (${placeholders(chunk.length)})`) .bind(userId, ...chunk).run(); } + // API-DB rows keyed by the user with no FK to cascade from (users live in + // the auth DB). Plain deletes, so a retried run just re-applies them. await env.DB.batch([ env.DB.prepare("DELETE FROM project_members WHERE user_id = ?").bind(userId), env.DB.prepare("DELETE FROM organization_members WHERE user_id = ?").bind(userId), env.DB.prepare("DELETE FROM api_keys WHERE user_id = ?").bind(userId), + env.DB.prepare("DELETE FROM doc_shares WHERE user_id = ?").bind(userId), + env.DB.prepare("DELETE FROM user_favourite_projects WHERE user_id = ?").bind(userId), ]); await env.ASSETS.delete([`avatars/${userId}-dark`, `avatars/${userId}-light`, `avatars/${userId}`]); // The account row and its audit record commit together: if either fails, // neither lands, the schedule survives (it cascades with the user), and - // the next run retries. + // the next run retries. email_verification_tokens has no FK to users, so + // it goes explicitly - its rows carry pending change-email addresses. await env.AUTH_DB.batch([ adminAuditStatement(env, actor, "user.delete", "user", userId, { email: user.email, @@ -309,6 +314,7 @@ export async function executeUserDeletion( stripe_customer_deleted: stripeCustomerDeleted, ...auditExtra, }), + env.AUTH_DB.prepare("DELETE FROM email_verification_tokens WHERE user_id = ?").bind(userId), env.AUTH_DB.prepare("DELETE FROM users WHERE id = ?").bind(userId), ]); diff --git a/packages/admin/src/routes/projects.test.ts b/packages/admin/src/routes/projects.test.ts index be0a5a64..38fbb841 100644 --- a/packages/admin/src/routes/projects.test.ts +++ b/packages/admin/src/routes/projects.test.ts @@ -291,23 +291,22 @@ describe("DELETE /:id - scale safety (AB-H1)", () => { "site-logos/proj-1-square", "site-logos/proj-1-wide", ]); - // 150 docs -> three 50-id chunks of (asset_revisions + doc_shares), then - // the final fts+projects batch. - expect(batches.length).toBe(4); - for (const batch of batches.slice(0, 3)) { - expect(batch[0].sql).toContain("asset_revisions"); - expect(batch[1].sql).toContain("doc_shares"); - // The whole point: never more than 50 bound params per statement. - expect(batch[0].binds.length).toBeLessThanOrEqual(50); - expect(batch[1].binds.length).toBeLessThanOrEqual(50); - } - const chunkBinds = batches.slice(0, 3).flatMap(b => b[0].binds); - expect(chunkBinds).toEqual(docIds.map(d => d.id)); - - const finalBatch = batches[3]; + // 150 docs -> three 50-id asset_revisions chunks (doc_shares cascade from + // docs, so there is no separate delete), then the final batch. + const chunks = calls.filter(c => c.sql.includes("asset_revisions")); + expect(chunks.length).toBe(3); + // The whole point: never more than 50 bound params per statement. + for (const chunk of chunks) expect(chunk.binds.length).toBeLessThanOrEqual(50); + expect(chunks.flatMap(c => c.binds)).toEqual(docIds.map(d => d.id)); + expect(calls.some(c => c.sql.includes("doc_shares"))).toBe(false); + + expect(batches.length).toBe(1); + const finalBatch = batches[0]; expect(finalBatch[0].sql).toContain("docs_fts"); - expect(finalBatch[1].sql).toContain("DELETE FROM projects"); + expect(finalBatch[1].sql).toContain("DELETE FROM docs WHERE project_id = ?"); expect(finalBatch[1].binds).toEqual(["proj-1"]); + expect(finalBatch[2].sql).toContain("DELETE FROM projects"); + expect(finalBatch[2].binds).toEqual(["proj-1"]); expect(releaseCustomDomain).toHaveBeenCalledWith(expect.anything(), "proj-1"); expect(writeAdminAudit).toHaveBeenCalledTimes(1);