mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-25 15:39:11 +00:00
Fix site aggs.
This commit is contained in:
parent
2d7d9cf7d8
commit
a27b7f8d1f
|
@ -27,6 +27,7 @@ mod tests {
|
||||||
comment::{Comment, CommentForm},
|
comment::{Comment, CommentForm},
|
||||||
community::{Community, CommunityForm},
|
community::{Community, CommunityForm},
|
||||||
post::{Post, PostForm},
|
post::{Post, PostForm},
|
||||||
|
site::{Site, SiteForm},
|
||||||
user::{UserForm, User_},
|
user::{UserForm, User_},
|
||||||
},
|
},
|
||||||
tests::establish_unpooled_connection,
|
tests::establish_unpooled_connection,
|
||||||
|
@ -68,6 +69,20 @@ mod tests {
|
||||||
|
|
||||||
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
||||||
|
|
||||||
|
let site_form = SiteForm {
|
||||||
|
name: "test_site".into(),
|
||||||
|
description: None,
|
||||||
|
icon: None,
|
||||||
|
banner: None,
|
||||||
|
creator_id: inserted_user.id,
|
||||||
|
enable_downvotes: true,
|
||||||
|
open_registration: true,
|
||||||
|
enable_nsfw: true,
|
||||||
|
updated: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Site::create(&conn, &site_form).unwrap();
|
||||||
|
|
||||||
let new_community = CommunityForm {
|
let new_community = CommunityForm {
|
||||||
name: "TIL_site_agg".into(),
|
name: "TIL_site_agg".into(),
|
||||||
creator_id: inserted_user.id,
|
creator_id: inserted_user.id,
|
||||||
|
@ -165,10 +180,7 @@ mod tests {
|
||||||
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
|
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
|
||||||
assert_eq!(1, user_num_deleted);
|
assert_eq!(1, user_num_deleted);
|
||||||
|
|
||||||
let site_aggregates_after_delete = SiteAggregates::read(&conn).unwrap();
|
let after_delete = SiteAggregates::read(&conn);
|
||||||
assert_eq!(0, site_aggregates_after_delete.users);
|
assert!(after_delete.is_err());
|
||||||
assert_eq!(0, site_aggregates_after_delete.communities);
|
|
||||||
assert_eq!(0, site_aggregates_after_delete.posts);
|
|
||||||
assert_eq!(0, site_aggregates_after_delete.comments);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,10 @@ impl Crud<SiteForm> for Site {
|
||||||
.set(new_site)
|
.set(new_site)
|
||||||
.get_result::<Self>(conn)
|
.get_result::<Self>(conn)
|
||||||
}
|
}
|
||||||
|
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
|
||||||
|
use crate::schema::site::dsl::*;
|
||||||
|
diesel::delete(site.find(site_id)).execute(conn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Site {
|
impl Site {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
create table site_aggregates (
|
create table site_aggregates (
|
||||||
id serial primary key,
|
id serial primary key,
|
||||||
site_id int references site on update cascade on delete cascade not null,
|
site_id int references site on update cascade on delete cascade not null,
|
||||||
users bigint not null default 0,
|
users bigint not null default 1,
|
||||||
posts bigint not null default 0,
|
posts bigint not null default 0,
|
||||||
comments bigint not null default 0,
|
comments bigint not null default 0,
|
||||||
communities bigint not null default 0
|
communities bigint not null default 0
|
||||||
|
@ -36,7 +36,7 @@ execute procedure site_aggregates_site();
|
||||||
|
|
||||||
-- Add site aggregate triggers
|
-- Add site aggregate triggers
|
||||||
-- user
|
-- user
|
||||||
create function site_aggregates_user()
|
create or replace function site_aggregates_user()
|
||||||
returns trigger language plpgsql
|
returns trigger language plpgsql
|
||||||
as $$
|
as $$
|
||||||
begin
|
begin
|
||||||
|
@ -44,8 +44,11 @@ begin
|
||||||
update site_aggregates
|
update site_aggregates
|
||||||
set users = users + 1;
|
set users = users + 1;
|
||||||
ELSIF (TG_OP = 'DELETE') THEN
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
update site_aggregates
|
-- Join to site since the creator might not be there anymore
|
||||||
set users = users - 1;
|
update site_aggregates sa
|
||||||
|
set users = users - 1
|
||||||
|
from site s
|
||||||
|
where sa.site_id = s.id;
|
||||||
END IF;
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
@ -64,8 +67,10 @@ begin
|
||||||
update site_aggregates
|
update site_aggregates
|
||||||
set posts = posts + 1;
|
set posts = posts + 1;
|
||||||
ELSIF (TG_OP = 'DELETE') THEN
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
update site_aggregates
|
update site_aggregates sa
|
||||||
set posts = posts - 1;
|
set posts = posts - 1
|
||||||
|
from site s
|
||||||
|
where sa.site_id = s.id;
|
||||||
END IF;
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
@ -84,8 +89,10 @@ begin
|
||||||
update site_aggregates
|
update site_aggregates
|
||||||
set comments = comments + 1;
|
set comments = comments + 1;
|
||||||
ELSIF (TG_OP = 'DELETE') THEN
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
update site_aggregates
|
update site_aggregates sa
|
||||||
set comments = comments - 1;
|
set comments = comments - 1
|
||||||
|
from site s
|
||||||
|
where sa.site_id = s.id;
|
||||||
END IF;
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
@ -104,8 +111,10 @@ begin
|
||||||
update site_aggregates
|
update site_aggregates
|
||||||
set communities = communities + 1;
|
set communities = communities + 1;
|
||||||
ELSIF (TG_OP = 'DELETE') THEN
|
ELSIF (TG_OP = 'DELETE') THEN
|
||||||
update site_aggregates
|
update site_aggregates sa
|
||||||
set communities = communities - 1;
|
set communities = communities - 1
|
||||||
|
from site s
|
||||||
|
where sa.site_id = s.id;
|
||||||
END IF;
|
END IF;
|
||||||
return null;
|
return null;
|
||||||
end $$;
|
end $$;
|
||||||
|
|
Loading…
Reference in a new issue