mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-22 20:31:19 +00:00
Finishing up user aggregates.
This commit is contained in:
parent
5d44dedfda
commit
d66f4e8ac0
6 changed files with 127 additions and 175 deletions
|
@ -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! {
|
table! {
|
||||||
user_ban (id) {
|
user_ban (id) {
|
||||||
id -> Int4,
|
id -> Int4,
|
||||||
|
@ -562,6 +573,7 @@ joinable!(post_report -> post (post_id));
|
||||||
joinable!(post_saved -> post (post_id));
|
joinable!(post_saved -> post (post_id));
|
||||||
joinable!(post_saved -> user_ (user_id));
|
joinable!(post_saved -> user_ (user_id));
|
||||||
joinable!(site -> user_ (creator_id));
|
joinable!(site -> user_ (creator_id));
|
||||||
|
joinable!(user_aggregates -> user_ (user_id));
|
||||||
joinable!(user_ban -> user_ (user_id));
|
joinable!(user_ban -> user_ (user_id));
|
||||||
joinable!(user_mention -> comment (comment_id));
|
joinable!(user_mention -> comment (comment_id));
|
||||||
joinable!(user_mention -> user_ (recipient_id));
|
joinable!(user_mention -> user_ (recipient_id));
|
||||||
|
@ -599,6 +611,7 @@ allow_tables_to_appear_in_same_query!(
|
||||||
site,
|
site,
|
||||||
site_aggregates,
|
site_aggregates,
|
||||||
user_,
|
user_,
|
||||||
|
user_aggregates,
|
||||||
user_ban,
|
user_ban,
|
||||||
user_fast,
|
user_fast,
|
||||||
user_mention,
|
user_mention,
|
||||||
|
|
|
@ -17,3 +17,5 @@ impl SiteAggregates {
|
||||||
site_aggregates::table.first::<Self>(conn)
|
site_aggregates::table.first::<Self>(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO add unit tests, to make sure triggers are working
|
||||||
|
|
|
@ -1,19 +1,11 @@
|
||||||
-- Site aggregates
|
-- Site aggregates
|
||||||
drop table site_aggregates;
|
drop table site_aggregates;
|
||||||
drop trigger site_aggregates_insert_user on user_;
|
drop trigger site_aggregates_user on user_;
|
||||||
drop trigger site_aggregates_delete_user on user_;
|
drop trigger site_aggregates_post on post;
|
||||||
drop trigger site_aggregates_insert_post on post;
|
drop trigger site_aggregates_comment on comment;
|
||||||
drop trigger site_aggregates_delete_post on post;
|
drop trigger site_aggregates_community on community;
|
||||||
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 function
|
drop function
|
||||||
site_aggregates_user_increment,
|
site_aggregates_user,
|
||||||
site_aggregates_user_decrement,
|
site_aggregates_post,
|
||||||
site_aggregates_post_increment,
|
site_aggregates_comment,
|
||||||
site_aggregates_post_decrement,
|
site_aggregates_community;
|
||||||
site_aggregates_comment_increment,
|
|
||||||
site_aggregates_comment_decrement,
|
|
||||||
site_aggregates_community_increment,
|
|
||||||
site_aggregates_community_decrement;
|
|
||||||
|
|
|
@ -15,110 +15,78 @@ insert into site_aggregates (users, posts, comments, communities)
|
||||||
|
|
||||||
-- Add site aggregate triggers
|
-- Add site aggregate triggers
|
||||||
-- user
|
-- user
|
||||||
create function site_aggregates_user_increment()
|
create function site_aggregates_user()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set users = users + 1;
|
update site_aggregates
|
||||||
|
set users = users + 1;
|
||||||
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
|
update site_aggregates
|
||||||
|
set users = users - 1;
|
||||||
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_user
|
create trigger site_aggregates_user
|
||||||
after insert on user_
|
after insert or delete on user_
|
||||||
execute procedure site_aggregates_user_increment();
|
execute procedure site_aggregates_user();
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
-- post
|
-- post
|
||||||
create function site_aggregates_post_increment()
|
create function site_aggregates_post()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set posts = posts + 1;
|
update site_aggregates
|
||||||
|
set posts = posts + 1;
|
||||||
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
|
update site_aggregates
|
||||||
|
set posts = posts - 1;
|
||||||
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_post
|
create trigger site_aggregates_post
|
||||||
after insert on post
|
after insert or delete on post
|
||||||
execute procedure site_aggregates_post_increment();
|
execute procedure site_aggregates_post();
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
-- comment
|
-- comment
|
||||||
create function site_aggregates_comment_increment()
|
create function site_aggregates_comment()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set comments = comments + 1;
|
update site_aggregates
|
||||||
|
set comments = comments + 1;
|
||||||
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
|
update site_aggregates
|
||||||
|
set comments = comments - 1;
|
||||||
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_comment
|
create trigger site_aggregates_comment
|
||||||
after insert on comment
|
after insert or delete on comment
|
||||||
execute procedure site_aggregates_comment_increment();
|
execute procedure site_aggregates_comment();
|
||||||
|
|
||||||
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
|
-- community
|
||||||
create function site_aggregates_community_increment()
|
create function site_aggregates_community()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set communities = communities + 1;
|
update site_aggregates
|
||||||
|
set communities = communities + 1;
|
||||||
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
|
update site_aggregates
|
||||||
|
set communities = communities - 1;
|
||||||
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_community
|
create trigger site_aggregates_community
|
||||||
after insert on community
|
after insert or delete on community
|
||||||
execute procedure site_aggregates_community_increment();
|
execute procedure site_aggregates_community();
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -34,112 +34,79 @@ insert into user_aggregates (user_id, post_count, post_score, comment_count, com
|
||||||
) cd on u.id = cd.creator_id;
|
) cd on u.id = cd.creator_id;
|
||||||
|
|
||||||
|
|
||||||
-- Add site aggregate triggers
|
-- Add user aggregate triggers
|
||||||
-- user
|
-- post count
|
||||||
create function site_aggregates_user_increment()
|
create function user_aggregates_post_count()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set users = users + 1;
|
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;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_user
|
create trigger user_aggregates_post_count
|
||||||
after insert on user_
|
after insert or delete on post
|
||||||
execute procedure site_aggregates_user_increment();
|
execute procedure user_aggregates_post_count();
|
||||||
|
|
||||||
create function site_aggregates_user_decrement()
|
-- post score
|
||||||
|
create function user_aggregates_post_score()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set users = users - 1;
|
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;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_delete_user
|
create trigger user_aggregates_post_score
|
||||||
after delete on user_
|
after insert or delete on post_like
|
||||||
execute procedure site_aggregates_user_decrement();
|
execute procedure user_aggregates_post_score();
|
||||||
|
|
||||||
-- post
|
-- comment count
|
||||||
create function site_aggregates_post_increment()
|
create function user_aggregates_comment_count()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set posts = posts + 1;
|
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;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_insert_post
|
create trigger user_aggregates_comment_count
|
||||||
after insert on post
|
after insert or delete on comment
|
||||||
execute procedure site_aggregates_post_increment();
|
execute procedure user_aggregates_comment_count();
|
||||||
|
|
||||||
create function site_aggregates_post_decrement()
|
-- comment score
|
||||||
|
create function user_aggregates_comment_score()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
update site_aggregates
|
IF (TG_OP = 'INSERT') THEN
|
||||||
set posts = posts - 1;
|
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;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
||||||
create trigger site_aggregates_delete_post
|
create trigger user_aggregates_comment_score
|
||||||
after delete on post
|
after insert or delete on comment_like
|
||||||
execute procedure site_aggregates_post_decrement();
|
execute procedure user_aggregates_comment_score();
|
||||||
|
|
||||||
-- 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();
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue