diff --git a/crates/db_schema/replaceable_schema/triggers.sql b/crates/db_schema/replaceable_schema/triggers.sql index 58862e66d..2d9b0df6e 100644 --- a/crates/db_schema/replaceable_schema/triggers.sql +++ b/crates/db_schema/replaceable_schema/triggers.sql @@ -655,55 +655,33 @@ CREATE TRIGGER change_values -- Combined tables triggers -- These insert (published, item_id) into X_combined tables --- Reports --- Comment -CREATE FUNCTION r.report_combined_comment_report_insert () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ +-- Reports (comment_report, post_report, private_message_report) +CREATE PROCEDURE r.create_report_combined_trigger (table_name text) +LANGUAGE plpgsql +AS $a$ BEGIN - INSERT INTO report_combined (published, comment_report_id) - VALUES (NEW.published, NEW.id); - RETURN NEW; -END -$$; + EXECUTE replace($b$ CREATE FUNCTION r.report_combined_thing_insert ( ) + RETURNS TRIGGER + LANGUAGE plpgsql + AS $$ + BEGIN + INSERT INTO report_combined (published, thing_id) + VALUES (NEW.published, NEW.id); + RETURN NEW; + END $$; + CREATE TRIGGER report_combined + AFTER INSERT ON thing + FOR EACH ROW + EXECUTE FUNCTION r.report_combined_thing_insert ( ); + $b$, + 'thing', + table_name); +END; +$a$; -CREATE TRIGGER report_combined_comment - AFTER INSERT ON comment_report - FOR EACH ROW - EXECUTE FUNCTION r.report_combined_comment_report_insert (); +CALL r.create_report_combined_trigger ('post_report'); --- Post -CREATE FUNCTION r.report_combined_post_report_insert () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - INSERT INTO report_combined (published, post_report_id) - VALUES (NEW.published, NEW.id); - RETURN NEW; -END -$$; +CALL r.create_report_combined_trigger ('comment_report'); -CREATE TRIGGER report_combined_post - AFTER INSERT ON post_report - FOR EACH ROW - EXECUTE FUNCTION r.report_combined_post_report_insert (); - --- Private message -CREATE FUNCTION r.report_combined_private_message_report_insert () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - INSERT INTO report_combined (published, private_message_report_id) - VALUES (NEW.published, NEW.id); - RETURN NEW; -END -$$; - -CREATE TRIGGER report_combined_private_message - AFTER INSERT ON private_message_report - FOR EACH ROW - EXECUTE FUNCTION r.report_combined_private_message_report_insert (); +CALL r.create_report_combined_trigger ('private_message_report');