]> Untitled Git - lemmy.git/blob - server/migrations/2019-12-29-164820_add_avatar/down.sql
74b4146d916e1e0f29a717e0779cbad25c8927fd
[lemmy.git] / server / migrations / 2019-12-29-164820_add_avatar / down.sql
1 -- the views
2 drop view user_mention_view;
3 drop view reply_view;
4 drop view comment_view;
5 drop view user_view;
6
7 -- user
8 create view user_view as 
9 select id,
10 name,
11 fedi_name,
12 admin,
13 banned,
14 published,
15 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
16 (select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
17 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
18 (select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
19 from user_ u;
20
21 -- post
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 ;
68
69 -- community
70
71 drop view community_view;
72 create view community_view as 
73 with all_community as
74 (
75   select *,
76   (select name from user_ u where c.creator_id = u.id) as creator_name,
77   (select name from category ct where c.category_id = ct.id) as category_name,
78   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
79   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
80   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
81   hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
82   from community c
83 )
84
85 select
86 ac.*,
87 u.id as user_id,
88 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
89 from user_ u
90 cross join all_community ac
91
92 union all
93
94 select 
95 ac.*,
96 null as user_id,
97 null as subscribed
98 from all_community ac
99 ;
100
101 -- Reply and comment view
102 create view comment_view as
103 with all_comment as
104 (
105   select        
106   c.*,
107   (select community_id from post p where p.id = c.post_id),
108   (select u.banned from user_ u where c.creator_id = u.id) as banned,
109   (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,
110   (select name from user_ where c.creator_id = user_.id) as creator_name,
111   coalesce(sum(cl.score), 0) as score,
112   count (case when cl.score = 1 then 1 else null end) as upvotes,
113   count (case when cl.score = -1 then 1 else null end) as downvotes
114   from comment c
115   left join comment_like cl on c.id = cl.comment_id
116   group by c.id
117 )
118
119 select
120 ac.*,
121 u.id as user_id,
122 coalesce(cl.score, 0) as my_vote,
123 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
124 from user_ u
125 cross join all_comment ac
126 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
127
128 union all
129
130 select 
131     ac.*,
132     null as user_id, 
133     null as my_vote,
134     null as saved
135 from all_comment ac
136 ;
137
138 create view reply_view as 
139 with closereply as (
140     select 
141     c2.id, 
142     c2.creator_id as sender_id, 
143     c.creator_id as recipient_id
144     from comment c
145     inner join comment c2 on c.id = c2.parent_id
146     where c2.creator_id != c.creator_id
147     -- Do union where post is null
148     union
149     select
150     c.id,
151     c.creator_id as sender_id,
152     p.creator_id as recipient_id
153     from comment c, post p
154     where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
155 )
156 select cv.*,
157 closereply.recipient_id
158 from comment_view cv, closereply
159 where closereply.id = cv.id
160 ;
161
162 -- user mention
163 create view user_mention_view as
164 select 
165     c.id,
166     um.id as user_mention_id,
167     c.creator_id,
168     c.post_id,
169     c.parent_id,
170     c.content,
171     c.removed,
172     um.read,
173     c.published,
174     c.updated,
175     c.deleted,
176     c.community_id,
177     c.banned,
178     c.banned_from_community,
179     c.creator_name,
180     c.score,
181     c.upvotes,
182     c.downvotes,
183     c.user_id,
184     c.my_vote,
185     c.saved,
186     um.recipient_id
187 from user_mention um, comment_view c
188 where um.comment_id = c.id;
189
190 -- community tables
191 drop view community_moderator_view;
192 drop view community_follower_view;
193 drop view community_user_ban_view;
194 drop view site_view;
195
196 create view community_moderator_view as 
197 select *,
198 (select name from user_ u where cm.user_id = u.id) as user_name,
199 (select name from community c where cm.community_id = c.id) as community_name
200 from community_moderator cm;
201
202 create view community_follower_view as 
203 select *,
204 (select name from user_ u where cf.user_id = u.id) as user_name,
205 (select name from community c where cf.community_id = c.id) as community_name
206 from community_follower cf;
207
208 create view community_user_ban_view as 
209 select *,
210 (select name from user_ u where cm.user_id = u.id) as user_name,
211 (select name from community c where cm.community_id = c.id) as community_name
212 from community_user_ban cm;
213
214 create view site_view as 
215 select *,
216 (select name from user_ u where s.creator_id = u.id) as creator_name,
217 (select count(*) from user_) as number_of_users,
218 (select count(*) from post) as number_of_posts,
219 (select count(*) from comment) as number_of_comments,
220 (select count(*) from community) as number_of_communities
221 from site s;
222
223 alter table user_ rename column avatar to icon;
224 alter table user_ alter column icon type bytea using icon::bytea;