Skip to content

Commit a8ffe90

Browse files
committed
fix: wrap all cron.schedule calls in exception-handling block
Move cron.schedule() calls inside the DO/EXCEPTION block so the entire seed script succeeds when pg_cron is not installed (e.g. local dev). Previously the unschedule was protected but schedule calls were bare SQL that would crash the script.
1 parent 2d88713 commit a8ffe90

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

supabase/seed.sql

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,26 @@ CREATE POLICY IF NOT EXISTS "Authenticated users can delete CAD files"
4747
USING (bucket_id = 'parts-cad');
4848

4949
-- Schedule cron jobs (requires pg_cron extension)
50-
-- Idempotent: unschedule first, then reschedule
50+
-- Entire block is wrapped in exception handling so the seed file
51+
-- succeeds even when pg_cron is not available (e.g. local dev).
5152
DO $$
5253
BEGIN
53-
-- Remove existing jobs if they exist (safe to call even if not present)
54+
-- Remove existing jobs if present (idempotent)
5455
PERFORM cron.unschedule('monthly-parts-reset') WHERE EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'monthly-parts-reset');
5556
PERFORM cron.unschedule('check-jobs-due-soon') WHERE EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'check-jobs-due-soon');
5657
PERFORM cron.unschedule('auto-close-attendance') WHERE EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'auto-close-attendance');
5758
PERFORM cron.unschedule('cleanup-expired-invitations') WHERE EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'cleanup-expired-invitations');
5859
PERFORM cron.unschedule('cleanup-mqtt-logs') WHERE EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'cleanup-mqtt-logs');
60+
61+
-- Schedule jobs
62+
PERFORM cron.schedule('monthly-parts-reset', '0 0 1 * *', 'SELECT reset_monthly_parts_counters()');
63+
PERFORM cron.schedule('check-jobs-due-soon', '0 8 * * *', 'SELECT check_jobs_due_soon()');
64+
PERFORM cron.schedule('auto-close-attendance', '0 0 * * *', 'SELECT auto_close_stale_attendance()');
65+
PERFORM cron.schedule('cleanup-expired-invitations','0 2 * * *', 'SELECT cleanup_expired_invitations()');
66+
PERFORM cron.schedule('cleanup-mqtt-logs', '0 3 * * 0', 'SELECT cleanup_old_mqtt_logs()');
67+
68+
RAISE NOTICE 'pg_cron jobs scheduled successfully';
5969
EXCEPTION WHEN OTHERS THEN
60-
-- pg_cron may not be available; skip gracefully
61-
RAISE NOTICE 'Could not unschedule existing cron jobs: %', SQLERRM;
70+
RAISE NOTICE 'pg_cron not available — skipping cron job scheduling: %', SQLERRM;
6271
END;
6372
$$;
64-
65-
-- Monthly reset of parts counter (1st of each month at midnight UTC)
66-
SELECT cron.schedule(
67-
'monthly-parts-reset',
68-
'0 0 1 * *',
69-
$$SELECT reset_monthly_parts_counters()$$
70-
);
71-
72-
-- Check for jobs due soon (daily at 8am UTC)
73-
SELECT cron.schedule(
74-
'check-jobs-due-soon',
75-
'0 8 * * *',
76-
$$SELECT check_jobs_due_soon()$$
77-
);
78-
79-
-- Auto-close stale attendance entries (daily at midnight UTC)
80-
SELECT cron.schedule(
81-
'auto-close-attendance',
82-
'0 0 * * *',
83-
$$SELECT auto_close_stale_attendance()$$
84-
);
85-
86-
-- Cleanup expired invitations (daily at 2am UTC)
87-
SELECT cron.schedule(
88-
'cleanup-expired-invitations',
89-
'0 2 * * *',
90-
$$SELECT cleanup_expired_invitations()$$
91-
);
92-
93-
-- Cleanup old MQTT logs (weekly on Sunday at 3am UTC)
94-
SELECT cron.schedule(
95-
'cleanup-mqtt-logs',
96-
'0 3 * * 0',
97-
$$SELECT cleanup_old_mqtt_logs()$$
98-
);

0 commit comments

Comments
 (0)