Update replaceable_schema.sql

This commit is contained in:
dullbananas 2023-12-27 20:51:31 -07:00 committed by GitHub
parent 6093f341d6
commit ebfe60cfdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,18 +42,34 @@ CREATE FUNCTION r.combine_transition_tables ()
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
RETURN QUERY IF (TG_OP = 'UPDATE') THEN
SELECT RETURN QUERY
-1 AS count_diff, SELECT
* -1 AS count_diff,
FROM *
old_table FROM
UNION ALL old_table
SELECT UNION ALL
1 AS count_diff, SELECT
* 1 AS count_diff,
FROM *
new_table; FROM
new_table;
ELSIF (TG_OP = 'INSERT') THEN
RETURN QUERY
SELECT
1 AS count_diff,
*
FROM
new_table;
ELSE
RETURN QUERY
SELECT
-1 AS count_diff,
*
FROM
old_table;
END IF;
END END
$$; $$;
@ -156,10 +172,7 @@ BEGIN
a.person_id = diff.creator_id; a.person_id = diff.creator_id;
RETURN NULL; RETURN NULL;
END $$; END $$;
CREATE TRIGGER aggregates CALL r.create_triggers ('thing_like', 'thing_aggregates_from_like');
AFTER INSERT OR DELETE OR UPDATE OF score ON thing_like REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_aggregates_from_like ( );
$b$, $b$,
'thing', 'thing',
thing_type); thing_type);
@ -272,11 +285,7 @@ WHERE
END END
$$; $$;
CREATE TRIGGER parent_aggregates CALL r.create_triggers ('comment', 'parent_aggregates_from_comment');
AFTER INSERT OR DELETE OR UPDATE OF deleted,
removed ON comment REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.parent_aggregates_from_comment ();
CREATE FUNCTION r.parent_aggregates_from_post () CREATE FUNCTION r.parent_aggregates_from_post ()
RETURNS TRIGGER RETURNS TRIGGER
@ -330,11 +339,7 @@ WHERE
END END
$$; $$;
CREATE TRIGGER parent_aggregates CALL r.create_triggers('post', 'parent_aggregates_from_post');
AFTER INSERT OR DELETE OR UPDATE OF deleted,
removed ON comment REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.parent_aggregates_from_comment ();
CREATE FUNCTION site_aggregates_from_community () CREATE FUNCTION site_aggregates_from_community ()
RETURNS TRIGGER RETURNS TRIGGER
@ -357,11 +362,7 @@ BEGIN
RETURN NULL; RETURN NULL;
$$; $$;
CREATE TRIGGER site_aggregates CALL rcreate_triggers ('community', 'site_aggregates_from_community');
AFTER INSERT OR DELETE OR UPDATE OF deleted,
removed ON community REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.site_aggregates_from_community ();
CREATE FUNCTION site_aggregates_from_person () CREATE FUNCTION site_aggregates_from_person ()
RETURNS TRIGGER RETURNS TRIGGER
@ -382,10 +383,7 @@ BEGIN
RETURN NULL; RETURN NULL;
$$; $$;
CREATE TRIGGER site_aggregates CALL r.create_triggers ('person', 'site_aggregates_from_person');
AFTER INSERT OR DELETE ON person REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.site_aggregates_from_person ();
-- For community_aggregates.comments, don't include comments of deleted or removed posts -- For community_aggregates.comments, don't include comments of deleted or removed posts
CREATE FUNCTION r.update_comment_count_from_post () CREATE FUNCTION r.update_comment_count_from_post ()
@ -466,10 +464,7 @@ WHERE
END END
$$; $$;
CREATE TRIGGER community_aggregates CALL r.create_triggers('community_follower', 'community_aggregates_from_subscriber');
AFTER INSERT OR DELETE ON community_follower REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION r.community_aggregates_from_subscriber ();
-- 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.
@ -557,21 +552,39 @@ BEGIN
community community
WHERE WHERE
community.id = new_post.community_id community.id = new_post.community_id
LIMIT 1) AS community, LIMIT 1) AS community;
ON CONFLICT RETURN NULL;
DO UPDATE SET END
featured_community = excluded.featured_community, $$;
featured_local = excluded.featured_local;
CREATE FUNCTION r.post_aggregates_from_post_update ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
post_aggregates
SET
featured_community = new_post.featured_community,
featured_local = new_post.featured_local
FROM
new_post
WHERE
post_aggregates.post_id = new_post.id;
RETURN NULL; RETURN NULL;
END END
$$; $$;
CREATE TRIGGER aggregates CREATE TRIGGER aggregates
AFTER INSERT OR UPDATE OF featured_community, AFTER INSERT ON post 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 TRIGGER aggregates_update
AFTER UPDATE ON post REFERENCING NEW TABLE AS new_post
FOR EACH STATEMENT
EXECUTE FUNCTION r.post_aggregates_from_post_update ();
CREATE FUNCTION r.site_aggregates_from_site () CREATE FUNCTION r.site_aggregates_from_site ()
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE plpgsql LANGUAGE plpgsql