1 -- Remove tokens no longer in use.
3 -- reduce verbosity of using epoch timestamps
6 (strftime('%s', 'now') - :codeLifespanSeconds) AS code_valid_offset
,
7 strftime('%s', 'now') AS now
9 -- only clean after code has expired
11 SELECT t.code_id
, t.is_token
, t.is_revoked
, t.expires
, t.refresh_expires
FROM token t
, times
12 WHERE t.created
< times.code_valid_offset
14 DELETE FROM token WHERE code_id
IN (
15 SELECT code_id
FROM cleanable_codes c
, times
17 NOT c.is_token
-- profile-only redemption
20 c.is_revoked
-- revoked
22 c.expires
< times.now
AND (
23 -- expired and unrefreshable
24 refresh_expires
IS NULL
26 -- expired and refresh expired
27 (refresh_expires
IS NOT NULL AND refresh_expires
< times.now
)