1 -- Drop the dependent views
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;
11 alter table post alter column name type varchar(100);
14 create view post_aggregates_view as
17 (select u.banned from user_ u where p.creator_id = u.id) as banned,
18 (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,
19 (select name from user_ where p.creator_id = user_.id) as creator_name,
20 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
21 (select name from community where p.community_id = community.id) as community_name,
22 (select removed from community c where p.community_id = c.id) as community_removed,
23 (select deleted from community c where p.community_id = c.id) as community_deleted,
24 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
25 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
26 coalesce(sum(pl.score), 0) as score,
27 count (case when pl.score = 1 then 1 else null end) as upvotes,
28 count (case when pl.score = -1 then 1 else null end) as downvotes,
29 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
31 left join post_like pl on p.id = pl.post_id
34 create materialized view post_aggregates_mview as select * from post_aggregates_view;
36 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
38 create view post_view as
42 from post_aggregates_view pa
47 coalesce(pl.score, 0) as my_vote,
48 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
49 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
50 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
52 cross join all_post ap
53 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
67 create view post_mview as
71 from post_aggregates_mview pa
76 coalesce(pl.score, 0) as my_vote,
77 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
78 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
79 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
81 cross join all_post ap
82 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
98 create view mod_remove_post_view as
100 (select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
101 (select name from post p where mrp.post_id = p.id) as post_name,
102 (select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
103 (select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
104 from mod_remove_post mrp;
106 create view mod_lock_post_view as
108 (select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
109 (select name from post p where mlp.post_id = p.id) as post_name,
110 (select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
111 (select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
112 from mod_lock_post mlp;
114 create view mod_remove_comment_view as
116 (select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
117 (select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
118 (select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
119 (select content from comment c where mrc.comment_id = c.id) as comment_content,
120 (select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
121 (select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
122 (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,
123 (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
124 from mod_remove_comment mrc;
126 create view mod_sticky_post_view as
128 (select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
129 (select name from post p where msp.post_id = p.id) as post_name,
130 (select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
131 (select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
132 from mod_sticky_post msp;