update dependencies and devDependencies
[squeep-indie-auther] / src / db / sqlite / sql / token-cleanup.sql
1 -- Remove tokens no longer in use.
2 WITH
3 -- reduce verbosity of using epoch timestamps
4 times AS (
5 SELECT
6 (strftime('%s', 'now') - :codeLifespanSeconds) AS code_valid_offset,
7 strftime('%s', 'now') AS now
8 ),
9 -- only clean after code has expired
10 cleanable_codes AS (
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
13 )
14 DELETE FROM token WHERE code_id IN (
15 SELECT code_id FROM cleanable_codes c, times
16 WHERE
17 NOT c.is_token -- profile-only redemption
18 OR (
19 c.is_token AND (
20 c.is_revoked -- revoked
21 OR (
22 c.expires < times.now AND (
23 -- expired and unrefreshable
24 refresh_expires IS NULL
25 OR
26 -- expired and refresh expired
27 (refresh_expires IS NOT NULL AND refresh_expires < times.now)
28 )
29 )
30 )
31 )
32 )