mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-25 22:01:19 +00:00
fix some things
This commit is contained in:
parent
ebfe60cfdb
commit
4b9f4ce043
1 changed files with 30 additions and 28 deletions
|
@ -75,14 +75,14 @@ $$;
|
||||||
|
|
||||||
-- Creates triggers for all operation types, which can't be 1 trigger when transition tables are used
|
-- Creates triggers for all operation types, which can't be 1 trigger when transition tables are used
|
||||||
CREATE PROCEDURE r.create_triggers (table_name text, function_name text)
|
CREATE PROCEDURE r.create_triggers (table_name text, function_name text)
|
||||||
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);
|
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);
|
||||||
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);
|
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);
|
||||||
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);
|
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);
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- 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)
|
||||||
|
@ -194,9 +194,9 @@ BEGIN
|
||||||
post_id,
|
post_id,
|
||||||
creator_id,
|
creator_id,
|
||||||
local,
|
local,
|
||||||
sum(count_diff) AS comments,
|
sum(count_diff) AS comments
|
||||||
FROM
|
FROM
|
||||||
combine_transition_tables ()
|
r.combine_transition_tables ()
|
||||||
WHERE
|
WHERE
|
||||||
NOT (deleted
|
NOT (deleted
|
||||||
OR removed)
|
OR removed)
|
||||||
|
@ -235,8 +235,7 @@ post_diff AS (
|
||||||
max(published)
|
max(published)
|
||||||
FROM new_table AS new_comment
|
FROM new_table AS new_comment
|
||||||
WHERE
|
WHERE
|
||||||
a.post_id = new_comment.post_id)
|
a.post_id = new_comment.post_id LIMIT 1)),
|
||||||
LIMIT 1),
|
|
||||||
newest_comment_time_necro = GREATEST (a.newest_comment_time_necro, (
|
newest_comment_time_necro = GREATEST (a.newest_comment_time_necro, (
|
||||||
SELECT
|
SELECT
|
||||||
max(published)
|
max(published)
|
||||||
|
@ -297,9 +296,9 @@ BEGIN
|
||||||
community_id,
|
community_id,
|
||||||
creator_id,
|
creator_id,
|
||||||
local,
|
local,
|
||||||
sum(count_diff) AS posts,
|
sum(count_diff) AS posts
|
||||||
FROM
|
FROM
|
||||||
combine_transition_tables ()
|
r.combine_transition_tables ()
|
||||||
WHERE
|
WHERE
|
||||||
NOT (deleted
|
NOT (deleted
|
||||||
OR removed)
|
OR removed)
|
||||||
|
@ -339,9 +338,9 @@ WHERE
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CALL r.create_triggers('post', 'parent_aggregates_from_post');
|
CALL r.create_triggers ('post', 'parent_aggregates_from_post');
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_from_community ()
|
CREATE FUNCTION r.site_aggregates_from_community ()
|
||||||
RETURNS TRIGGER
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
|
@ -354,17 +353,18 @@ BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
sum(change_diff) AS communities
|
sum(change_diff) AS communities
|
||||||
FROM
|
FROM
|
||||||
combine_transition_tables ()
|
r.combine_transition_tables ()
|
||||||
WHERE
|
WHERE
|
||||||
local
|
local
|
||||||
AND NOT (deleted
|
AND NOT (deleted
|
||||||
OR removed)) AS diff;
|
OR removed)) AS diff;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CALL rcreate_triggers ('community', 'site_aggregates_from_community');
|
CALL r.create_triggers ('community', 'site_aggregates_from_community');
|
||||||
|
|
||||||
CREATE FUNCTION site_aggregates_from_person ()
|
CREATE FUNCTION r.site_aggregates_from_person ()
|
||||||
RETURNS TRIGGER
|
RETURNS TRIGGER
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
|
@ -377,10 +377,11 @@ BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
sum(change_diff) AS users
|
sum(change_diff) AS users
|
||||||
FROM
|
FROM
|
||||||
combine_transition_tables ()
|
r.combine_transition_tables ()
|
||||||
WHERE
|
WHERE
|
||||||
local) AS diff;
|
local) AS diff;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CALL r.create_triggers ('person', 'site_aggregates_from_person');
|
CALL r.create_triggers ('person', 'site_aggregates_from_person');
|
||||||
|
@ -424,11 +425,11 @@ BEGIN
|
||||||
WHERE
|
WHERE
|
||||||
a.community_id = diff.community_id;
|
a.community_id = diff.community_id;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TRIGGER comment_count
|
CREATE TRIGGER comment_count
|
||||||
AFTER UPDATE OF deleted,
|
AFTER UPDATE ON post REFERENCING OLD TABLE AS old_post NEW TABLE AS new_post
|
||||||
removed ON post REFERENCING OLD TABLE AS old_post NEW TABLE AS new_post
|
|
||||||
FOR EACH STATEMENT
|
FOR EACH STATEMENT
|
||||||
EXECUTE FUNCTION r.update_comment_count_from_post ();
|
EXECUTE FUNCTION r.update_comment_count_from_post ();
|
||||||
|
|
||||||
|
@ -447,7 +448,7 @@ BEGIN
|
||||||
community_id,
|
community_id,
|
||||||
sum(count_diff) AS subscribers
|
sum(count_diff) AS subscribers
|
||||||
FROM
|
FROM
|
||||||
combine_transition_tables ()
|
r.combine_transition_tables ()
|
||||||
WHERE (
|
WHERE (
|
||||||
SELECT
|
SELECT
|
||||||
local
|
local
|
||||||
|
@ -464,7 +465,7 @@ WHERE
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CALL r.create_triggers('community_follower', 'community_aggregates_from_subscriber');
|
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.
|
||||||
|
@ -515,7 +516,7 @@ CREATE FUNCTION r.person_aggregates_from_person ()
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO person_aggregates (person_id)
|
INSERT INTO person_aggregates (person_id)
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id
|
||||||
FROM
|
FROM
|
||||||
new_person;
|
new_person;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
|
@ -592,13 +593,14 @@ CREATE FUNCTION r.site_aggregates_from_site ()
|
||||||
BEGIN
|
BEGIN
|
||||||
-- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
|
-- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
|
||||||
-- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
|
-- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
|
||||||
IF NOT EXISTS (
|
IF (NOT EXISTS (
|
||||||
SELECT
|
SELECT
|
||||||
1
|
1
|
||||||
FROM
|
FROM
|
||||||
site_aggregates) THEN
|
site_aggregates)) THEN
|
||||||
INSERT INTO site_aggregates (site_id)
|
INSERT INTO site_aggregates (site_id)
|
||||||
VALUES (NEW.id);
|
VALUES (NEW.id);
|
||||||
|
END IF;
|
||||||
RETURN NULL;
|
RETURN NULL;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
Loading…
Reference in a new issue