]> Untitled Git - lemmy.git/blob - migrations/2019-04-29-175834_add_delete_columns/up.sql
Isomorphic docker (#1124)
[lemmy.git] / migrations / 2019-04-29-175834_add_delete_columns / up.sql
1 alter table community add column deleted boolean default false not null;
2 alter table post add column deleted boolean default false not null;
3 alter table comment add column deleted boolean default false not null;
4
5 -- The views
6 drop view community_view;
7
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   from community c
18 )
19
20 select
21 ac.*,
22 u.id as user_id,
23 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
24 from user_ u
25 cross join all_community ac
26
27 union all
28
29 select 
30 ac.*,
31 null as user_id,
32 null as subscribed
33 from all_community ac
34 ;
35
36
37 drop view post_view;
38 create view post_view as
39 with all_post as
40 (
41   select        
42   p.*,
43   (select name from user_ where p.creator_id = user_.id) as creator_name,
44   (select name from community where p.community_id = community.id) as community_name,
45   (select removed from community c where p.community_id = c.id) as community_removed,
46   (select deleted from community c where p.community_id = c.id) as community_deleted,
47   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
48   coalesce(sum(pl.score), 0) as score,
49   count (case when pl.score = 1 then 1 else null end) as upvotes,
50   count (case when pl.score = -1 then 1 else null end) as downvotes,
51   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
52   from post p
53   left join post_like pl on p.id = pl.post_id
54   group by p.id
55 )
56
57 select
58 ap.*,
59 u.id as user_id,
60 coalesce(pl.score, 0) as my_vote,
61 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
62 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
63 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
64 from user_ u
65 cross join all_post ap
66 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
67
68 union all
69
70 select 
71 ap.*,
72 null as user_id,
73 null as my_vote,
74 null as subscribed,
75 null as read,
76 null as saved
77 from all_post ap
78 ;
79
80 drop view reply_view;
81 drop view comment_view;
82 create view comment_view as
83 with all_comment as
84 (
85   select        
86   c.*,
87   (select community_id from post p where p.id = c.post_id),
88   (select u.banned from user_ u where c.creator_id = u.id) as banned,
89   (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
90   (select name from user_ where c.creator_id = user_.id) as creator_name,
91   coalesce(sum(cl.score), 0) as score,
92   count (case when cl.score = 1 then 1 else null end) as upvotes,
93   count (case when cl.score = -1 then 1 else null end) as downvotes
94   from comment c
95   left join comment_like cl on c.id = cl.comment_id
96   group by c.id
97 )
98
99 select
100 ac.*,
101 u.id as user_id,
102 coalesce(cl.score, 0) as my_vote,
103 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
104 from user_ u
105 cross join all_comment ac
106 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
107
108 union all
109
110 select 
111     ac.*,
112     null as user_id, 
113     null as my_vote,
114     null as saved
115 from all_comment ac
116 ;
117
118 create view reply_view as 
119 with closereply as (
120     select 
121     c2.id, 
122     c2.creator_id as sender_id, 
123     c.creator_id as recipient_id
124     from comment c
125     inner join comment c2 on c.id = c2.parent_id
126     where c2.creator_id != c.creator_id
127     -- Do union where post is null
128     union
129     select
130     c.id,
131     c.creator_id as sender_id,
132     p.creator_id as recipient_id
133     from comment c, post p
134     where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
135 )
136 select cv.*,
137 closereply.recipient_id
138 from comment_view cv, closereply
139 where closereply.id = cv.id
140 ;
141