mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add configurable retention for aibridge (#20828)
Closes https://github.com/coder/internal/issues/1134 --------- Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
@@ -326,3 +326,38 @@ FROM
|
||||
prompt_aggregates pa,
|
||||
tool_aggregates tool_agg
|
||||
;
|
||||
|
||||
-- name: DeleteOldAIBridgeRecords :one
|
||||
WITH
|
||||
-- We don't have FK relationships between the dependent tables and aibridge_interceptions, so we can't rely on DELETE CASCADE.
|
||||
to_delete AS (
|
||||
SELECT id FROM aibridge_interceptions
|
||||
WHERE started_at < @before_time::timestamp with time zone
|
||||
),
|
||||
-- CTEs are executed in order.
|
||||
tool_usages AS (
|
||||
DELETE FROM aibridge_tool_usages
|
||||
WHERE interception_id IN (SELECT id FROM to_delete)
|
||||
RETURNING 1
|
||||
),
|
||||
token_usages AS (
|
||||
DELETE FROM aibridge_token_usages
|
||||
WHERE interception_id IN (SELECT id FROM to_delete)
|
||||
RETURNING 1
|
||||
),
|
||||
user_prompts AS (
|
||||
DELETE FROM aibridge_user_prompts
|
||||
WHERE interception_id IN (SELECT id FROM to_delete)
|
||||
RETURNING 1
|
||||
),
|
||||
interceptions AS (
|
||||
DELETE FROM aibridge_interceptions
|
||||
WHERE id IN (SELECT id FROM to_delete)
|
||||
RETURNING 1
|
||||
)
|
||||
-- Cumulative count.
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM tool_usages) +
|
||||
(SELECT COUNT(*) FROM token_usages) +
|
||||
(SELECT COUNT(*) FROM user_prompts) +
|
||||
(SELECT COUNT(*) FROM interceptions) as total_deleted;
|
||||
|
||||
Reference in New Issue
Block a user