Update up.sql

This commit is contained in:
dullbananas 2023-12-20 16:42:04 -07:00 committed by GitHub
parent 23c64ea902
commit 465440491d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,8 +15,15 @@ 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
@ -27,10 +34,20 @@ CREATE OR REPLACE FUNCTION community_aggregates_post_count_insert ()
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
$$; $$;
@ -40,17 +57,26 @@ CREATE OR REPLACE FUNCTION person_aggregates_post_insert ()
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 OR REPLACE TRIGGER post_aggregates_post CREATE OR REPLACE TRIGGER post_aggregates_post
AFTER INSERT ON post AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
REFERENCING NEW TABLE AS new_post
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE post_aggregates_post (); EXECUTE PROCEDURE post_aggregates_post ();
@ -63,8 +89,7 @@ CREATE OR REPLACE TRIGGER community_aggregates_post_count
EXECUTE PROCEDURE community_aggregates_post_count (); EXECUTE PROCEDURE community_aggregates_post_count ();
CREATE OR REPLACE TRIGGER community_aggregates_post_count_insert CREATE OR REPLACE TRIGGER community_aggregates_post_count_insert
AFTER INSERT ON post AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
REFERENCING NEW TABLE AS new_post
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE community_aggregates_post_count_insert (); EXECUTE PROCEDURE community_aggregates_post_count_insert ();
@ -97,7 +122,11 @@ 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
@ -114,8 +143,7 @@ CREATE OR REPLACE TRIGGER site_aggregates_post_update
EXECUTE PROCEDURE site_aggregates_post_update (); EXECUTE PROCEDURE site_aggregates_post_update ();
CREATE OR REPLACE TRIGGER site_aggregates_post_insert CREATE OR REPLACE TRIGGER site_aggregates_post_insert
AFTER INSERT ON post AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
REFERENCING NEW TABLE AS new_post
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE site_aggregates_post_insert (); EXECUTE PROCEDURE site_aggregates_post_insert ();
@ -126,8 +154,7 @@ CREATE OR REPLACE TRIGGER person_aggregates_post_count
EXECUTE PROCEDURE person_aggregates_post_count (); EXECUTE PROCEDURE person_aggregates_post_count ();
CREATE OR REPLACE TRIGGER person_aggregates_post_insert CREATE OR REPLACE TRIGGER person_aggregates_post_insert
AFTER INSERT ON post AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
REFERENCING NEW TABLE AS new_post
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE person_aggregates_post_insert (); EXECUTE PROCEDURE person_aggregates_post_insert ();