]> Untitled Git - lemmy.git/blob - server/migrations/2019-04-29-175834_add_delete_columns/down.sql
5e13295b2a2f496f24997dd927776eeeff799f2d
[lemmy.git] / server / migrations / 2019-04-29-175834_add_delete_columns / down.sql
1 drop view reply_view;
2 drop view comment_view;
3 drop view community_view;
4 drop view post_view;
5 alter table community drop column deleted;
6 alter table post drop column deleted;
7 alter table comment drop column deleted;
8
9 create view community_view as 
10 with all_community as
11 (
12   select *,
13   (select name from user_ u where c.creator_id = u.id) as creator_name,
14   (select name from category ct where c.category_id = ct.id) as category_name,
15   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
16   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
17   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
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 create or replace view post_view as
38 with all_post as
39 (
40   select        
41   p.*,
42   (select name from user_ where p.creator_id = user_.id) as creator_name,
43   (select name from community where p.community_id = community.id) as community_name,
44   (select removed from community c where p.community_id = c.id) as community_removed,
45   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
46   coalesce(sum(pl.score), 0) as score,
47   count (case when pl.score = 1 then 1 else null end) as upvotes,
48   count (case when pl.score = -1 then 1 else null end) as downvotes,
49   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
50   from post p
51   left join post_like pl on p.id = pl.post_id
52   group by p.id
53 )
54
55 select
56 ap.*,
57 u.id as user_id,
58 coalesce(pl.score, 0) as my_vote,
59 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
60 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
61 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
62 from user_ u
63 cross join all_post ap
64 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
65
66 union all
67
68 select 
69 ap.*,
70 null as user_id,
71 null as my_vote,
72 null as subscribed,
73 null as read,
74 null as saved
75 from all_post ap
76 ;
77
78 create view comment_view as
79 with all_comment as
80 (
81   select        
82   c.*,
83   (select community_id from post p where p.id = c.post_id),
84   (select u.banned from user_ u where c.creator_id = u.id) as banned,
85   (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,
86   (select name from user_ where c.creator_id = user_.id) as creator_name,
87   coalesce(sum(cl.score), 0) as score,
88   count (case when cl.score = 1 then 1 else null end) as upvotes,
89   count (case when cl.score = -1 then 1 else null end) as downvotes
90   from comment c
91   left join comment_like cl on c.id = cl.comment_id
92   group by c.id
93 )
94
95 select
96 ac.*,
97 u.id as user_id,
98 coalesce(cl.score, 0) as my_vote,
99 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
100 from user_ u
101 cross join all_comment ac
102 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
103
104 union all
105
106 select 
107     ac.*,
108     null as user_id, 
109     null as my_vote,
110     null as saved
111 from all_comment ac
112 ;
113
114 create view reply_view as 
115 with closereply as (
116     select 
117     c2.id, 
118     c2.creator_id as sender_id, 
119     c.creator_id as recipient_id
120     from comment c
121     inner join comment c2 on c.id = c2.parent_id
122     where c2.creator_id != c.creator_id
123     -- Do union where post is null
124     union
125     select
126     c.id,
127     c.creator_id as sender_id,
128     p.creator_id as recipient_id
129     from comment c, post p
130     where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
131 )
132 select cv.*,
133 closereply.recipient_id
134 from comment_view cv, closereply
135 where closereply.id = cv.id
136 ;
137