fix: make workbench trace migration idempotent

This commit is contained in:
lyon
2026-06-20 20:24:48 +08:00
parent 4bd7ef5c96
commit 69d5264360
2 changed files with 22 additions and 13 deletions
@@ -391,23 +391,28 @@ INSERT INTO workbench_trace_events (
updated_at
)
SELECT
id,
trace_id,
agent_session_id,
trace_id,
source_trace_events.id,
source_trace_events.trace_id,
source_trace_events.agent_session_id,
source_trace_events.trace_id,
NULL,
0,
id,
0,
source_trace_events.durable_projection_seq,
source_trace_events.id,
source_trace_events.durable_projection_seq,
'agent_trace_event',
false,
false,
COALESCE(NULLIF(event_json, ''), '{}'),
occurred_at,
occurred_at
FROM agent_trace_events
WHERE id IS NOT NULL AND trace_id IS NOT NULL
ON CONFLICT (id) DO NOTHING;
COALESCE(NULLIF(source_trace_events.event_json, ''), '{}'),
source_trace_events.occurred_at,
source_trace_events.occurred_at
FROM (
SELECT
agent_trace_events.*,
(ROW_NUMBER() OVER (PARTITION BY trace_id ORDER BY occurred_at, id))::integer AS durable_projection_seq
FROM agent_trace_events
WHERE id IS NOT NULL AND trace_id IS NOT NULL
) AS source_trace_events
ON CONFLICT DO NOTHING;
INSERT INTO workbench_turns (
turn_id,
+4
View File
@@ -80,6 +80,10 @@ test("initial migration declares Workbench fact backfill sources", async () => {
assert.match(sql, /ON CONFLICT \(session_id\) DO NOTHING/u);
assert.match(sql, /ON CONFLICT \(trace_id\) DO NOTHING/u);
const traceBackfillMatch = sql.match(/INSERT INTO workbench_trace_events \([\s\S]*?ON CONFLICT DO NOTHING;/u);
assert.ok(traceBackfillMatch, "missing workbench_trace_events idempotent backfill");
assert.match(traceBackfillMatch[0], /ROW_NUMBER\(\) OVER \(PARTITION BY trace_id ORDER BY occurred_at, id\)/u);
assert.match(traceBackfillMatch[0], /durable_projection_seq/u);
});
test("protocol record guards catch schema drift before runtime writes", () => {