Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit bd172ff

Browse files
author
bitfl0wer
committed
feat: function cleanup_expired_tokens, table comment
1 parent bfcdd61 commit bd172ff

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

migrations/0007_api_keys.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,21 @@ CREATE TABLE IF NOT EXISTS user_tokens (
1010
uaid UUID NOT NULL REFERENCES actors (uaid) ON DELETE CASCADE,
1111
valid_not_after TIMESTAMP NULL
1212
);
13+
14+
COMMENT ON TABLE user_tokens IS 'User access token hashes. Cleans up expired tokens on each insert operation of this table. Use view filtering to exclude expired tokens on queries.';
15+
16+
-- We could use the postgres cron extension, but if it is possible and feasible, I'd like to stick
17+
-- with a "vanilla" postgres setup.
18+
CREATE OR REPLACE FUNCTION cleanup_expired_tokens()
19+
RETURNS TRIGGER AS $$
20+
BEGIN
21+
DELETE FROM user_tokens
22+
WHERE valid_not_after IS NOT NULL
23+
AND valid_not_after < NOW();
24+
RETURN NEW;
25+
END;
26+
$$ LANGUAGE plpgsql;
27+
28+
CREATE TRIGGER cleanup_expired_tokens_trigger
29+
AFTER INSERT ON user_tokens
30+
EXECUTE FUNCTION cleanup_expired_tokens();

0 commit comments

Comments
 (0)