Files
Convivo/supabase/migrations/20261009231817_delete_event_schedule.sql
22de4a8645 feat: add event-scoped schedule deletion and import date guard
Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-10 01:44:40 +02:00

158 lines
5.1 KiB
PL/PgSQL

-- Admin-only schedule wipe for one event. This removes slots and sessions for
-- the requested event; FK cascades remove dependent session rows, but event
-- registrations, contacts, sponsors, connections, points and the event itself
-- are intentionally left intact.
create or replace function public.delete_event_schedule(p_event_id uuid)
returns jsonb
language plpgsql security definer set search_path = '' as $$
declare
v_event public.events;
v_slots_deleted integer := 0;
v_sessions_deleted integer := 0;
v_session_speakers_deleted integer := 0;
v_preferences_deleted integer := 0;
v_checkins_deleted integer := 0;
v_feedback_deleted integer := 0;
v_material_paths_unpurged integer := 0;
begin
if auth.uid() is null then
raise exception 'not authenticated' using errcode = '42501';
end if;
if not public.is_admin() then
raise exception 'admin only' using errcode = '42501';
end if;
if p_event_id is null then
raise exception 'event id is required';
end if;
select * into v_event
from public.events
where id = p_event_id
for update;
if not found then
raise exception 'event not found';
end if;
perform 1 from public.slots where event_id = p_event_id order by id for update;
perform 1 from public.sessions where event_id = p_event_id order by id for update;
perform 1
from public.preferences preference
join public.sessions talk on talk.id = preference.session_id
where talk.event_id = p_event_id
order by preference.id
for update of preference;
perform 1
from public.checkins checkin_row
join public.sessions talk on talk.id = checkin_row.session_id
where talk.event_id = p_event_id
order by checkin_row.id
for update of checkin_row;
perform 1
from public.feedback feedback_row
join public.sessions talk on talk.id = feedback_row.session_id
where talk.event_id = p_event_id
order by feedback_row.id
for update of feedback_row;
if exists (
select 1
from public.sessions talk
join public.slots slot on slot.id = talk.slot_id
where talk.event_id = p_event_id
and slot.event_id <> p_event_id
) then
raise exception 'cannot delete schedule: session references a slot from another event';
end if;
if exists (
select 1
from public.sessions talk
join public.slots slot on slot.id = talk.slot_id
where slot.event_id = p_event_id
and talk.event_id <> p_event_id
) then
raise exception 'cannot delete schedule: another event has a session in this event slot';
end if;
if exists (
select 1
from public.preferences preference
join public.sessions talk on talk.id = preference.session_id
where talk.event_id = p_event_id
and preference.event_id <> p_event_id
) then
raise exception 'cannot delete schedule: preference event mismatch';
end if;
if exists (
select 1
from public.checkins checkin_row
join public.sessions talk on talk.id = checkin_row.session_id
where talk.event_id = p_event_id
and checkin_row.event_id <> p_event_id
) then
raise exception 'cannot delete schedule: check-in event mismatch';
end if;
if exists (
select 1
from public.feedback feedback_row
join public.sessions talk on talk.id = feedback_row.session_id
where talk.event_id = p_event_id
and feedback_row.event_id <> p_event_id
) then
raise exception 'cannot delete schedule: feedback event mismatch';
end if;
select count(*) into v_slots_deleted
from public.slots
where event_id = p_event_id;
select count(*) into v_sessions_deleted
from public.sessions
where event_id = p_event_id;
select count(*) into v_session_speakers_deleted
from public.session_speakers speaker
join public.sessions talk on talk.id = speaker.session_id
where talk.event_id = p_event_id;
select count(*) into v_preferences_deleted
from public.preferences preference
join public.sessions talk on talk.id = preference.session_id
where talk.event_id = p_event_id;
select count(*) into v_checkins_deleted
from public.checkins checkin_row
join public.sessions talk on talk.id = checkin_row.session_id
where talk.event_id = p_event_id;
select count(*) into v_feedback_deleted
from public.feedback feedback_row
join public.sessions talk on talk.id = feedback_row.session_id
where talk.event_id = p_event_id;
select count(*) into v_material_paths_unpurged
from public.sessions
where event_id = p_event_id
and material_path is not null;
delete from public.sessions
where event_id = p_event_id;
delete from public.slots
where event_id = p_event_id;
return jsonb_build_object(
'event_id', p_event_id,
'event_name', v_event.name,
'slots_deleted', v_slots_deleted,
'sessions_deleted', v_sessions_deleted,
'session_speakers_deleted', v_session_speakers_deleted,
'preferences_deleted', v_preferences_deleted,
'checkins_deleted', v_checkins_deleted,
'feedback_deleted', v_feedback_deleted,
'material_paths_unpurged', v_material_paths_unpurged
);
end;
$$;
revoke all on function public.delete_event_schedule(uuid) from public, anon, authenticated;
grant execute on function public.delete_event_schedule(uuid) to authenticated;
notify pgrst, 'reload schema';