]> Untitled Git - lemmy.git/blob - server/migrations/2020-02-08-145624_add_post_newest_activity_time/down.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2020-02-08-145624_add_post_newest_activity_time / down.sql
1 drop view post_view;
2 drop view post_mview;
3 drop materialized view post_aggregates_mview;
4 drop view post_aggregates_view;
5
6 -- regen post view
7 create view post_aggregates_view as
8 select        
9 p.*,
10 (select u.banned from user_ u where p.creator_id = u.id) as banned,
11 (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,
12 (select name from user_ where p.creator_id = user_.id) as creator_name,
13 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
14 (select name from community where p.community_id = community.id) as community_name,
15 (select removed from community c where p.community_id = c.id) as community_removed,
16 (select deleted from community c where p.community_id = c.id) as community_deleted,
17 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
18 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
19 coalesce(sum(pl.score), 0) as score,
20 count (case when pl.score = 1 then 1 else null end) as upvotes,
21 count (case when pl.score = -1 then 1 else null end) as downvotes,
22 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
23 from post p
24 left join post_like pl on p.id = pl.post_id
25 group by p.id;
26
27 create materialized view post_aggregates_mview as select * from post_aggregates_view;
28
29 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
30
31 create view post_view as 
32 with all_post as (
33   select
34   pa.*
35   from post_aggregates_view pa
36 )
37 select
38 ap.*,
39 u.id as user_id,
40 coalesce(pl.score, 0) as my_vote,
41 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
42 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
43 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
44 from user_ u
45 cross join all_post ap
46 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
47
48 union all
49
50 select 
51 ap.*,
52 null as user_id,
53 null as my_vote,
54 null as subscribed,
55 null as read,
56 null as saved
57 from all_post ap
58 ;
59
60 create view post_mview as 
61 with all_post as (
62   select
63   pa.*
64   from post_aggregates_mview pa
65 )
66 select
67 ap.*,
68 u.id as user_id,
69 coalesce(pl.score, 0) as my_vote,
70 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
71 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
72 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
73 from user_ u
74 cross join all_post ap
75 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
76
77 union all
78
79 select 
80 ap.*,
81 null as user_id,
82 null as my_vote,
83 null as subscribed,
84 null as read,
85 null as saved
86 from all_post ap
87 ;
88