]> Untitled Git - lemmy.git/blob - migrations/2019-04-03-155205_create_community_view/up.sql
Isomorphic docker (#1124)
[lemmy.git] / migrations / 2019-04-03-155205_create_community_view / up.sql
1 create view community_view as 
2 with all_community as
3 (
4   select *,
5   (select name from user_ u where c.creator_id = u.id) as creator_name,
6   (select name from category ct where c.category_id = ct.id) as category_name,
7   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
8   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
9   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
10   from community c
11 )
12
13 select
14 ac.*,
15 u.id as user_id,
16 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
17 from user_ u
18 cross join all_community ac
19
20 union all
21
22 select 
23 ac.*,
24 null as user_id,
25 null as subscribed
26 from all_community ac
27 ;
28
29 create view community_moderator_view as 
30 select *,
31 (select name from user_ u where cm.user_id = u.id) as user_name,
32 (select name from community c where cm.community_id = c.id) as community_name
33 from community_moderator cm;
34
35 create view community_follower_view as 
36 select *,
37 (select name from user_ u where cf.user_id = u.id) as user_name,
38 (select name from community c where cf.community_id = c.id) as community_name
39 from community_follower cf;
40
41 create view community_user_ban_view as 
42 select *,
43 (select name from user_ u where cm.user_id = u.id) as user_name,
44 (select name from community c where cm.community_id = c.id) as community_name
45 from community_user_ban cm;
46
47 create view site_view as 
48 select *,
49 (select name from user_ u where s.creator_id = u.id) as creator_name,
50 (select count(*) from user_) as number_of_users,
51 (select count(*) from post) as number_of_posts,
52 (select count(*) from comment) as number_of_comments
53 from site s;