mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-29 07:41:20 +00:00
stuff
This commit is contained in:
parent
25afdb2f96
commit
0f200435d7
5 changed files with 330 additions and 239 deletions
5
.pg_format
Normal file
5
.pg_format
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Keep normal formatting between `BEGIN` and `COMMIT` statements
|
||||||
|
nogrouping=1
|
||||||
|
|
||||||
|
# Don't add spaces after % in dollar-quoted function body when using `format`
|
||||||
|
placeholder=.%.
|
|
@ -1,6 +1,7 @@
|
||||||
DROP SCHEMA IF EXISTS r CASCADE;
|
DROP SCHEMA IF EXISTS r CASCADE;
|
||||||
|
|
||||||
CREATE FUNCTION comment_aggregates_comment() RETURNS trigger
|
CREATE FUNCTION comment_aggregates_comment ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -15,7 +16,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION comment_aggregates_score() RETURNS trigger
|
CREATE FUNCTION comment_aggregates_score ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -80,7 +82,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION comment_removed_resolve_reports() RETURNS trigger
|
CREATE FUNCTION comment_removed_resolve_reports ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -96,7 +99,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION community_aggregates_comment_count() RETURNS trigger
|
CREATE FUNCTION community_aggregates_comment_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -125,7 +129,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION community_aggregates_community() RETURNS trigger
|
CREATE FUNCTION community_aggregates_community ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -140,7 +145,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION community_aggregates_post_count() RETURNS trigger
|
CREATE FUNCTION community_aggregates_post_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -215,19 +221,31 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION community_aggregates_post_count_insert() RETURNS trigger
|
CREATE FUNCTION community_aggregates_post_count_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
UPDATE community_aggregates
|
UPDATE
|
||||||
SET posts = posts + post_group.count
|
community_aggregates
|
||||||
FROM (SELECT community_id, count(*) FROM new_post GROUP BY community_id) post_group
|
SET
|
||||||
WHERE community_aggregates.community_id = post_group.community_id;
|
posts = posts + post_group.count
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
community_id,
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
new_post
|
||||||
|
GROUP BY
|
||||||
|
community_id) post_group
|
||||||
|
WHERE
|
||||||
|
community_aggregates.community_id = post_group.community_id;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION community_aggregates_subscriber_count() RETURNS trigger
|
CREATE FUNCTION community_aggregates_subscriber_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -258,7 +276,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_comment_count() RETURNS trigger
|
CREATE FUNCTION person_aggregates_comment_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -281,7 +300,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_comment_score() RETURNS trigger
|
CREATE FUNCTION person_aggregates_comment_score ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -311,7 +331,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_person() RETURNS trigger
|
CREATE FUNCTION person_aggregates_person ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -326,7 +347,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_post_count() RETURNS trigger
|
CREATE FUNCTION person_aggregates_post_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -349,19 +371,31 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_post_insert() RETURNS trigger
|
CREATE FUNCTION person_aggregates_post_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
UPDATE person_aggregates
|
UPDATE
|
||||||
SET post_count = post_count + post_group.count
|
person_aggregates
|
||||||
FROM (SELECT creator_id, count(*) FROM new_post GROUP BY creator_id) post_group
|
SET
|
||||||
WHERE person_aggregates.person_id = post_group.creator_id;
|
post_count = post_count + post_group.count
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
creator_id,
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
new_post
|
||||||
|
GROUP BY
|
||||||
|
creator_id) post_group
|
||||||
|
WHERE
|
||||||
|
person_aggregates.person_id = post_group.creator_id;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION person_aggregates_post_score() RETURNS trigger
|
CREATE FUNCTION person_aggregates_post_score ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -391,7 +425,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_aggregates_comment_count() RETURNS trigger
|
CREATE FUNCTION post_aggregates_comment_count ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -444,7 +479,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_aggregates_featured_community() RETURNS trigger
|
CREATE FUNCTION post_aggregates_featured_community ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -458,7 +494,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_aggregates_featured_local() RETURNS trigger
|
CREATE FUNCTION post_aggregates_featured_local ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -472,7 +509,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_aggregates_post() RETURNS trigger
|
CREATE FUNCTION post_aggregates_post ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -484,14 +522,22 @@ BEGIN
|
||||||
published,
|
published,
|
||||||
community_id,
|
community_id,
|
||||||
creator_id,
|
creator_id,
|
||||||
(SELECT community.instance_id FROM community WHERE community.id = community_id LIMIT 1)
|
(
|
||||||
|
SELECT
|
||||||
|
community.instance_id
|
||||||
FROM
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
community.id = community_id
|
||||||
|
LIMIT 1)
|
||||||
|
FROM
|
||||||
new_post;
|
new_post;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_aggregates_score() RETURNS trigger
|
CREATE FUNCTION post_aggregates_score ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -556,7 +602,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION post_removed_resolve_reports() RETURNS trigger
|
CREATE FUNCTION post_removed_resolve_reports ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -572,7 +619,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_comment_delete() RETURNS trigger
|
CREATE FUNCTION site_aggregates_comment_delete ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -590,7 +638,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_comment_insert() RETURNS trigger
|
CREATE FUNCTION site_aggregates_comment_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -608,7 +657,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_community_insert() RETURNS trigger
|
CREATE FUNCTION site_aggregates_community_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -626,7 +676,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_person_delete() RETURNS trigger
|
CREATE FUNCTION site_aggregates_person_delete ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -643,7 +694,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_person_insert() RETURNS trigger
|
CREATE FUNCTION site_aggregates_person_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -655,7 +707,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_post_delete() RETURNS trigger
|
CREATE FUNCTION site_aggregates_post_delete ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -673,14 +726,19 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_post_insert() RETURNS trigger
|
CREATE FUNCTION site_aggregates_post_insert ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
UPDATE
|
UPDATE
|
||||||
site_aggregates sa
|
site_aggregates sa
|
||||||
SET
|
SET
|
||||||
posts = posts + (SELECT count(*) FROM new_post)
|
posts = posts + (
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
new_post)
|
||||||
FROM
|
FROM
|
||||||
site s
|
site s
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -689,7 +747,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_post_update() RETURNS trigger
|
CREATE FUNCTION site_aggregates_post_update ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -707,7 +766,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_site() RETURNS trigger
|
CREATE FUNCTION site_aggregates_site ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -729,7 +789,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION was_removed_or_deleted(tg_op text, old record, new record) RETURNS boolean
|
CREATE FUNCTION was_removed_or_deleted (tg_op text, old record, new record)
|
||||||
|
RETURNS boolean
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -747,7 +808,8 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION was_restored_or_created(tg_op text, old record, new record) RETURNS boolean
|
CREATE FUNCTION was_restored_or_created (tg_op text, old record, new record)
|
||||||
|
RETURNS boolean
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -765,63 +827,169 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER comment_aggregates_comment AFTER INSERT OR DELETE ON comment FOR EACH ROW EXECUTE FUNCTION comment_aggregates_comment();
|
CREATE TRIGGER comment_aggregates_comment
|
||||||
|
AFTER INSERT OR DELETE ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION comment_aggregates_comment ();
|
||||||
|
|
||||||
CREATE TRIGGER comment_aggregates_score AFTER INSERT OR DELETE ON comment_like FOR EACH ROW EXECUTE FUNCTION comment_aggregates_score();
|
CREATE TRIGGER comment_aggregates_score
|
||||||
|
AFTER INSERT OR DELETE ON comment_like
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION comment_aggregates_score ();
|
||||||
|
|
||||||
CREATE TRIGGER comment_removed_resolve_reports AFTER INSERT ON mod_remove_comment FOR EACH ROW WHEN (new.removed) EXECUTE FUNCTION comment_removed_resolve_reports();
|
CREATE TRIGGER comment_removed_resolve_reports
|
||||||
|
AFTER INSERT ON mod_remove_comment
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN (new.removed)
|
||||||
|
EXECUTE FUNCTION comment_removed_resolve_reports ();
|
||||||
|
|
||||||
CREATE TRIGGER community_aggregates_comment_count AFTER INSERT OR DELETE OR UPDATE OF removed, deleted ON comment FOR EACH ROW EXECUTE FUNCTION community_aggregates_comment_count();
|
CREATE TRIGGER community_aggregates_comment_count
|
||||||
|
AFTER INSERT OR DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION community_aggregates_comment_count ();
|
||||||
|
|
||||||
CREATE TRIGGER community_aggregates_community AFTER INSERT OR DELETE ON community FOR EACH ROW EXECUTE FUNCTION community_aggregates_community();
|
CREATE TRIGGER community_aggregates_community
|
||||||
|
AFTER INSERT OR DELETE ON community
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION community_aggregates_community ();
|
||||||
|
|
||||||
CREATE TRIGGER community_aggregates_post_count AFTER DELETE OR UPDATE OF removed, deleted ON post FOR EACH ROW EXECUTE FUNCTION community_aggregates_post_count();
|
CREATE TRIGGER community_aggregates_post_count
|
||||||
|
AFTER DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION community_aggregates_post_count ();
|
||||||
|
|
||||||
CREATE TRIGGER community_aggregates_post_count_insert AFTER INSERT ON post REFERENCING NEW TABLE AS new_post FOR EACH STATEMENT EXECUTE FUNCTION community_aggregates_post_count_insert();
|
CREATE TRIGGER community_aggregates_post_count_insert
|
||||||
|
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION community_aggregates_post_count_insert ();
|
||||||
|
|
||||||
CREATE TRIGGER community_aggregates_subscriber_count AFTER INSERT OR DELETE ON community_follower FOR EACH ROW EXECUTE FUNCTION community_aggregates_subscriber_count();
|
CREATE TRIGGER community_aggregates_subscriber_count
|
||||||
|
AFTER INSERT OR DELETE ON community_follower
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION community_aggregates_subscriber_count ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_comment_count AFTER INSERT OR DELETE OR UPDATE OF removed, deleted ON comment FOR EACH ROW EXECUTE FUNCTION person_aggregates_comment_count();
|
CREATE TRIGGER person_aggregates_comment_count
|
||||||
|
AFTER INSERT OR DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION person_aggregates_comment_count ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_comment_score AFTER INSERT OR DELETE ON comment_like FOR EACH ROW EXECUTE FUNCTION person_aggregates_comment_score();
|
CREATE TRIGGER person_aggregates_comment_score
|
||||||
|
AFTER INSERT OR DELETE ON comment_like
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION person_aggregates_comment_score ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_person AFTER INSERT OR DELETE ON person FOR EACH ROW EXECUTE FUNCTION person_aggregates_person();
|
CREATE TRIGGER person_aggregates_person
|
||||||
|
AFTER INSERT OR DELETE ON person
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION person_aggregates_person ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_post_count AFTER DELETE OR UPDATE OF removed, deleted ON post FOR EACH ROW EXECUTE FUNCTION person_aggregates_post_count();
|
CREATE TRIGGER person_aggregates_post_count
|
||||||
|
AFTER DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION person_aggregates_post_count ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_post_insert AFTER INSERT ON post REFERENCING NEW TABLE AS new_post FOR EACH STATEMENT EXECUTE FUNCTION person_aggregates_post_insert();
|
CREATE TRIGGER person_aggregates_post_insert
|
||||||
|
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION person_aggregates_post_insert ();
|
||||||
|
|
||||||
CREATE TRIGGER person_aggregates_post_score AFTER INSERT OR DELETE ON post_like FOR EACH ROW EXECUTE FUNCTION person_aggregates_post_score();
|
CREATE TRIGGER person_aggregates_post_score
|
||||||
|
AFTER INSERT OR DELETE ON post_like
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION person_aggregates_post_score ();
|
||||||
|
|
||||||
CREATE TRIGGER post_aggregates_comment_count AFTER INSERT OR DELETE OR UPDATE OF removed, deleted ON comment FOR EACH ROW EXECUTE FUNCTION post_aggregates_comment_count();
|
CREATE TRIGGER post_aggregates_comment_count
|
||||||
|
AFTER INSERT OR DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION post_aggregates_comment_count ();
|
||||||
|
|
||||||
CREATE TRIGGER post_aggregates_featured_community AFTER UPDATE ON post FOR EACH ROW WHEN ((old.featured_community IS DISTINCT FROM new.featured_community)) EXECUTE FUNCTION post_aggregates_featured_community();
|
CREATE TRIGGER post_aggregates_featured_community
|
||||||
|
AFTER UPDATE ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((old.featured_community IS DISTINCT FROM new.featured_community))
|
||||||
|
EXECUTE FUNCTION post_aggregates_featured_community ();
|
||||||
|
|
||||||
CREATE TRIGGER post_aggregates_featured_local AFTER UPDATE ON post FOR EACH ROW WHEN ((old.featured_local IS DISTINCT FROM new.featured_local)) EXECUTE FUNCTION post_aggregates_featured_local();
|
CREATE TRIGGER post_aggregates_featured_local
|
||||||
|
AFTER UPDATE ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((old.featured_local IS DISTINCT FROM new.featured_local))
|
||||||
|
EXECUTE FUNCTION post_aggregates_featured_local ();
|
||||||
|
|
||||||
CREATE TRIGGER post_aggregates_post AFTER INSERT ON post REFERENCING NEW TABLE AS new_post FOR EACH STATEMENT EXECUTE FUNCTION post_aggregates_post();
|
CREATE TRIGGER post_aggregates_post
|
||||||
|
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION post_aggregates_post ();
|
||||||
|
|
||||||
CREATE TRIGGER post_aggregates_score AFTER INSERT OR DELETE ON post_like FOR EACH ROW EXECUTE FUNCTION post_aggregates_score();
|
CREATE TRIGGER post_aggregates_score
|
||||||
|
AFTER INSERT OR DELETE ON post_like
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION post_aggregates_score ();
|
||||||
|
|
||||||
CREATE TRIGGER post_removed_resolve_reports AFTER INSERT ON mod_remove_post FOR EACH ROW WHEN (new.removed) EXECUTE FUNCTION post_removed_resolve_reports();
|
CREATE TRIGGER post_removed_resolve_reports
|
||||||
|
AFTER INSERT ON mod_remove_post
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN (new.removed)
|
||||||
|
EXECUTE FUNCTION post_removed_resolve_reports ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_comment_delete AFTER DELETE OR UPDATE OF removed, deleted ON comment FOR EACH ROW WHEN ((old.local = true)) EXECUTE FUNCTION site_aggregates_comment_delete();
|
CREATE TRIGGER site_aggregates_comment_delete
|
||||||
|
AFTER DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((old.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_comment_delete ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_comment_insert AFTER INSERT OR UPDATE OF removed, deleted ON comment FOR EACH ROW WHEN ((new.local = true)) EXECUTE FUNCTION site_aggregates_comment_insert();
|
CREATE TRIGGER site_aggregates_comment_insert
|
||||||
|
AFTER INSERT OR UPDATE OF removed,
|
||||||
|
deleted ON comment
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((new.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_comment_insert ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_community_insert AFTER INSERT OR UPDATE OF removed, deleted ON community FOR EACH ROW WHEN ((new.local = true)) EXECUTE FUNCTION site_aggregates_community_insert();
|
CREATE TRIGGER site_aggregates_community_insert
|
||||||
|
AFTER INSERT OR UPDATE OF removed,
|
||||||
|
deleted ON community
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((new.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_community_insert ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_person_delete AFTER DELETE ON person FOR EACH ROW WHEN ((old.local = true)) EXECUTE FUNCTION site_aggregates_person_delete();
|
CREATE TRIGGER site_aggregates_person_delete
|
||||||
|
AFTER DELETE ON person
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((old.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_person_delete ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_person_insert AFTER INSERT ON person FOR EACH ROW WHEN ((new.local = true)) EXECUTE FUNCTION site_aggregates_person_insert();
|
CREATE TRIGGER site_aggregates_person_insert
|
||||||
|
AFTER INSERT ON person
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((new.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_person_insert ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_post_delete AFTER DELETE OR UPDATE OF removed, deleted ON post FOR EACH ROW WHEN ((old.local = true)) EXECUTE FUNCTION site_aggregates_post_delete();
|
CREATE TRIGGER site_aggregates_post_delete
|
||||||
|
AFTER DELETE OR UPDATE OF removed,
|
||||||
|
deleted ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((old.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_post_delete ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_post_insert AFTER INSERT ON post REFERENCING NEW TABLE AS new_post FOR EACH STATEMENT EXECUTE FUNCTION site_aggregates_post_insert();
|
CREATE TRIGGER site_aggregates_post_insert
|
||||||
|
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION site_aggregates_post_insert ();
|
||||||
|
|
||||||
|
CREATE TRIGGER site_aggregates_post_update
|
||||||
|
AFTER UPDATE OF removed,
|
||||||
|
deleted ON post
|
||||||
|
FOR EACH ROW
|
||||||
|
WHEN ((new.local = TRUE))
|
||||||
|
EXECUTE FUNCTION site_aggregates_post_update ();
|
||||||
|
|
||||||
|
CREATE TRIGGER site_aggregates_site
|
||||||
|
AFTER INSERT OR DELETE ON site
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION site_aggregates_site ();
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_post_update AFTER UPDATE OF removed, deleted ON post FOR EACH ROW WHEN ((new.local = true)) EXECUTE FUNCTION site_aggregates_post_update();
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TRIGGER site_aggregates_site AFTER INSERT OR DELETE ON site FOR EACH ROW EXECUTE FUNCTION site_aggregates_site();
|
|
3
migrations/2023-12-25-034523_replaceable-schema/up.sql
Normal file
3
migrations/2023-12-25-034523_replaceable-schema/up.sql
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
-- Drop functions and use `CASCADE` to drop the triggers that use them
|
||||||
|
DROP FUNCTION comment_aggregates_comment, comment_aggregates_score, comment_removed_resolve_reports, community_aggregates_comment_count, community_aggregates_community, community_aggregates_post_count, community_aggregates_post_count_insert, community_aggregates_subscriber_count, person_aggregates_comment_count, person_aggregates_comment_score, person_aggregates_person, person_aggregates_post_count, person_aggregates_post_insert, person_aggregates_post_score, post_aggregates_comment_count, post_aggregates_featured_community, post_aggregates_featured_local, post_aggregates_post, post_aggregates_score, post_removed_resolve_reports, site_aggregates_comment_delete, site_aggregates_comment_insert, site_aggregates_community_insert, site_aggregates_person_delete, site_aggregates_person_insert, site_aggregates_post_delete, site_aggregates_post_insert, site_aggregates_post_update, site_aggregates_site, was_removed_or_deleted, was_restored_or_created CASCADE;
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
-- Drop functions and use `CASCADE` to drop the triggers that use them
|
|
||||||
DROP FUNCTION
|
|
||||||
comment_aggregates_comment,
|
|
||||||
comment_aggregates_score,
|
|
||||||
comment_removed_resolve_reports,
|
|
||||||
community_aggregates_comment_count,
|
|
||||||
community_aggregates_community,
|
|
||||||
community_aggregates_post_count,
|
|
||||||
community_aggregates_post_count_insert,
|
|
||||||
community_aggregates_subscriber_count,
|
|
||||||
person_aggregates_comment_count,
|
|
||||||
person_aggregates_comment_score,
|
|
||||||
person_aggregates_person,
|
|
||||||
person_aggregates_post_count,
|
|
||||||
person_aggregates_post_insert,
|
|
||||||
person_aggregates_post_score,
|
|
||||||
post_aggregates_comment_count,
|
|
||||||
post_aggregates_featured_community,
|
|
||||||
post_aggregates_featured_local,
|
|
||||||
post_aggregates_post,
|
|
||||||
post_aggregates_score,
|
|
||||||
post_removed_resolve_reports,
|
|
||||||
site_aggregates_comment_delete,
|
|
||||||
site_aggregates_comment_insert,
|
|
||||||
site_aggregates_community_insert,
|
|
||||||
site_aggregates_person_delete,
|
|
||||||
site_aggregates_person_insert,
|
|
||||||
site_aggregates_post_delete,
|
|
||||||
site_aggregates_post_insert,
|
|
||||||
site_aggregates_post_update,
|
|
||||||
site_aggregates_site,
|
|
||||||
was_removed_or_deleted,
|
|
||||||
was_restored_or_created CASCADE;
|
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
-- If you add something here that depends on something (such as a table) created in a new migration, then down.sql must use
|
-- If you add something here that depends on something (such as a table) created in a new migration, then down.sql must use
|
||||||
-- `CASCADE` when dropping it. This doesn't need to be fixed in old migrations because the "replaceable-schema" migration
|
-- `CASCADE` when dropping it. This doesn't need to be fixed in old migrations because the "replaceable-schema" migration
|
||||||
-- runs `DROP SCHEMA IF EXISTS r CASCADE` in down.sql.
|
-- runs `DROP SCHEMA IF EXISTS r CASCADE` in down.sql.
|
||||||
|
|
||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
DROP SCHEMA IF EXISTS r CASCADE;
|
DROP SCHEMA IF EXISTS r CASCADE;
|
||||||
|
@ -19,7 +18,6 @@ DROP SCHEMA IF EXISTS r CASCADE;
|
||||||
CREATE SCHEMA r;
|
CREATE SCHEMA r;
|
||||||
|
|
||||||
-- Rank calculations
|
-- Rank calculations
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION r.controversy_rank (upvotes numeric, downvotes numeric)
|
CREATE OR REPLACE FUNCTION r.controversy_rank (upvotes numeric, downvotes numeric)
|
||||||
RETURNS float
|
RETURNS float
|
||||||
AS $$
|
AS $$
|
||||||
|
@ -57,37 +55,29 @@ CREATE FUNCTION r.combine_transition_tables ()
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- 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 PROCEDURE r.resolve_reports_when_target_removed (target_name text)
|
CREATE PROCEDURE r.resolve_reports_when_target_removed (target_name text)
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $a$
|
AS $a$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE format($b$
|
EXECUTE format($b$ CREATE FUNCTION r.resolve_reports_when_%1 $ s_removed ( )
|
||||||
CREATE FUNCTION r.resolve_reports_when_%1$s_removed ()
|
RETURNS TRIGGER
|
||||||
RETURNS trigger
|
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
UPDATE
|
UPDATE
|
||||||
%1$s_report AS report
|
%1$s_report AS report
|
||||||
SET
|
SET
|
||||||
resolved = TRUE,
|
resolved = TRUE, resolver_id = mod_person_id, updated = now()
|
||||||
resolver_id = mod_person_id,
|
FROM new_removal
|
||||||
updated = now()
|
|
||||||
FROM
|
|
||||||
new_removal
|
|
||||||
WHERE
|
WHERE
|
||||||
report.%1$s_id = new_removal.%1$a_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_%1$s
|
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 r.resolve_reports_when_%1$s_removed ();
|
EXECUTE FUNCTION r.resolve_reports_when_ %1 $ s_removed ( );
|
||||||
$b$,
|
$b$,
|
||||||
target_name);
|
target_name);
|
||||||
END
|
END
|
||||||
|
@ -99,21 +89,8 @@ CALL r.resolve_reports_when_target_removed ('post');
|
||||||
|
|
||||||
-- 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.
|
||||||
|
|
||||||
CALL r.upsert_aggregates ('comment', 'published', NULL);
|
|
||||||
|
|
||||||
CALL r.upsert_aggregates ('community', 'published', NULL);
|
|
||||||
|
|
||||||
CALL r.upsert_aggregates ('person', NULL, NULL);
|
|
||||||
|
|
||||||
CALL r.upsert_aggregates (
|
|
||||||
'post',
|
|
||||||
'published, newest_comment_time, newest_comment_time_necro, community_id, creator_id, instance_id, featured_community, featured_local',
|
|
||||||
'published AS newest_comment_time, published AS newest_comment_time_necro, (SELECT community.instance_id FROM community WHERE community.id = community_id LIMIT 1) AS instance_id'
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE FUNCTION r.comment_aggregates_from_comment ()
|
CREATE FUNCTION r.comment_aggregates_from_comment ()
|
||||||
RETURNS trigger
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -123,19 +100,17 @@ BEGIN
|
||||||
published
|
published
|
||||||
FROM
|
FROM
|
||||||
new_comment;
|
new_comment;
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
CREATE TRIGGER aggregates
|
||||||
AFTER INSERT ON comment
|
AFTER INSERT ON comment REFERENCING NEW TABLE AS new_comment
|
||||||
REFERENCING NEW TABLE AS new_comment
|
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.comment_aggregates_from_comment ();
|
EXECUTE FUNCTION r.comment_aggregates_from_comment ();
|
||||||
|
|
||||||
CREATE FUNCTION r.community_aggregates_from_community ()
|
CREATE FUNCTION r.community_aggregates_from_community ()
|
||||||
RETURNS trigger
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -145,19 +120,17 @@ BEGIN
|
||||||
published
|
published
|
||||||
FROM
|
FROM
|
||||||
new_community;
|
new_community;
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
CREATE TRIGGER aggregates
|
||||||
AFTER INSERT ON community
|
AFTER INSERT ON community REFERENCING NEW TABLE AS new_community
|
||||||
REFERENCING NEW TABLE AS new_community
|
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.community_aggregates_from_community ();
|
EXECUTE FUNCTION r.community_aggregates_from_community ();
|
||||||
|
|
||||||
CREATE FUNCTION r.person_aggregates_from_person ()
|
CREATE FUNCTION r.person_aggregates_from_person ()
|
||||||
RETURNS trigger
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -166,51 +139,50 @@ BEGIN
|
||||||
id,
|
id,
|
||||||
FROM
|
FROM
|
||||||
new_person;
|
new_person;
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
CREATE TRIGGER aggregates
|
||||||
AFTER INSERT ON person
|
AFTER INSERT ON person REFERENCING NEW TABLE AS new_person
|
||||||
REFERENCING NEW TABLE AS new_person
|
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.person_aggregates_from_person ();
|
EXECUTE FUNCTION r.person_aggregates_from_person ();
|
||||||
|
|
||||||
CREATE FUNCTION r.post_aggregates_from_post ()
|
CREATE FUNCTION r.post_aggregates_from_post ()
|
||||||
RETURNS trigger
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro, community_id, creator_id, instance_id, featured_community, featured_local)
|
INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro, community_id, creator_id, instance_id, featured_community, featured_local)
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
new_post.id,
|
||||||
published,
|
new_post.published,
|
||||||
published,
|
new_post.published,
|
||||||
published,
|
new_post.published,
|
||||||
community_id,
|
new_post.community_id,
|
||||||
creator_id,
|
new_post.creator_id,
|
||||||
(SELECT community.instance_id FROM community WHERE community.id = community_id LIMIT 1),
|
community.instance_id,
|
||||||
featured_community,
|
new_post.featured_community,
|
||||||
featured_local
|
new_post.featured_local
|
||||||
FROM
|
FROM
|
||||||
new_post
|
new_post
|
||||||
ON CONFLICT DO UPDATE SET
|
INNER JOIN community ON community.id = new_post.community_id
|
||||||
|
ON CONFLICT
|
||||||
|
DO UPDATE SET
|
||||||
featured_community = excluded.featured_community,
|
featured_community = excluded.featured_community,
|
||||||
featured_local = excluded.featured_local;
|
featured_local = excluded.featured_local;
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
CREATE TRIGGER aggregates
|
||||||
AFTER INSERT OR UPDATE OF featured_community, featured_local ON post
|
AFTER INSERT OR UPDATE OF featured_community,
|
||||||
REFERENCING NEW TABLE AS new_post
|
featured_local ON post REFERENCING NEW TABLE AS new_post
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.post_aggregates_from_post ();
|
EXECUTE FUNCTION r.post_aggregates_from_post ();
|
||||||
|
|
||||||
CREATE FUNCTION r.site_aggregates_from_site ()
|
CREATE FUNCTION r.site_aggregates_from_site ()
|
||||||
RETURNS trigger
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -221,11 +193,8 @@ BEGIN
|
||||||
1
|
1
|
||||||
FROM
|
FROM
|
||||||
site_aggregates) THEN
|
site_aggregates) THEN
|
||||||
INSERT INTO
|
INSERT INTO site_aggregates (site_id)
|
||||||
site_aggregates (site_id)
|
VALUES (NEW.id);
|
||||||
VALUES
|
|
||||||
(NEW.id);
|
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
@ -236,46 +205,31 @@ CREATE TRIGGER aggregates
|
||||||
EXECUTE FUNCTION r.site_aggregates_from_site ();
|
EXECUTE FUNCTION r.site_aggregates_from_site ();
|
||||||
|
|
||||||
-- These triggers update aggregates in response to votes.
|
-- These triggers update aggregates in response to votes.
|
||||||
|
|
||||||
CREATE PROCEDURE r.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 r.%1$s_aggregates_from_like ( )
|
||||||
CREATE FUNCTION r.%1$s_aggregates_from_like ()
|
RETURNS TRIGGER
|
||||||
RETURNS trigger
|
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
WITH
|
WITH
|
||||||
-- Update aggregates for target
|
-- Update aggregates for target
|
||||||
target_diff (creator_id, score) AS (
|
target_diff (
|
||||||
UPDATE
|
creator_id, score
|
||||||
|
) AS ( UPDATE
|
||||||
%1$s_aggregates AS target_aggregates
|
%1$s_aggregates AS target_aggregates
|
||||||
SET
|
SET
|
||||||
score = score + diff.upvotes - diff.downvotes,
|
score = score + diff.upvotes - diff.downvotes, upvotes = upvotes + diff.upvotes, downvotes = downvotes + diff.downvotes, controversy_rank = controversy_rank ((upvotes + diff.upvotes)::numeric, (downvotes + diff.downvotes)::numeric)
|
||||||
upvotes = upvotes + diff.upvotes,
|
|
||||||
downvotes = downvotes + diff.downvotes,
|
|
||||||
controversy_rank = controversy_rank (
|
|
||||||
(upvotes + diff.upvotes)::numeric,
|
|
||||||
(downvotes + diff.downvotes)::numeric
|
|
||||||
)
|
|
||||||
FROM (
|
FROM (
|
||||||
SELECT
|
SELECT
|
||||||
target_id,
|
target_id, sum(count_diff) FILTER (WHERE score = 1) AS upvotes, sum(count_diff) FILTER (WHERE score <> 1) AS downvotes FROM r.combine_transition_tables ()
|
||||||
sum(count_diff) FILTER (WHERE score = 1) AS upvotes,
|
GROUP BY target_id) AS diff
|
||||||
sum(count_diff) FILTER (WHERE score <> 1) AS downvotes
|
|
||||||
FROM
|
|
||||||
r.combine_transition_tables ()
|
|
||||||
GROUP BY
|
|
||||||
target_id
|
|
||||||
) AS diff
|
|
||||||
WHERE
|
WHERE
|
||||||
target_aggregates.comment_id = diff.target_id
|
target_aggregates.comment_id = diff.target_id
|
||||||
RETURNING
|
RETURNING
|
||||||
%2$s,
|
%2$s, diff.upvotes - diff.downvotes)
|
||||||
diff.upvotes - diff.downvotes
|
|
||||||
)
|
|
||||||
-- Update aggregates for target's creator
|
-- Update aggregates for target's creator
|
||||||
UPDATE
|
UPDATE
|
||||||
person_aggregates
|
person_aggregates
|
||||||
|
@ -288,18 +242,13 @@ BEGIN
|
||||||
FROM
|
FROM
|
||||||
target_diff
|
target_diff
|
||||||
GROUP BY
|
GROUP BY
|
||||||
creator_id
|
creator_id) AS diff
|
||||||
) AS diff
|
|
||||||
WHERE
|
WHERE
|
||||||
person_aggregates.person_id = diff.creator_id;
|
person_aggregates.person_id = diff.creator_id;
|
||||||
|
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END $$;
|
||||||
$$;
|
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
CREATE TRIGGER aggregates
|
||||||
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_table NEW TABLE AS new_table
|
||||||
REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
|
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.%1$s_aggregates_from_like;
|
EXECUTE FUNCTION r.%1$s_aggregates_from_like;
|
||||||
$b$,
|
$b$,
|
||||||
|
|
Loading…
Reference in a new issue