feat: purge expired api keys in dbpurge (#20863)

closes https://github.com/coder/coder/issues/19889

This is in response to a migration in v2.27 that takes very long on deployments with large `api_key` tables.
This commit is contained in:
Steven Masley
2025-11-24 10:24:32 -06:00
committed by GitHub
parent c6631e1e50
commit cefe07d074
10 changed files with 197 additions and 4 deletions
+20
View File
@@ -85,6 +85,26 @@ DELETE FROM
WHERE
user_id = $1;
-- name: DeleteExpiredAPIKeys :one
WITH expired_keys AS (
SELECT id
FROM api_keys
-- expired keys only
WHERE expires_at < @before::timestamptz
LIMIT @limit_count
),
deleted_rows AS (
DELETE FROM
api_keys
USING
expired_keys
WHERE
api_keys.id = expired_keys.id
RETURNING api_keys.id
)
SELECT COUNT(deleted_rows.id) AS deleted_count FROM deleted_rows;
;
-- name: ExpirePrebuildsAPIKeys :exec
-- Firstly, collect api_keys owned by the prebuilds user that correlate
-- to workspaces no longer owned by the prebuilds user.