Skip to content

Commit aed3399

Browse files
committed
nits
1 parent d98b1e3 commit aed3399

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

supabase/migrations/20251006232741_reduce-db-ram-usage.sql

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
-- Replaced with Redis for webhook status tracking
22
DROP TABLE IF EXISTS public.webhook_process_status;
33

4-
-- Replace materialized view with regular table for better performance
5-
-- Drop the materialized view and its indices first
6-
DROP MATERIALIZED VIEW IF EXISTS public.workflow_events_summary CASCADE;
7-
84
-- Create workflow_runs table (replacing the materialized view)
95
-- This table will be directly upserted to by the webhook handler
106
CREATE TABLE IF NOT EXISTS public.workflow_runs (
@@ -55,6 +51,7 @@ CREATE INDEX idx_workflow_runs_unique_lookup ON public.workflow_runs USING btree
5551

5652
-- Backfill workflow_runs table with existing data from the materialized view
5753
-- This preserves historical data before we drop the view
54+
-- Join with repositories table to get repository_name (matching how the view was built)
5855
INSERT INTO public.workflow_runs (
5956
workflow_run_id,
6057
class_id,
@@ -79,28 +76,35 @@ INSERT INTO public.workflow_runs (
7976
updated_at
8077
)
8178
SELECT
82-
workflow_run_id,
83-
class_id,
84-
repository_name,
85-
workflow_name,
86-
workflow_path,
87-
head_sha,
88-
head_branch,
89-
run_number,
90-
run_attempt,
91-
actor_login,
92-
triggering_actor_login,
93-
assignment_id,
94-
profile_id,
95-
requested_at,
96-
in_progress_at,
97-
completed_at,
98-
conclusion,
99-
queue_time_seconds,
100-
run_time_seconds,
79+
wes.workflow_run_id,
80+
wes.class_id,
81+
r.repository as repository_name, -- Get repository name from repositories table
82+
wes.workflow_name,
83+
wes.workflow_path,
84+
wes.head_sha,
85+
wes.head_branch,
86+
wes.run_number,
87+
wes.run_attempt,
88+
wes.actor_login,
89+
wes.triggering_actor_login,
90+
wes.assignment_id,
91+
wes.profile_id,
92+
wes.requested_at,
93+
wes.in_progress_at,
94+
wes.completed_at,
95+
wes.conclusion,
96+
wes.queue_time_seconds,
97+
wes.run_time_seconds,
10198
NOW() as created_at,
10299
NOW() as updated_at
103-
FROM public.workflow_events_summary
100+
FROM public.workflow_events_summary wes
101+
LEFT JOIN public.repositories r ON (
102+
r.assignment_id = wes.assignment_id
103+
AND (
104+
(wes.profile_id IS NOT NULL AND r.profile_id = wes.profile_id)
105+
OR (wes.profile_id IS NULL AND r.assignment_group_id IS NOT NULL)
106+
)
107+
)
104108
ON CONFLICT ON CONSTRAINT workflow_runs_unique_run DO NOTHING;
105109

106110
-- Log the backfill results
@@ -320,7 +324,10 @@ CREATE TRIGGER trigger_maintain_workflow_runs
320324
COMMENT ON TRIGGER trigger_maintain_workflow_runs ON public.workflow_events IS
321325
'Maintains workflow_runs table in real-time by aggregating events as they arrive. Eliminates need for materialized view refreshes.';
322326

323-
-- Update get_workflow_events_summary_for_class to use workflow_runs table
327+
-- Drop public.get_workflow_events_summary_for_class function
328+
DROP FUNCTION IF EXISTS public.get_workflow_events_summary_for_class(bigint);
329+
330+
-- Create new get_workflow_events_summary_for_class function
324331
CREATE OR REPLACE FUNCTION public.get_workflow_events_summary_for_class(p_class_id bigint)
325332
RETURNS SETOF public.workflow_runs
326333
LANGUAGE plpgsql
@@ -590,6 +597,10 @@ COMMENT ON FUNCTION public.get_all_class_metrics() IS
590597
-- Drop the now-obsolete refresh function and its scheduled job
591598
DROP FUNCTION IF EXISTS public.refresh_workflow_events_summary();
592599

600+
-- Replace materialized view with regular table for better performance
601+
-- Drop the materialized view and its indices
602+
DROP MATERIALIZED VIEW IF EXISTS public.workflow_events_summary CASCADE;
603+
593604
-- Drop the pg_cron job that was refreshing the materialized view every 5 minutes
594605
DO $$
595606
BEGIN

0 commit comments

Comments
 (0)