Finishing up user aggregates.

This commit is contained in:
Dessalines 2020-12-03 10:18:17 -05:00
parent 5d44dedfda
commit d66f4e8ac0
6 changed files with 127 additions and 175 deletions

View file

@ -481,6 +481,17 @@ table! {
}
}
table! {
user_aggregates (id) {
id -> Int4,
user_id -> Int4,
post_count -> Int8,
post_score -> Int8,
comment_count -> Int8,
comment_score -> Int8,
}
}
table! {
user_ban (id) {
id -> Int4,
@ -562,6 +573,7 @@ joinable!(post_report -> post (post_id));
joinable!(post_saved -> post (post_id));
joinable!(post_saved -> user_ (user_id));
joinable!(site -> user_ (creator_id));
joinable!(user_aggregates -> user_ (user_id));
joinable!(user_ban -> user_ (user_id));
joinable!(user_mention -> comment (comment_id));
joinable!(user_mention -> user_ (recipient_id));
@ -599,6 +611,7 @@ allow_tables_to_appear_in_same_query!(
site,
site_aggregates,
user_,
user_aggregates,
user_ban,
user_fast,
user_mention,

View file

@ -17,3 +17,5 @@ impl SiteAggregates {
site_aggregates::table.first::<Self>(conn)
}
}
// TODO add unit tests, to make sure triggers are working

View file

@ -1,19 +1,11 @@
-- Site aggregates
drop table site_aggregates;
drop trigger site_aggregates_insert_user on user_;
drop trigger site_aggregates_delete_user on user_;
drop trigger site_aggregates_insert_post on post;
drop trigger site_aggregates_delete_post on post;
drop trigger site_aggregates_insert_comment on comment;
drop trigger site_aggregates_delete_comment on comment;
drop trigger site_aggregates_insert_community on community;
drop trigger site_aggregates_delete_community on community;
drop trigger site_aggregates_user on user_;
drop trigger site_aggregates_post on post;
drop trigger site_aggregates_comment on comment;
drop trigger site_aggregates_community on community;
drop function
site_aggregates_user_increment,
site_aggregates_user_decrement,
site_aggregates_post_increment,
site_aggregates_post_decrement,
site_aggregates_comment_increment,
site_aggregates_comment_decrement,
site_aggregates_community_increment,
site_aggregates_community_decrement;
site_aggregates_user,
site_aggregates_post,
site_aggregates_comment,
site_aggregates_community;

View file

@ -15,110 +15,78 @@ insert into site_aggregates (users, posts, comments, communities)
-- Add site aggregate triggers
-- user
create function site_aggregates_user_increment()
create function site_aggregates_user()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set users = users + 1;
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;
return null;
end $$;
create trigger site_aggregates_insert_user
after insert on user_
execute procedure site_aggregates_user_increment();
create function site_aggregates_user_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set users = users - 1;
return null;
end $$;
create trigger site_aggregates_delete_user
after delete on user_
execute procedure site_aggregates_user_decrement();
create trigger site_aggregates_user
after insert or delete on user_
execute procedure site_aggregates_user();
-- post
create function site_aggregates_post_increment()
create function site_aggregates_post()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set posts = posts + 1;
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;
return null;
end $$;
create trigger site_aggregates_insert_post
after insert on post
execute procedure site_aggregates_post_increment();
create function site_aggregates_post_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set posts = posts - 1;
return null;
end $$;
create trigger site_aggregates_delete_post
after delete on post
execute procedure site_aggregates_post_decrement();
create trigger site_aggregates_post
after insert or delete on post
execute procedure site_aggregates_post();
-- comment
create function site_aggregates_comment_increment()
create function site_aggregates_comment()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set comments = comments + 1;
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;
return null;
end $$;
create trigger site_aggregates_insert_comment
after insert on comment
execute procedure site_aggregates_comment_increment();
create function site_aggregates_comment_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set comments = comments - 1;
return null;
end $$;
create trigger site_aggregates_delete_comment
after delete on comment
execute procedure site_aggregates_comment_decrement();
create trigger site_aggregates_comment
after insert or delete on comment
execute procedure site_aggregates_comment();
-- community
create function site_aggregates_community_increment()
create function site_aggregates_community()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set communities = communities + 1;
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;
return null;
end $$;
create trigger site_aggregates_insert_community
after insert on community
execute procedure site_aggregates_community_increment();
create function site_aggregates_community_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set communities = communities - 1;
return null;
end $$;
create trigger site_aggregates_delete_community
after delete on community
execute procedure site_aggregates_community_decrement();
create trigger site_aggregates_community
after insert or delete on community
execute procedure site_aggregates_community();

View file

@ -1 +1,11 @@
-- This file should undo anything in `up.sql`
-- User aggregates
drop table user_aggregates;
drop trigger user_aggregates_post_count on post;
drop trigger user_aggregates_post_score on post_like;
drop trigger user_aggregates_comment_count on comment;
drop trigger user_aggregates_comment_score on comment_like;
drop function
user_aggregates_post_count,
user_aggregates_post_score,
user_aggregates_comment_count,
user_aggregates_comment_score;

View file

@ -34,112 +34,79 @@ insert into user_aggregates (user_id, post_count, post_score, comment_count, com
) cd on u.id = cd.creator_id;
-- Add site aggregate triggers
-- user
create function site_aggregates_user_increment()
-- Add user aggregate triggers
-- post count
create function user_aggregates_post_count()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set users = users + 1;
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set post_count = post_count + 1 where user_id = NEW.user_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set post_count = post_count - 1 where user_id = OLD.user_id;
END IF;
return null;
end $$;
create trigger site_aggregates_insert_user
after insert on user_
execute procedure site_aggregates_user_increment();
create trigger user_aggregates_post_count
after insert or delete on post
execute procedure user_aggregates_post_count();
create function site_aggregates_user_decrement()
-- post score
create function user_aggregates_post_score()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set users = users - 1;
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set post_score = post_score + NEW.score where user_id = NEW.user_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set post_score = post_score - OLD.score where user_id = OLD.user_id;
END IF;
return null;
end $$;
create trigger site_aggregates_delete_user
after delete on user_
execute procedure site_aggregates_user_decrement();
create trigger user_aggregates_post_score
after insert or delete on post_like
execute procedure user_aggregates_post_score();
-- post
create function site_aggregates_post_increment()
-- comment count
create function user_aggregates_comment_count()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set posts = posts + 1;
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set comment_count = comment_count + 1 where user_id = NEW.user_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set comment_count = comment_count - 1 where user_id = OLD.user_id;
END IF;
return null;
end $$;
create trigger site_aggregates_insert_post
after insert on post
execute procedure site_aggregates_post_increment();
create trigger user_aggregates_comment_count
after insert or delete on comment
execute procedure user_aggregates_comment_count();
create function site_aggregates_post_decrement()
-- comment score
create function user_aggregates_comment_score()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set posts = posts - 1;
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set comment_score = comment_score + NEW.score where user_id = NEW.user_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set comment_score = comment_score - OLD.score where user_id = OLD.user_id;
END IF;
return null;
end $$;
create trigger site_aggregates_delete_post
after delete on post
execute procedure site_aggregates_post_decrement();
-- comment
create function site_aggregates_comment_increment()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set comments = comments + 1;
return null;
end $$;
create trigger site_aggregates_insert_comment
after insert on comment
execute procedure site_aggregates_comment_increment();
create function site_aggregates_comment_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set comments = comments - 1;
return null;
end $$;
create trigger site_aggregates_delete_comment
after delete on comment
execute procedure site_aggregates_comment_decrement();
-- community
create function site_aggregates_community_increment()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set communities = communities + 1;
return null;
end $$;
create trigger site_aggregates_insert_community
after insert on community
execute procedure site_aggregates_community_increment();
create function site_aggregates_community_decrement()
returns trigger language plpgsql
as $$
begin
update site_aggregates
set communities = communities - 1;
return null;
end $$;
create trigger site_aggregates_delete_community
after delete on community
execute procedure site_aggregates_community_decrement();
create trigger user_aggregates_comment_score
after insert or delete on comment_like
execute procedure user_aggregates_comment_score();