source: db-scripts/triggers/compute_daily_completion_all.sql@ 42da64d

Last change on this file since 42da64d was 42da64d, checked in by Andrej <asumanovski@…>, 8 weeks ago

Fixed auth issue

  • Property mode set to 100644
File size: 1.1 KB
Line 
1-- Compute daily completion for all discipline users for a given date
2-- Usage: SELECT trekr.fn_compute_daily_completion_for_all(day := DATE '2026-05-06');
3
4CREATE OR REPLACE FUNCTION trekr.fn_compute_daily_completion_for_all(day date)
5RETURNS void
6LANGUAGE plpgsql
7AS $$
8DECLARE
9 u RECORD;
10BEGIN
11 IF day IS NULL THEN
12 RAISE EXCEPTION 'day is required';
13 END IF;
14
15 FOR u IN SELECT user_id FROM trekr.discipline_users LOOP
16 BEGIN
17 PERFORM trekr.fn_compute_daily_completion(u.user_id, day);
18 EXCEPTION WHEN OTHERS THEN
19 -- log and continue (requires a logging table or use RAISE NOTICE)
20 RAISE NOTICE 'compute_daily_completion failed for user %: %', u.user_id, SQLERRM;
21 END;
22 END LOOP;
23END;
24$$;
25
26-- Optionally create a pg_cron job to run nightly (requires pg_cron extension)
27-- Example (commented):
28-- CREATE EXTENSION IF NOT EXISTS pg_cron;
29-- SELECT cron.schedule('compute_daily_completions_every_day', '59 23 * * *',
30-- $$SELECT trekr.fn_compute_daily_completion_for_all(current_date - INTERVAL '1 day')$$);
31
Note: See TracBrowser for help on using the repository browser.