@@ -71,6 +71,19 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz.
7171 if err := tx .ExpirePrebuildsAPIKeys (ctx , dbtime .Time (start )); err != nil {
7272 return xerrors .Errorf ("failed to expire prebuilds user api keys: %w" , err )
7373 }
74+ expiredAPIKeys , err := tx .DeleteExpiredAPIKeys (ctx , database.DeleteExpiredAPIKeysParams {
75+ // Leave expired keys for a week to allow the backend to know the difference
76+ // between a 404 and an expired key. This purge code is just to bound the size of
77+ // the table to something more reasonable.
78+ Before : dbtime .Time (start .Add (time .Hour * 24 * 7 * - 1 )),
79+ // There could be a lot of expired keys here, so set a limit to prevent this
80+ // taking too long.
81+ // This runs every 10 minutes, so it deletes ~1.5m keys per day at most.
82+ LimitCount : 10000 ,
83+ })
84+ if err != nil {
85+ return xerrors .Errorf ("failed to delete expired api keys: %w" , err )
86+ }
7487
7588 deleteOldAuditLogConnectionEventsBefore := start .Add (- maxAuditLogConnectionEventAge )
7689 if err := tx .DeleteOldAuditLogConnectionEvents (ctx , database.DeleteOldAuditLogConnectionEventsParams {
@@ -80,7 +93,7 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz.
8093 return xerrors .Errorf ("failed to delete old audit log connection events: %w" , err )
8194 }
8295
83- logger .Debug (ctx , "purged old database entries" , slog .F ("duration" , clk .Since (start )))
96+ logger .Debug (ctx , "purged old database entries" , slog .F ("expired_api_keys" , expiredAPIKeys ), slog . F ( " duration" , clk .Since (start )))
8497
8598 return nil
8699 }, database .DefaultTXOptions ().WithID ("db_purge" )); err != nil {
0 commit comments