39 lines
1.5 KiB
PL/PgSQL
39 lines
1.5 KiB
PL/PgSQL
-- delete outdated
|
|
DROP FUNCTION IF EXISTS public.tg__add_job();
|
|
|
|
|
|
-- 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,
|
|
'stream_id', NEW.id
|
|
), max_attempts := 12);
|
|
return NEW;
|
|
end;
|
|
$$;
|
|
|
|
|
|
|
|
|
|
-- when a stream is updated, we add a job in graphile to update_discord_message
|
|
CREATE TRIGGER stream_update
|
|
AFTER UPDATE ON api.streams
|
|
FOR EACH ROW
|
|
EXECUTE PROCEDURE public.tg__update_discord_message('update_discord_message');
|
|
|
|
-- when a stream is created, we add a 'record' job in graphile-worker
|
|
CREATE TRIGGER stream_create
|
|
AFTER INSERT ON api.streams
|
|
FOR EACH ROW
|
|
EXECUTE PROCEDURE public.tg__add_record_job('record');
|
|
|