Update replaceable_schema.sql

This commit is contained in:
dullbananas 2023-12-23 16:09:55 -07:00 committed by GitHub
parent d2dc15330d
commit 1eb231cc97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,7 @@ CREATE SCHEMA r;
-- Rank calculations -- Rank calculations
CREATE OR REPLACE FUNCTION controversy_rank (upvotes numeric, downvotes numeric) CREATE OR REPLACE FUNCTION r.controversy_rank (upvotes numeric, downvotes numeric)
RETURNS float RETURNS float
AS $$ AS $$
BEGIN BEGIN
@ -40,13 +40,18 @@ IMMUTABLE;
-- These triggers resolve an item's reports when the item is marked as removed. -- These triggers resolve an item's reports when the item is marked as removed.
CREATE FUNCTION resolve_reports_when_comment_removed () CREATE PROCEDURE r.resolve_reports_when_target_removed (target_name text)
LANGUAGE plpgsql
AS $a$
BEGIN
EXECUTE format($b$
CREATE FUNCTION r.resolve_reports_when_%1$s_removed ()
RETURNS trigger RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
UPDATE UPDATE
comment_report %1$s_report AS report
SET SET
resolved = TRUE, resolved = TRUE,
resolver_id = mod_person_id, resolver_id = mod_person_id,
@ -54,48 +59,30 @@ BEGIN
FROM FROM
new_removal new_removal
WHERE WHERE
comment_report.comment_id = new_removal.comment_id AND new_removal.removed; report.%1$s_id = new_removal.%1$a_id AND new_removal.removed;
RETURN NULL; RETURN NULL;
END END
$$; $$;
CREATE TRIGGER resolve_reports CREATE TRIGGER resolve_reports
AFTER INSERT ON mod_remove_comment AFTER INSERT ON mod_remove_%1$s
REFERENCING NEW TABLE AS new_removal REFERENCING NEW TABLE AS new_removal
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION resolve_reports_when_comment_removed (); EXECUTE FUNCTION r.resolve_reports_when_%1$s_removed ();
$b$,
CREATE FUNCTION resolve_reports_when_post_removed () target_name);
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
post_report
SET
resolved = TRUE,
resolver_id = mod_person_id,
updated = now()
FROM
new_removal
WHERE
post_report.post_id = new_removal.post_id AND new_removal.removed;
RETURN NULL;
END END
$$; $a$;
CREATE TRIGGER resolve_reports CALL r.resolve_reports_when_target_removed ('comment');
AFTER INSERT ON mod_remove_post
REFERENCING NEW TABLE AS new_removal CALL r.resolve_reports_when_target_removed ('post');
FOR EACH STATEMENT
EXECUTE FUNCTION resolve_reports_when_post_removed ();
-- These triggers create and update rows in each aggregates table to match its associated table's rows. -- These triggers create and update rows in each aggregates table to match its associated table's rows.
-- Deleting rows and updating IDs are already handled by `CASCADE` in foreign key constraints. -- Deleting rows and updating IDs are already handled by `CASCADE` in foreign key constraints.
CREATE FUNCTION comment_aggregates_from_comment () CREATE FUNCTION r.comment_aggregates_from_comment ()
RETURNS trigger RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
@ -139,7 +126,7 @@ CREATE TRIGGER aggregates
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION r.community_aggregates_from_community (); EXECUTE FUNCTION r.community_aggregates_from_community ();
CREATE FUNCTION person_aggregates_from_person () CREATE FUNCTION r.person_aggregates_from_person ()
RETURNS trigger RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
@ -220,12 +207,12 @@ CREATE TRIGGER aggregates
-- These triggers update aggregates in response to votes. -- These triggers update aggregates in response to votes.
CREATE PROCEDURE aggregates_from_like (target_name text, creator_id_getter text) CREATE PROCEDURE r.aggregates_from_like (target_name text, creator_id_getter text)
LANGUAGE plpgsql LANGUAGE plpgsql
AS $a$ AS $a$
BEGIN BEGIN
EXECUTE format($b$ EXECUTE format($b$
CREATE FUNCTION %1$s_aggregates_from_like () CREATE FUNCTION r.%1$s_aggregates_from_like ()
RETURNS trigger RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
@ -294,16 +281,16 @@ BEGIN
AFTER INSERT OR DELETE OR UPDATE OF score ON %1$s_like AFTER INSERT OR DELETE OR UPDATE OF score ON %1$s_like
REFERENCING OLD TABLE AS old_like NEW TABLE AS new_like REFERENCING OLD TABLE AS old_like NEW TABLE AS new_like
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION %1$s_aggregates_from_like; EXECUTE FUNCTION r.%1$s_aggregates_from_like;
$b$, $b$,
target_name, target_name,
creator_id_getter); creator_id_getter);
END END
$a$; $a$;
CALL aggregates_from_like ('comment', '(SELECT creator_id FROM comment WHERE id = vote_group.target_id LIMIT 1)'); CALL r.aggregates_from_like ('comment', '(SELECT creator_id FROM comment WHERE id = vote_group.target_id LIMIT 1)');
CALL aggregates_from_like ('post', 'target_aggregates.creator_id'); CALL r.aggregates_from_like ('post', 'target_aggregates.creator_id');
COMMIT; COMMIT;