Skip to content

Commit ad93262

Browse files
authored
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
1 parent c750695 commit ad93262

File tree

9 files changed

+235
-162
lines changed

9 files changed

+235
-162
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ func (q *querier) DeleteOAuth2ProviderAppTokensByAppAndUserID(ctx context.Contex
17321732
return q.db.DeleteOAuth2ProviderAppTokensByAppAndUserID(ctx, arg)
17331733
}
17341734

1735-
func (q *querier) DeleteOldAIBridgeRecords(ctx context.Context, beforeTime time.Time) (int32, error) {
1735+
func (q *querier) DeleteOldAIBridgeRecords(ctx context.Context, beforeTime time.Time) (int64, error) {
17361736
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceAibridgeInterception); err != nil {
17371737
return -1, err
17381738
}

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4705,7 +4705,7 @@ func (s *MethodTestSuite) TestAIBridge() {
47054705

47064706
s.Run("DeleteOldAIBridgeRecords", s.Mocked(func(db *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
47074707
t := dbtime.Now()
4708-
db.EXPECT().DeleteOldAIBridgeRecords(gomock.Any(), t).Return(int32(0), nil).AnyTimes()
4708+
db.EXPECT().DeleteOldAIBridgeRecords(gomock.Any(), t).Return(int64(0), nil).AnyTimes()
47094709
check.Args(t).Asserts(rbac.ResourceAibridgeInterception, policy.ActionDelete)
47104710
}))
47114711
}

coderd/database/dbmetrics/querymetrics.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbpurge/dbpurge.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, vals *coder
118118
return xerrors.Errorf("failed to delete old audit log connection events: %w", err)
119119
}
120120

121-
deleteAIBridgeRecordsBefore := start.Add(-vals.AI.BridgeConfig.Retention.Value())
122-
// nolint:gocritic // Needs to run as aibridge context.
123-
purgedAIBridgeRecords, err := tx.DeleteOldAIBridgeRecords(dbauthz.AsAIBridged(ctx), deleteAIBridgeRecordsBefore)
124-
if err != nil {
125-
return xerrors.Errorf("failed to delete old aibridge records: %w", err)
121+
var purgedAIBridgeRecords int64
122+
aibridgeRetention := vals.AI.BridgeConfig.Retention.Value()
123+
if aibridgeRetention > 0 {
124+
deleteAIBridgeRecordsBefore := start.Add(-aibridgeRetention)
125+
// nolint:gocritic // Needs to run as aibridge context.
126+
purgedAIBridgeRecords, err = tx.DeleteOldAIBridgeRecords(dbauthz.AsAIBridged(ctx), deleteAIBridgeRecordsBefore)
127+
if err != nil {
128+
return xerrors.Errorf("failed to delete old aibridge records: %w", err)
129+
}
126130
}
127131

128132
var purgedConnectionLogs int64

0 commit comments

Comments
 (0)