]> Untitled Git - lemmy.git/blob - migrations/2019-09-09-042010_add_stickied_posts/down.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / migrations / 2019-09-09-042010_add_stickied_posts / down.sql
1 drop view post_view;
2 drop view mod_sticky_post_view;
3 alter table post drop column stickied;
4
5 drop table mod_sticky_post;
6
7 create view post_view as
8 with all_post as
9 (
10   select        
11   p.*,
12   (select u.banned from user_ u where p.creator_id = u.id) as banned,
13   (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
14   (select name from user_ where p.creator_id = user_.id) as creator_name,
15   (select name from community where p.community_id = community.id) as community_name,
16   (select removed from community c where p.community_id = c.id) as community_removed,
17   (select deleted from community c where p.community_id = c.id) as community_deleted,
18   (select nsfw from community c where p.community_id = c.id) as community_nsfw,
19   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
20   coalesce(sum(pl.score), 0) as score,
21   count (case when pl.score = 1 then 1 else null end) as upvotes,
22   count (case when pl.score = -1 then 1 else null end) as downvotes,
23   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
24   from post p
25   left join post_like pl on p.id = pl.post_id
26   group by p.id
27 )
28
29 select
30 ap.*,
31 u.id as user_id,
32 coalesce(pl.score, 0) as my_vote,
33 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
34 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
35 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
36 from user_ u
37 cross join all_post ap
38 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
39
40 union all
41
42 select 
43 ap.*,
44 null as user_id,
45 null as my_vote,
46 null as subscribed,
47 null as read,
48 null as saved
49 from all_post ap
50 ;