]> Untitled Git - lemmy.git/blob - server/migrations/2020-02-06-165953_change_post_title_length/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2020-02-06-165953_change_post_title_length / up.sql
1 -- Drop the dependent views
2 drop view post_view;
3 drop view post_mview;
4 drop materialized view post_aggregates_mview;
5 drop view post_aggregates_view;
6 drop view mod_remove_post_view;
7 drop view mod_sticky_post_view;
8 drop view mod_lock_post_view;
9 drop view mod_remove_comment_view;
10
11 -- Add the extra post limit
12 alter table post alter column name type varchar(200);
13
14 -- regen post view
15 create view post_aggregates_view as
16 select        
17 p.*,
18 (select u.banned from user_ u where p.creator_id = u.id) as banned,
19 (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,
20 (select name from user_ where p.creator_id = user_.id) as creator_name,
21 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
22 (select name from community where p.community_id = community.id) as community_name,
23 (select removed from community c where p.community_id = c.id) as community_removed,
24 (select deleted from community c where p.community_id = c.id) as community_deleted,
25 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
26 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
27 coalesce(sum(pl.score), 0) as score,
28 count (case when pl.score = 1 then 1 else null end) as upvotes,
29 count (case when pl.score = -1 then 1 else null end) as downvotes,
30 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
31 from post p
32 left join post_like pl on p.id = pl.post_id
33 group by p.id;
34
35 create materialized view post_aggregates_mview as select * from post_aggregates_view;
36
37 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
38
39 create view post_view as 
40 with all_post as (
41   select
42   pa.*
43   from post_aggregates_view pa
44 )
45 select
46 ap.*,
47 u.id as user_id,
48 coalesce(pl.score, 0) as my_vote,
49 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
50 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
51 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
52 from user_ u
53 cross join all_post ap
54 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
55
56 union all
57
58 select 
59 ap.*,
60 null as user_id,
61 null as my_vote,
62 null as subscribed,
63 null as read,
64 null as saved
65 from all_post ap
66 ;
67
68 create view post_mview as 
69 with all_post as (
70   select
71   pa.*
72   from post_aggregates_mview pa
73 )
74 select
75 ap.*,
76 u.id as user_id,
77 coalesce(pl.score, 0) as my_vote,
78 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
79 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
80 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
81 from user_ u
82 cross join all_post ap
83 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
84
85 union all
86
87 select 
88 ap.*,
89 null as user_id,
90 null as my_vote,
91 null as subscribed,
92 null as read,
93 null as saved
94 from all_post ap
95 ;
96
97 -- The mod views
98
99 create view mod_remove_post_view as 
100 select mrp.*,
101 (select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
102 (select name from post p where mrp.post_id = p.id) as post_name,
103 (select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
104 (select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
105 from mod_remove_post mrp;
106
107 create view mod_lock_post_view as 
108 select mlp.*,
109 (select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
110 (select name from post p where mlp.post_id = p.id) as post_name,
111 (select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
112 (select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
113 from mod_lock_post mlp;
114
115 create view mod_remove_comment_view as 
116 select mrc.*,
117 (select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
118 (select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
119 (select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
120 (select content from comment c where mrc.comment_id = c.id) as comment_content,
121 (select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
122 (select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
123 (select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id, 
124 (select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
125 from mod_remove_comment mrc;
126
127 create view mod_sticky_post_view as 
128 select msp.*,
129 (select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
130 (select name from post p where msp.post_id = p.id) as post_name,
131 (select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
132 (select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
133 from mod_sticky_post msp;