mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-29 07:41:20 +00:00
Merge branch 'bliss' of https://github.com/dullbananas/lemmy into bliss
This commit is contained in:
commit
d67bd5f249
1 changed files with 125 additions and 113 deletions
|
@ -36,43 +36,104 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- Selects both old and new rows in a trigger. Column 1 is -1 if old and 1 if new, which can be used with `sum` to get
|
-- This function creates statement-level triggers for all operation types. It's designed this way
|
||||||
-- the number to add to a count. Column 2 is the original row as a composite value.
|
-- because of these limitations:
|
||||||
CREATE FUNCTION r.combine_transition_tables (tg_op text)
|
-- * A trigger that uses transition tables can only handle 1 operation type.
|
||||||
RETURNS SETOF record
|
-- * Transition tables must be relevant for the operation type (for example, `NEW TABLE` is
|
||||||
|
-- not allowed for a `DELETE` trigger)
|
||||||
|
-- * Transition tables are only provided to the trigger function, not to functions that it calls.
|
||||||
|
--
|
||||||
|
-- This function can only be called once per table. The command to run is given as the 2nd argument
|
||||||
|
-- and has access to these tables:
|
||||||
|
-- * `old_table` with old rows
|
||||||
|
-- * `new_table` with new rows
|
||||||
|
-- * `combined_transition_tables` with both old and new rows, with 2 columns:
|
||||||
|
-- 1. `-1` for old rows and `1` for new rows, which can be used with `sum` to get the number
|
||||||
|
-- to add to a count
|
||||||
|
-- 2. the old or new row as a composite value
|
||||||
|
CREATE PROCEDURE r.create_triggers (table_name text, command text)
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $a$
|
||||||
|
DECLARE
|
||||||
|
defs text := $b$
|
||||||
|
CREATE FUNCTION r.thing_delete_statement ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF (TG_OP IN ('UPDATE', 'DELETE')) THEN
|
PERFORM (
|
||||||
RETURN QUERY
|
WITH
|
||||||
|
combined_transition_tables AS (
|
||||||
|
select_old_table
|
||||||
|
),
|
||||||
|
trigger_result AS command
|
||||||
SELECT
|
SELECT
|
||||||
-1,
|
1
|
||||||
old_table
|
);
|
||||||
FROM
|
RETURN NULL;
|
||||||
old_table;
|
|
||||||
END IF;
|
|
||||||
IF (TG_OP IN ('UPDATE', 'INSERT')) THEN
|
|
||||||
RETURN QUERY
|
|
||||||
SELECT
|
|
||||||
1,
|
|
||||||
new_table
|
|
||||||
FROM
|
|
||||||
new_table;
|
|
||||||
END IF;
|
|
||||||
RETURN;
|
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
CREATE TRIGGER delete_statement
|
||||||
-- Creates triggers for all operation types, which can't be 1 trigger when transition tables are used
|
AFTER DELETE ON thing REFERENCING OLD TABLE AS old_table
|
||||||
CREATE PROCEDURE r.create_triggers (table_name text, function_name text)
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION r.thing_delete_statement ();
|
||||||
|
CREATE FUNCTION r.thing_insert_statement ()
|
||||||
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE format('CREATE TRIGGER %2$s_insert AFTER INSERT ON %1$s REFERENCING NEW TABLE AS new_table FOR EACH STATEMENT EXECUTE FUNCTION r.%2$s ();', table_name, function_name);
|
PERFORM (
|
||||||
EXECUTE format('CREATE TRIGGER %2$s_delete AFTER DELETE ON %1$s REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION r.%2$s ();', table_name, function_name);
|
WITH
|
||||||
EXECUTE format('CREATE TRIGGER %2$s_update AFTER UPDATE ON %1$s REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table FOR EACH STATEMENT EXECUTE FUNCTION r.%2$s ();', table_name, function_name);
|
combined_transition_tables AS (
|
||||||
|
select_new_table
|
||||||
|
),
|
||||||
|
trigger_result AS command
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
);
|
||||||
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
CREATE TRIGGER insert_statement
|
||||||
|
AFTER DELETE ON thing REFERENCING NEW TABLE AS new_table
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION r.thing_insert_statement ();
|
||||||
|
CREATE FUNCTION r.thing_update_statement ()
|
||||||
|
RETURNS TRIGGER
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $$
|
||||||
|
BEGIN
|
||||||
|
PERFORM (
|
||||||
|
WITH
|
||||||
|
combined_transition_tables AS (
|
||||||
|
select_old_table
|
||||||
|
UNION ALL
|
||||||
|
select_new_table
|
||||||
|
),
|
||||||
|
trigger_result AS command
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
);
|
||||||
|
RETURN NULL;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
CREATE TRIGGER update_statement
|
||||||
|
AFTER UPDATE ON thing REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION r.thing_update_statement ();
|
||||||
|
$b$;
|
||||||
|
BEGIN
|
||||||
|
defs := replace(defs, 'select_old_table', $$
|
||||||
|
SELECT -1 AS count_diff, old_table AS thing FROM old_table
|
||||||
|
$$);
|
||||||
|
defs := replace(defs, 'select_new_table', $$
|
||||||
|
SELECT 1 AS count_diff, new_table AS thing FROM new_table
|
||||||
|
$$);
|
||||||
|
defs := replace(defs, 'thing', table_name);
|
||||||
|
defs := replace(defs, 'command', format('(%s)', command));
|
||||||
|
EXECUTE defs;
|
||||||
|
END
|
||||||
|
$a$;
|
||||||
|
|
||||||
-- Define functions
|
-- Define functions
|
||||||
CREATE FUNCTION r.creator_id_from_post_aggregates (agg post_aggregates)
|
CREATE FUNCTION r.creator_id_from_post_aggregates (agg post_aggregates)
|
||||||
|
@ -119,11 +180,7 @@ BEGIN
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.resolve_reports_when_thing_removed ( );
|
EXECUTE FUNCTION r.resolve_reports_when_thing_removed ( );
|
||||||
-- When a thing gets a vote, update its aggregates and its creator's aggregates
|
-- When a thing gets a vote, update its aggregates and its creator's aggregates
|
||||||
CREATE FUNCTION r.thing_aggregates_from_like ( )
|
CALL r.create_triggers ('thing_like', $$
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
WITH thing_diff AS (
|
WITH thing_diff AS (
|
||||||
UPDATE
|
UPDATE
|
||||||
thing_aggregates AS a
|
thing_aggregates AS a
|
||||||
|
@ -138,7 +195,7 @@ BEGIN
|
||||||
sum(count_diff) FILTER (WHERE (thing_like).score = 1) AS upvotes,
|
sum(count_diff) FILTER (WHERE (thing_like).score = 1) AS upvotes,
|
||||||
sum(count_diff) FILTER (WHERE (thing_like).score != 1) AS downvotes
|
sum(count_diff) FILTER (WHERE (thing_like).score != 1) AS downvotes
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combined_transition_tables
|
||||||
AS (count_diff bigint,
|
AS (count_diff bigint,
|
||||||
thing_like thing_like)
|
thing_like thing_like)
|
||||||
GROUP BY
|
GROUP BY
|
||||||
|
@ -161,13 +218,8 @@ BEGIN
|
||||||
GROUP BY
|
GROUP BY
|
||||||
creator_id) AS diff
|
creator_id) AS diff
|
||||||
WHERE
|
WHERE
|
||||||
a.person_id = diff.creator_id;
|
a.person_id = diff.creator_id
|
||||||
RETURN NULL;
|
$$);
|
||||||
END $$;
|
|
||||||
CALL r.create_triggers ('thing_like', 'thing_aggregates_from_like');
|
|
||||||
$b$,
|
|
||||||
'thing',
|
|
||||||
thing_type);
|
|
||||||
END
|
END
|
||||||
$a$;
|
$a$;
|
||||||
|
|
||||||
|
@ -176,11 +228,7 @@ CALL r.post_or_comment ('post');
|
||||||
CALL r.post_or_comment ('comment');
|
CALL r.post_or_comment ('comment');
|
||||||
|
|
||||||
-- Create triggers that update counts in parent aggregates
|
-- Create triggers that update counts in parent aggregates
|
||||||
CREATE FUNCTION r.parent_aggregates_from_comment ()
|
CALL r.create_triggers('comment', $$
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
WITH comment_group AS (
|
WITH comment_group AS (
|
||||||
SELECT
|
SELECT
|
||||||
(comment).post_id,
|
(comment).post_id,
|
||||||
|
@ -188,7 +236,7 @@ BEGIN
|
||||||
(comment).local,
|
(comment).local,
|
||||||
sum(count_diff) AS comments
|
sum(count_diff) AS comments
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combined_transition_tables
|
||||||
AS (count_diff bigint,
|
AS (count_diff bigint,
|
||||||
comment comment)
|
comment comment)
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -272,18 +320,10 @@ FROM (
|
||||||
GROUP BY
|
GROUP BY
|
||||||
community_id) AS diff
|
community_id) AS diff
|
||||||
WHERE
|
WHERE
|
||||||
a.community_id = diff.community_id;
|
a.community_id = diff.community_id
|
||||||
RETURN NULL;
|
$$);
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CALL r.create_triggers ('comment', 'parent_aggregates_from_comment');
|
CALL r.create_triggers('post', $$
|
||||||
|
|
||||||
CREATE FUNCTION r.parent_aggregates_from_post ()
|
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
WITH post_group AS (
|
WITH post_group AS (
|
||||||
SELECT
|
SELECT
|
||||||
(post).community_id,
|
(post).community_id,
|
||||||
|
@ -291,7 +331,7 @@ BEGIN
|
||||||
(post).local,
|
(post).local,
|
||||||
sum(count_diff) AS posts
|
sum(count_diff) AS posts
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combined_post_transition_tables
|
||||||
AS (count_diff bigint,
|
AS (count_diff bigint,
|
||||||
post post)
|
post post)
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -327,18 +367,10 @@ SET
|
||||||
FROM
|
FROM
|
||||||
post_group
|
post_group
|
||||||
WHERE
|
WHERE
|
||||||
a.community_id = post_group.community_id;
|
a.community_id = post_group.community_id
|
||||||
RETURN NULL;
|
$$);
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CALL r.create_triggers ('post', 'parent_aggregates_from_post');
|
CALL r.create_triggers('community', $$
|
||||||
|
|
||||||
CREATE FUNCTION r.site_aggregates_from_community ()
|
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
UPDATE
|
UPDATE
|
||||||
site_aggregates AS a
|
site_aggregates AS a
|
||||||
SET
|
SET
|
||||||
|
@ -347,20 +379,12 @@ BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
sum(count_diff) AS communities
|
sum(count_diff) AS communities
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combined_transition_tables
|
||||||
AS (count_diff bigint, community community)
|
AS (count_diff bigint, community community)
|
||||||
WHERE (community).local AND NOT ((community).deleted OR (community).removed)) AS diff;
|
WHERE (community).local AND NOT ((community).deleted OR (community).removed)) AS diff
|
||||||
RETURN NULL;
|
$$);
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CALL r.create_triggers ('community', 'site_aggregates_from_community');
|
CALL r.create_triggers('person', $$
|
||||||
|
|
||||||
CREATE FUNCTION r.site_aggregates_from_person ()
|
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
UPDATE
|
UPDATE
|
||||||
site_aggregates AS a
|
site_aggregates AS a
|
||||||
SET
|
SET
|
||||||
|
@ -369,14 +393,10 @@ BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
sum(count_diff) AS users
|
sum(count_diff) AS users
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combined_transition_tables
|
||||||
AS (count_diff bigint, person person)
|
AS (count_diff bigint, person person)
|
||||||
WHERE (person).local) AS diff;
|
WHERE (person).local) AS diff
|
||||||
RETURN NULL;
|
$$);
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CALL r.create_triggers ('person', '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 ()
|
||||||
|
@ -426,11 +446,7 @@ CREATE TRIGGER comment_count
|
||||||
EXECUTE FUNCTION r.update_comment_count_from_post ();
|
EXECUTE FUNCTION r.update_comment_count_from_post ();
|
||||||
|
|
||||||
-- Count subscribers for local communities
|
-- Count subscribers for local communities
|
||||||
CREATE FUNCTION r.community_aggregates_from_subscriber ()
|
CALL r.create_triggers ('community_follower', $$
|
||||||
RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
UPDATE
|
UPDATE
|
||||||
community_aggregates AS a
|
community_aggregates AS a
|
||||||
SET
|
SET
|
||||||
|
@ -440,7 +456,7 @@ BEGIN
|
||||||
(community_follower).community_id,
|
(community_follower).community_id,
|
||||||
sum(count_diff) AS subscribers
|
sum(count_diff) AS subscribers
|
||||||
FROM
|
FROM
|
||||||
r.combine_transition_tables (TG_OP)
|
combine_transition_tables
|
||||||
AS (count_diff bigint, community_follower community_follower)
|
AS (count_diff bigint, community_follower community_follower)
|
||||||
WHERE (
|
WHERE (
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -453,12 +469,8 @@ BEGIN
|
||||||
GROUP BY
|
GROUP BY
|
||||||
(community_follower).community_id) AS diff
|
(community_follower).community_id) AS diff
|
||||||
WHERE
|
WHERE
|
||||||
a.community_id = diff.community_id;
|
a.community_id = diff.community_id
|
||||||
RETURN NULL;
|
$$);
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CALL r.create_triggers ('community_follower', '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.
|
||||||
|
@ -551,6 +563,11 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
CREATE TRIGGER aggregates
|
||||||
|
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
||||||
|
FOR EACH STATEMENT
|
||||||
|
EXECUTE FUNCTION r.post_aggregates_from_post ();
|
||||||
|
|
||||||
CREATE FUNCTION r.post_aggregates_from_post_update ()
|
CREATE FUNCTION r.post_aggregates_from_post_update ()
|
||||||
RETURNS TRIGGER
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
|
@ -569,11 +586,6 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER aggregates
|
|
||||||
AFTER INSERT ON post REFERENCING NEW TABLE AS new_post
|
|
||||||
FOR EACH STATEMENT
|
|
||||||
EXECUTE FUNCTION r.post_aggregates_from_post ();
|
|
||||||
|
|
||||||
CREATE TRIGGER aggregates_update
|
CREATE TRIGGER aggregates_update
|
||||||
AFTER UPDATE ON post REFERENCING NEW TABLE AS new_post
|
AFTER UPDATE ON post REFERENCING NEW TABLE AS new_post
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
|
|
Loading…
Reference in a new issue