Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/admin/src/lib/cursor.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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<?)");
});
});
11 changes: 9 additions & 2 deletions packages/admin/src/lib/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ export function decodeCursor(raw: string): KeysetCursor | null {
// cursor under ORDER BY (<tsCol> DESC, <idCol> 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,
idCol: string,
): { 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],
};
}
65 changes: 65 additions & 0 deletions packages/admin/src/lib/projectDelete.sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
23 changes: 14 additions & 9 deletions packages/admin/src/lib/projectDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -47,25 +47,30 @@ 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
// the project + its cascading project_custom_domains row. Best-effort no-op
// 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),
]);

Expand Down
54 changes: 54 additions & 0 deletions packages/admin/src/lib/userDeletion.sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion packages/admin/src/lib/userDeletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
8 changes: 7 additions & 1 deletion packages/admin/src/lib/userDeletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
]);

Expand Down
Loading
Loading