fp/services/migrations-schema/migrations/00038_add-vods-update-trigg...

42 lines
1.6 KiB
MySQL
Raw Normal View History

2024-08-27 07:11:24 +00:00
-- we are moving recording functionality from streams to vods
-- delete outdated
DROP FUNCTION IF EXISTS public.tg__add_record_job CASCADE;
DROP TRIGGER IF EXISTS stream_update ON api.streams;
DROP TRIGGER IF EXISTS stream_create ON api.streams;
-- We create a function which lets Postgrest's automation user create jobs in Graphile Worker.
-- Normally only the database owner, in our case `postgres`, can add jobs due to RLS in graphile_worker tables.
-- Under the advice of graphile_worker author, we can use a SECURITY DEFINER wrapper function.
-- @see https://worker.graphile.org/docs/sql-add-job#graphile_workeradd_job:~:text=graphile_worker.add_job(...),that%20are%20necessary.)
-- @see https://discord.com/channels/489127045289476126/1179293106336694333/1179605043729670306
-- @see https://discord.com/channels/489127045289476126/498852330754801666/1067707497235873822
CREATE FUNCTION public.tg__add_record_job() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'pg_catalog', 'public', 'pg_temp'
AS $$
begin
PERFORM graphile_worker.add_job('record', json_build_object(
'url', NEW.url,
'vod_id', NEW.id
), max_attempts := 12);
return NEW;
end;
$$;
-- when a vod is updated, we add a job in graphile to update_discord_message
CREATE TRIGGER vod_update
AFTER UPDATE ON api.vods
FOR EACH ROW
EXECUTE PROCEDURE public.tg__update_discord_message('update_discord_message');
-- when a vod is created, we add a 'record' job in graphile-worker
CREATE TRIGGER vod_create
AFTER INSERT ON api.vods
FOR EACH ROW
EXECUTE PROCEDURE public.tg__add_record_job('record');