mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-14 19:43:55 +00:00
refactor(#74): Enhance function robustness and configurability
This commit is contained in:
@@ -6,16 +6,29 @@ grant all privileges on all tables in schema cron to postgres;
|
|||||||
|
|
||||||
-- Create a function to delete expired tokens
|
-- Create a function to delete expired tokens
|
||||||
CREATE OR REPLACE FUNCTION delete_expired_tokens()
|
CREATE OR REPLACE FUNCTION delete_expired_tokens()
|
||||||
RETURNS void LANGUAGE plpgsql AS $$
|
RETURNS integer LANGUAGE plpgsql AS $$
|
||||||
|
DECLARE
|
||||||
|
deletion_count integer;
|
||||||
|
expiry_interval interval := '24 hours'::interval;
|
||||||
BEGIN
|
BEGIN
|
||||||
DELETE FROM account_deletion_requests
|
DELETE FROM account_deletion_requests
|
||||||
WHERE requested_at < NOW() - INTERVAL '24 hours';
|
WHERE requested_at < NOW() - expiry_interval
|
||||||
|
RETURNING COUNT(*) INTO deletion_count;
|
||||||
|
RAISE NOTICE 'Deleted % expired token(s)', deletion_count;
|
||||||
|
RETURN deletion_count;
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- Schedule the function to run every hour
|
DO $$
|
||||||
SELECT cron.schedule(
|
BEGIN
|
||||||
'delete_expired_tokens',
|
-- Schedule the function to run every hour
|
||||||
'0 * * * *',
|
PERFORM cron.schedule(
|
||||||
'CALL delete_expired_tokens()'
|
'delete_expired_tokens',
|
||||||
);
|
'0 * * * *',
|
||||||
|
'SELECT delete_expired_tokens()'
|
||||||
|
);
|
||||||
|
EXCEPTION WHEN OTHERS THEN
|
||||||
|
RAISE NOTICE 'Failed to schedule token cleanup: %', SQLERRM;
|
||||||
|
RAISE;
|
||||||
|
END;
|
||||||
|
$$;
|
||||||
Reference in New Issue
Block a user