]> Untitled Git - lemmy.git/blob - migrations/2019-08-11-000918_add_nsfw_columns/down.sql
Isomorphic docker (#1124)
[lemmy.git] / migrations / 2019-08-11-000918_add_nsfw_columns / down.sql
1 drop view community_view;
2 drop view post_view;
3 alter table community drop column nsfw;
4 alter table post drop column nsfw;
5 alter table user_ drop column show_nsfw;
6
7 -- the views
8 create view community_view as 
9 with all_community as
10 (
11   select *,
12   (select name from user_ u where c.creator_id = u.id) as creator_name,
13   (select name from category ct where c.category_id = ct.id) as category_name,
14   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
15   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
16   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
17   hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
18   from community c
19 )
20
21 select
22 ac.*,
23 u.id as user_id,
24 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
25 from user_ u
26 cross join all_community ac
27
28 union all
29
30 select 
31 ac.*,
32 null as user_id,
33 null as subscribed
34 from all_community ac
35 ;
36
37
38 -- Post view
39 create view post_view as
40 with all_post as
41 (
42   select        
43   p.*,
44   (select name from user_ where p.creator_id = user_.id) as creator_name,
45   (select name from community where p.community_id = community.id) as community_name,
46   (select removed from community c where p.community_id = c.id) as community_removed,
47   (select deleted from community c where p.community_id = c.id) as community_deleted,
48   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
49   coalesce(sum(pl.score), 0) as score,
50   count (case when pl.score = 1 then 1 else null end) as upvotes,
51   count (case when pl.score = -1 then 1 else null end) as downvotes,
52   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
53   from post p
54   left join post_like pl on p.id = pl.post_id
55   group by p.id
56 )
57
58 select
59 ap.*,
60 u.id as user_id,
61 coalesce(pl.score, 0) as my_vote,
62 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
63 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
64 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
65 from user_ u
66 cross join all_post ap
67 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
68
69 union all
70
71 select 
72 ap.*,
73 null as user_id,
74 null as my_vote,
75 null as subscribed,
76 null as read,
77 null as saved
78 from all_post ap
79 ;
80