]> Untitled Git - lemmy.git/blob - migrations/2019-09-09-042010_add_stickied_posts/up.sql
Isomorphic docker (#1124)
[lemmy.git] / migrations / 2019-09-09-042010_add_stickied_posts / up.sql
1 -- Add the column
2 alter table post add column stickied boolean default false not null;
3
4 -- Add the mod table
5 create table mod_sticky_post (
6   id serial primary key,
7   mod_user_id int references user_ on update cascade on delete cascade not null,
8   post_id int references post on update cascade on delete cascade not null,
9   stickied boolean default true,
10   when_ timestamp not null default now()
11 );
12
13 -- Add mod view
14 create view mod_sticky_post_view as 
15 select msp.*,
16 (select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
17 (select name from post p where msp.post_id = p.id) as post_name,
18 (select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
19 (select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
20 from mod_sticky_post msp;
21
22 -- Recreate the view
23 drop view post_view;
24 create view post_view as
25 with all_post as
26 (
27   select        
28   p.*,
29   (select u.banned from user_ u where p.creator_id = u.id) as banned,
30   (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,
31   (select name from user_ where p.creator_id = user_.id) as creator_name,
32   (select name from community where p.community_id = community.id) as community_name,
33   (select removed from community c where p.community_id = c.id) as community_removed,
34   (select deleted from community c where p.community_id = c.id) as community_deleted,
35   (select nsfw from community c where p.community_id = c.id) as community_nsfw,
36   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
37   coalesce(sum(pl.score), 0) as score,
38   count (case when pl.score = 1 then 1 else null end) as upvotes,
39   count (case when pl.score = -1 then 1 else null end) as downvotes,
40   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
41   from post p
42   left join post_like pl on p.id = pl.post_id
43   group by p.id
44 )
45
46 select
47 ap.*,
48 u.id as user_id,
49 coalesce(pl.score, 0) as my_vote,
50 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
51 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
52 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
53 from user_ u
54 cross join all_post ap
55 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
56
57 union all
58
59 select 
60 ap.*,
61 null as user_id,
62 null as my_vote,
63 null as subscribed,
64 null as read,
65 null as saved
66 from all_post ap
67 ;