From 8788b90271ac8a64728c4c31f6f1b93567bef95e Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Tue, 12 Nov 2024 21:53:01 +0000 Subject: [PATCH 1/2] feat(#74): Add token expiration for account deletion --- ...41112214000_add_token_cleanup_function.sql | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 supabase/migrations/20241112214000_add_token_cleanup_function.sql diff --git a/supabase/migrations/20241112214000_add_token_cleanup_function.sql b/supabase/migrations/20241112214000_add_token_cleanup_function.sql new file mode 100644 index 0000000..b85de41 --- /dev/null +++ b/supabase/migrations/20241112214000_add_token_cleanup_function.sql @@ -0,0 +1,21 @@ +-- Enable the "pg_cron" extension +create extension pg_cron with schema pg_catalog; + +grant usage on schema cron to postgres; +grant all privileges on all tables in schema cron to postgres; + +-- Create a function to delete expired tokens +CREATE OR REPLACE FUNCTION delete_expired_tokens() +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + DELETE FROM account_deletion_requests + WHERE requested_at < NOW() - INTERVAL '24 hours'; +END; +$$; + +-- Schedule the function to run every hour +SELECT cron.schedule( + 'delete_expired_tokens', + '0 * * * *', + 'CALL delete_expired_tokens()' +); \ No newline at end of file From 77254c85fdf17f119a62c11581df078e0f22e154 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Tue, 12 Nov 2024 22:26:05 +0000 Subject: [PATCH 2/2] refactor(#74): Enhance function robustness and configurability --- ...41112214000_add_token_cleanup_function.sql | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/supabase/migrations/20241112214000_add_token_cleanup_function.sql b/supabase/migrations/20241112214000_add_token_cleanup_function.sql index b85de41..9a0d392 100644 --- a/supabase/migrations/20241112214000_add_token_cleanup_function.sql +++ b/supabase/migrations/20241112214000_add_token_cleanup_function.sql @@ -6,16 +6,29 @@ grant all privileges on all tables in schema cron to postgres; -- Create a function to 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 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; $$; --- Schedule the function to run every hour -SELECT cron.schedule( - 'delete_expired_tokens', - '0 * * * *', - 'CALL delete_expired_tokens()' -); \ No newline at end of file +DO $$ +BEGIN + -- Schedule the function to run every hour + PERFORM cron.schedule( + 'delete_expired_tokens', + '0 * * * *', + 'SELECT delete_expired_tokens()' + ); +EXCEPTION WHEN OTHERS THEN + RAISE NOTICE 'Failed to schedule token cleanup: %', SQLERRM; + RAISE; +END; +$$; \ No newline at end of file