fix(coderd/database/dbpurge): allow disabling AI Bridge retention with 0 (#21062)

Previously setting AI Bridge retention to 0 would cause records to be
deleted immediately since we didn't check for the zero value before
calculating the deletion threshold.

This adds a check for aibridgeRetention > 0 to skip deletion when
retention is disabled, matching the pattern used for other retention
settings (connection logs, audit logs, etc.).

Also fixes the return type of DeleteOldAIBridgeRecords from int32 to
int64 since COUNT(*) returns bigint in PostgreSQL.

Refs #21055
This commit is contained in:
Mathias Fredriksson
2025-12-03 11:37:18 +02:00
committed by GitHub
parent c750695d83
commit ad93262d07
9 changed files with 239 additions and 166 deletions
+5 -4
View File
@@ -354,17 +354,18 @@ WITH
WHERE id IN (SELECT id FROM to_delete)
RETURNING 1
)
SELECT
SELECT (
(SELECT COUNT(*) FROM tool_usages) +
(SELECT COUNT(*) FROM token_usages) +
(SELECT COUNT(*) FROM user_prompts) +
(SELECT COUNT(*) FROM interceptions) as total_deleted
(SELECT COUNT(*) FROM interceptions)
)::bigint as total_deleted
`
// Cumulative count.
func (q *sqlQuerier) DeleteOldAIBridgeRecords(ctx context.Context, beforeTime time.Time) (int32, error) {
func (q *sqlQuerier) DeleteOldAIBridgeRecords(ctx context.Context, beforeTime time.Time) (int64, error) {
row := q.db.QueryRowContext(ctx, deleteOldAIBridgeRecords, beforeTime)
var total_deleted int32
var total_deleted int64
err := row.Scan(&total_deleted)
return total_deleted, err
}