2020-12-02 22:39:31 -05:00
|
|
|
-- Add site aggregates
|
|
|
|
create table site_aggregates (
|
|
|
|
id serial primary key,
|
2020-12-19 20:10:47 -05:00
|
|
|
site_id int references site on update cascade on delete cascade not null,
|
|
|
|
users bigint not null default 0,
|
|
|
|
posts bigint not null default 0,
|
|
|
|
comments bigint not null default 0,
|
|
|
|
communities bigint not null default 0
|
2020-12-02 22:39:31 -05:00
|
|
|
);
|
|
|
|
|
2020-12-19 20:10:47 -05:00
|
|
|
insert into site_aggregates (site_id, users, posts, comments, communities)
|
|
|
|
select id as site_id,
|
|
|
|
( select coalesce(count(*), 0) from user_) as users,
|
2020-12-02 22:39:31 -05:00
|
|
|
( select coalesce(count(*), 0) from post) as posts,
|
|
|
|
( select coalesce(count(*), 0) from comment) as comments,
|
2020-12-19 20:10:47 -05:00
|
|
|
( select coalesce(count(*), 0) from community) as communities
|
|
|
|
from site;
|
|
|
|
|
|
|
|
-- initial site add
|
|
|
|
create function site_aggregates_site()
|
|
|
|
returns trigger language plpgsql
|
|
|
|
as $$
|
|
|
|
begin
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
insert into site_aggregates (site_id) values (NEW.id);
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
delete from site_aggregates where site_id = OLD.id;
|
|
|
|
END IF;
|
|
|
|
return null;
|
|
|
|
end $$;
|
|
|
|
|
|
|
|
create trigger site_aggregates_site
|
|
|
|
after insert or delete on site
|
|
|
|
for each row
|
|
|
|
execute procedure site_aggregates_site();
|
2020-12-02 22:39:31 -05:00
|
|
|
|
|
|
|
-- Add site aggregate triggers
|
|
|
|
-- user
|
2020-12-03 10:18:17 -05:00
|
|
|
create function site_aggregates_user()
|
2020-12-02 22:39:31 -05:00
|
|
|
returns trigger language plpgsql
|
|
|
|
as $$
|
|
|
|
begin
|
2020-12-03 10:18:17 -05:00
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set users = users + 1;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set users = users - 1;
|
|
|
|
END IF;
|
2020-12-02 22:39:31 -05:00
|
|
|
return null;
|
|
|
|
end $$;
|
|
|
|
|
2020-12-03 10:18:17 -05:00
|
|
|
create trigger site_aggregates_user
|
|
|
|
after insert or delete on user_
|
2020-12-09 11:52:10 -05:00
|
|
|
for each row
|
2020-12-03 10:18:17 -05:00
|
|
|
execute procedure site_aggregates_user();
|
2020-12-02 22:39:31 -05:00
|
|
|
|
|
|
|
-- post
|
2020-12-03 10:18:17 -05:00
|
|
|
create function site_aggregates_post()
|
2020-12-02 22:39:31 -05:00
|
|
|
returns trigger language plpgsql
|
|
|
|
as $$
|
|
|
|
begin
|
2020-12-03 10:18:17 -05:00
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set posts = posts + 1;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set posts = posts - 1;
|
|
|
|
END IF;
|
2020-12-02 22:39:31 -05:00
|
|
|
return null;
|
|
|
|
end $$;
|
|
|
|
|
2020-12-03 10:18:17 -05:00
|
|
|
create trigger site_aggregates_post
|
|
|
|
after insert or delete on post
|
2020-12-06 22:17:52 -05:00
|
|
|
for each row
|
2020-12-03 10:18:17 -05:00
|
|
|
execute procedure site_aggregates_post();
|
2020-12-02 22:39:31 -05:00
|
|
|
|
|
|
|
-- comment
|
2020-12-03 10:18:17 -05:00
|
|
|
create function site_aggregates_comment()
|
2020-12-02 22:39:31 -05:00
|
|
|
returns trigger language plpgsql
|
|
|
|
as $$
|
|
|
|
begin
|
2020-12-03 10:18:17 -05:00
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set comments = comments + 1;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set comments = comments - 1;
|
|
|
|
END IF;
|
2020-12-02 22:39:31 -05:00
|
|
|
return null;
|
|
|
|
end $$;
|
|
|
|
|
2020-12-03 10:18:17 -05:00
|
|
|
create trigger site_aggregates_comment
|
|
|
|
after insert or delete on comment
|
2020-12-06 22:17:52 -05:00
|
|
|
for each row
|
2020-12-03 10:18:17 -05:00
|
|
|
execute procedure site_aggregates_comment();
|
2020-12-02 22:39:31 -05:00
|
|
|
|
|
|
|
-- community
|
2020-12-03 10:18:17 -05:00
|
|
|
create function site_aggregates_community()
|
2020-12-02 22:39:31 -05:00
|
|
|
returns trigger language plpgsql
|
|
|
|
as $$
|
|
|
|
begin
|
2020-12-03 10:18:17 -05:00
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set communities = communities + 1;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
update site_aggregates
|
|
|
|
set communities = communities - 1;
|
|
|
|
END IF;
|
2020-12-02 22:39:31 -05:00
|
|
|
return null;
|
|
|
|
end $$;
|
|
|
|
|
2020-12-03 10:18:17 -05:00
|
|
|
create trigger site_aggregates_community
|
|
|
|
after insert or delete on community
|
2020-12-06 22:17:52 -05:00
|
|
|
for each row
|
2020-12-03 10:18:17 -05:00
|
|
|
execute procedure site_aggregates_community();
|
2020-12-02 22:39:31 -05:00
|
|
|
|