From 44e11def55113b86272a8537d359c6fa1f957608 Mon Sep 17 00:00:00 2001 From: RealDiligent Date: Tue, 21 Jul 2026 21:41:00 +0800 Subject: [PATCH] fix(review): only count real D1 writes in backfillContributorGateHistory inserted (#7804) Co-authored-by: Cursor --- .../contributor-gate-history-backfill.ts | 4 +-- .../contributor-gate-history-backfill.test.ts | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/review/contributor-gate-history-backfill.ts b/src/review/contributor-gate-history-backfill.ts index fef1a8389a..87f985aed8 100644 --- a/src/review/contributor-gate-history-backfill.ts +++ b/src/review/contributor-gate-history-backfill.ts @@ -82,14 +82,14 @@ export async function backfillContributorGateHistory(env: Env, opts: { limit?: n continue; } try { - await env.DB.prepare( + const result = await env.DB.prepare( `INSERT INTO contributor_gate_history (id, login, source, project, target_id, decision, head_sha, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO NOTHING`, ) .bind(`contrib:${login}:${c.source}:${c.targetId}@${c.headSha ?? "none"}`, login, c.source, c.project, c.targetId, c.decision, c.headSha, c.createdAt) .run(); - inserted += 1; + if (result.meta.changes > 0) inserted += 1; } catch (error) { console.warn(JSON.stringify({ event: "contributor_gate_history_backfill_write_error", project: c.project, targetId: c.targetId, message: errorMessage(error).slice(0, 200) })); } diff --git a/test/unit/contributor-gate-history-backfill.test.ts b/test/unit/contributor-gate-history-backfill.test.ts index f8fc0207d0..e5a9f81f55 100644 --- a/test/unit/contributor-gate-history-backfill.test.ts +++ b/test/unit/contributor-gate-history-backfill.test.ts @@ -149,6 +149,36 @@ describe("backfillContributorGateHistory (#fairness-analytics)", () => { warn.mockRestore(); }); + it("REGRESSION: does not count an ON CONFLICT DO NOTHING no-op toward inserted", async () => { + const candidate = { + project: "owner/repo", + targetId: "owner/repo#50", + decision: "merge", + headSha: "sha50", + source: "gittensory-native", + authorLogin: "octocat", + createdAt: "2026-06-25T12:00:00.000Z", + }; + const env = { + DB: { + prepare: (sql: string) => ({ + bind: (..._args: unknown[]) => { + if (/FROM review_audit/.test(sql)) { + return { all: async () => ({ results: [candidate] }) }; + } + if (/INSERT INTO contributor_gate_history/.test(sql)) { + return { run: async () => ({ meta: { changes: 0 } }) }; + } + throw new Error(`unexpected sql: ${sql}`); + }, + }), + }, + } as unknown as Env; + + const result = await backfillContributorGateHistory(env); + expect(result).toEqual({ scanned: 1, inserted: 0, skippedNoAuthor: 0, hasMore: false }); + }); + it("is best-effort per row: a write failure on one candidate is logged and does not stop the rest of the batch", async () => { const env = createTestEnv(); await insertPullRequest(env, "owner/repo", 20, "octocat");