Skip to content

Commit 155f361

Browse files
committed
feat: update pg_cron migration — rename daily-cron→youtube-stats, handle existing jobs
- Unschedules existing jobs (daily-cron, daily-content-ingest) before creating new ones - Renames daily-cron to youtube-stats (same schedule: midnight UTC, same /api/cron endpoint) - Replaces daily-content-ingest with ingest-daily (10am UTC) - Updates check-renders from every 2min to every 5min - All unschedules wrapped in DO blocks for idempotent re-runs - Uses current_setting('app.cron_secret') for auth headers
1 parent 2ac61b4 commit 155f361

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

supabase/migrations/002_cron_schedules.sql

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,53 @@
44
--
55
-- Prerequisites:
66
-- These Supabase config vars must be set before running this migration:
7-
-- ALTER DATABASE postgres SET app.site_url = 'https://your-vercel-url.vercel.app';
7+
-- ALTER DATABASE postgres SET app.site_url = 'https://codingcat.dev';
88
-- ALTER DATABASE postgres SET app.cron_secret = 'your-cron-secret-here';
99
--
1010
-- You can set them in the Supabase dashboard under Database → Extensions,
1111
-- or via SQL in the SQL Editor.
1212
--
1313
-- Pipeline flow:
14-
-- 1. ingest-daily → discovers trends, creates Sanity doc (status: "researching" or "script_ready")
15-
-- 2. check-research → polls NotebookLM, enriches script, transitions to "script_ready"
16-
-- 3. check-renders → audio gen → Remotion render → upload → publish
17-
-- 4. sponsor-outreach → automated sponsor discovery + outreach emails
14+
-- 1. youtube-stats → daily YouTube analytics sync (renamed from daily-cron)
15+
-- 2. ingest-daily → discovers trends, creates Sanity doc (status: "researching" or "script_ready")
16+
-- 3. check-research → polls NotebookLM, enriches script, transitions to "script_ready"
17+
-- 4. check-renders → audio gen → Remotion render → upload → publish
18+
-- 5. sponsor-outreach → automated sponsor discovery + outreach emails
1819
-- ==========================================================================
1920

2021
-- Enable required extensions
2122
CREATE EXTENSION IF NOT EXISTS pg_cron;
2223
CREATE EXTENSION IF NOT EXISTS pg_net;
2324

2425
-- ---------------------------------------------------------------------------
25-
-- Remove any existing schedules (idempotent re-runs)
26+
-- Remove old/renamed schedules (idempotent re-runs)
2627
-- ---------------------------------------------------------------------------
2728
-- pg_cron's unschedule() throws if the job doesn't exist, so we use DO blocks
29+
30+
-- Old name → renamed to youtube-stats
31+
DO $$
32+
BEGIN
33+
PERFORM cron.unschedule('daily-cron');
34+
EXCEPTION WHEN OTHERS THEN
35+
NULL;
36+
END $$;
37+
38+
-- Old name → replaced by ingest-daily
39+
DO $$
40+
BEGIN
41+
PERFORM cron.unschedule('daily-content-ingest');
42+
EXCEPTION WHEN OTHERS THEN
43+
NULL;
44+
END $$;
45+
46+
-- Remove our own jobs too (for idempotent re-runs)
47+
DO $$
48+
BEGIN
49+
PERFORM cron.unschedule('youtube-stats');
50+
EXCEPTION WHEN OTHERS THEN
51+
NULL;
52+
END $$;
53+
2854
DO $$
2955
BEGIN
3056
PERFORM cron.unschedule('ingest-daily');
@@ -53,8 +79,21 @@ EXCEPTION WHEN OTHERS THEN
5379
NULL;
5480
END $$;
5581

82+
-- ---------------------------------------------------------------------------
83+
-- Schedule: YouTube Stats — daily at midnight UTC (renamed from daily-cron)
84+
-- ---------------------------------------------------------------------------
85+
SELECT cron.schedule(
86+
'youtube-stats',
87+
'0 0 * * *',
88+
$$SELECT net.http_get(
89+
url := current_setting('app.site_url') || '/api/cron',
90+
headers := jsonb_build_object('Authorization', 'Bearer ' || current_setting('app.cron_secret'))
91+
)$$
92+
);
93+
5694
-- ---------------------------------------------------------------------------
5795
-- Schedule: Ingest — daily at 10:00 UTC
96+
-- Discovers trending topics, creates Sanity doc, starts NotebookLM research
5897
-- ---------------------------------------------------------------------------
5998
SELECT cron.schedule(
6099
'ingest-daily',

0 commit comments

Comments
 (0)