diff --git a/crates/db_schema/src/schema_setup/diff_check.rs b/crates/db_schema/src/schema_setup/diff_check.rs index 35211b163..2f103bb83 100644 --- a/crates/db_schema/src/schema_setup/diff_check.rs +++ b/crates/db_schema/src/schema_setup/diff_check.rs @@ -252,11 +252,18 @@ fn remove_skipped_item_from_beginning(s: &str) -> Option<&str> { Some(after_first_occurence(after, "\n")) } // Skip old views and fast table triggers - else if let Some(after) = s.strip_prefix("CREATE VIEW ") + else if let Some(after) = s + .strip_prefix("CREATE VIEW ") .or_else(|| s.strip_prefix("CREATE OR REPLACE VIEW ")) .or_else(|| s.strip_prefix("CREATE MATERIALIZED VIEW ")) - .or_else(|| s.strip_prefix("CREATE FUNCTION public.").and_then(after_skipped_trigger_name)/*.and_then(|a| a.strip_prefix("()"))*/) - .or_else(|| s.strip_prefix("CREATE TRIGGER ").and_then(after_skipped_trigger_name)/*.and_then(|a| a.strip_prefix(' '))*/) + .or_else(|| { + s.strip_prefix("CREATE FUNCTION public.") + .and_then(after_skipped_trigger_name) + } /* .and_then(|a| a.strip_prefix("()")) */) + .or_else(|| { + s.strip_prefix("CREATE TRIGGER ") + .and_then(after_skipped_trigger_name) + } /* .and_then(|a| a.strip_prefix(' ')) */) { Some(after_first_occurence(after, "\n\n")) } else { diff --git a/migrations/2020-03-26-192410_add_activitypub_tables/down.sql b/migrations/2020-03-26-192410_add_activitypub_tables/down.sql index 88c86bb48..13a63141d 100644 --- a/migrations/2020-03-26-192410_add_activitypub_tables/down.sql +++ b/migrations/2020-03-26-192410_add_activitypub_tables/down.sql @@ -143,3 +143,4 @@ SELECT NULL AS subscribed FROM all_community ac; + diff --git a/migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/down.sql b/migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/down.sql index f2c6822f6..49b8f993a 100644 --- a/migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/down.sql +++ b/migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/down.sql @@ -1,14 +1,14 @@ DROP VIEW post_mview; - DROP MATERIALIZED VIEW post_aggregates_mview; +DROP MATERIALIZED VIEW post_aggregates_mview; - DROP VIEW post_view, post_aggregates_view; +DROP VIEW post_view, post_aggregates_view; - DROP VIEW user_mention_view, comment_view, user_mention_mview, reply_view, comment_mview; +DROP VIEW user_mention_view, comment_view, user_mention_mview, reply_view, comment_mview; - DROP MATERIALIZED VIEW comment_aggregates_mview; +DROP MATERIALIZED VIEW comment_aggregates_mview; - DROP VIEW comment_aggregates_view; +DROP VIEW comment_aggregates_view; ALTER TABLE post DROP COLUMN ap_id, @@ -19,525 +19,525 @@ ALTER TABLE comment DROP COLUMN local; -- Views are the same as before, except `*` does not reference the dropped columns - CREATE VIEW post_aggregates_view AS - SELECT - p.*, - ( - SELECT - u.banned - FROM - user_ u - WHERE - p.creator_id = u.id) AS banned, - ( - 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, - ( - SELECT - name - FROM - user_ - WHERE - p.creator_id = user_.id) AS creator_name, - ( - SELECT - avatar - FROM - user_ - WHERE - p.creator_id = user_.id) AS creator_avatar, - ( - SELECT - name - FROM - community - WHERE - p.community_id = community.id) AS community_name, - ( - SELECT - removed - FROM - community c - WHERE - p.community_id = c.id) AS community_removed, - ( - SELECT - deleted - FROM - community c - WHERE - p.community_id = c.id) AS community_deleted, - ( - SELECT - nsfw - FROM - community c - WHERE - p.community_id = c.id) AS community_nsfw, - ( - SELECT - count(*) - FROM - comment - WHERE - comment.post_id = p.id) AS number_of_comments, - coalesce(sum(pl.score), 0) AS score, - count( - CASE WHEN pl.score = 1 THEN - 1 - ELSE - NULL - END) AS upvotes, - count( - CASE WHEN pl.score = - 1 THEN - 1 - ELSE - NULL - END) AS downvotes, - hot_rank (coalesce(sum(pl.score), 0), ( - CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN - p.published -- Prevents necro-bumps - ELSE - greatest (c.recent_comment_time, p.published) - END)) AS hot_rank, - ( - CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN - p.published -- Prevents necro-bumps - ELSE - greatest (c.recent_comment_time, p.published) - END) AS newest_activity_time - FROM - post p - LEFT JOIN post_like pl ON p.id = pl.post_id - LEFT JOIN ( - SELECT - post_id, - max(published) AS recent_comment_time - FROM - comment - GROUP BY - 1) c ON p.id = c.post_id - GROUP BY - p.id, - c.recent_comment_time; +CREATE VIEW post_aggregates_view AS +SELECT + p.*, + ( + SELECT + u.banned + FROM + user_ u + WHERE + p.creator_id = u.id) AS banned, + ( + 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, + ( + SELECT + name + FROM + user_ + WHERE + p.creator_id = user_.id) AS creator_name, + ( + SELECT + avatar + FROM + user_ + WHERE + p.creator_id = user_.id) AS creator_avatar, + ( + SELECT + name + FROM + community + WHERE + p.community_id = community.id) AS community_name, + ( + SELECT + removed + FROM + community c + WHERE + p.community_id = c.id) AS community_removed, + ( + SELECT + deleted + FROM + community c + WHERE + p.community_id = c.id) AS community_deleted, + ( + SELECT + nsfw + FROM + community c + WHERE + p.community_id = c.id) AS community_nsfw, + ( + SELECT + count(*) + FROM + comment + WHERE + comment.post_id = p.id) AS number_of_comments, + coalesce(sum(pl.score), 0) AS score, + count( + CASE WHEN pl.score = 1 THEN + 1 + ELSE + NULL + END) AS upvotes, + count( + CASE WHEN pl.score = - 1 THEN + 1 + ELSE + NULL + END) AS downvotes, + hot_rank (coalesce(sum(pl.score), 0), ( + CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN + p.published -- Prevents necro-bumps + ELSE + greatest (c.recent_comment_time, p.published) + END)) AS hot_rank, + ( + CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN + p.published -- Prevents necro-bumps + ELSE + greatest (c.recent_comment_time, p.published) + END) AS newest_activity_time +FROM + post p + LEFT JOIN post_like pl ON p.id = pl.post_id + LEFT JOIN ( + SELECT + post_id, + max(published) AS recent_comment_time + FROM + comment + GROUP BY + 1) c ON p.id = c.post_id +GROUP BY + p.id, + c.recent_comment_time; - CREATE VIEW post_view AS - with all_post AS ( - SELECT - pa.* - FROM - post_aggregates_view pa - ) - SELECT - ap.*, - u.id AS user_id, - coalesce(pl.score, 0) AS my_vote, - ( - SELECT - cf.id::bool - FROM - community_follower cf - WHERE - u.id = cf.user_id - AND cf.community_id = ap.community_id) AS subscribed, - ( - SELECT - pr.id::bool - FROM - post_read pr - WHERE - u.id = pr.user_id - AND pr.post_id = ap.id) AS read, - ( - SELECT - ps.id::bool - FROM - post_saved ps - WHERE - u.id = ps.user_id - AND ps.post_id = ap.id) AS saved - FROM - user_ u - CROSS JOIN all_post ap - LEFT JOIN post_like pl ON u.id = pl.user_id - AND ap.id = pl.post_id - UNION ALL - SELECT - ap.*, - NULL AS user_id, - NULL AS my_vote, - NULL AS subscribed, - NULL AS read, - NULL AS saved - FROM - all_post ap; +CREATE VIEW post_view AS +with all_post AS ( + SELECT + pa.* + FROM + post_aggregates_view pa +) +SELECT + ap.*, + u.id AS user_id, + coalesce(pl.score, 0) AS my_vote, + ( + SELECT + cf.id::bool + FROM + community_follower cf + WHERE + u.id = cf.user_id + AND cf.community_id = ap.community_id) AS subscribed, + ( + SELECT + pr.id::bool + FROM + post_read pr + WHERE + u.id = pr.user_id + AND pr.post_id = ap.id) AS read, + ( + SELECT + ps.id::bool + FROM + post_saved ps + WHERE + u.id = ps.user_id + AND ps.post_id = ap.id) AS saved +FROM + user_ u + CROSS JOIN all_post ap + LEFT JOIN post_like pl ON u.id = pl.user_id + AND ap.id = pl.post_id + UNION ALL + SELECT + ap.*, + NULL AS user_id, + NULL AS my_vote, + NULL AS subscribed, + NULL AS read, + NULL AS saved + FROM + all_post ap; - CREATE MATERIALIZED VIEW post_aggregates_mview AS - SELECT - * - FROM - post_aggregates_view; +CREATE MATERIALIZED VIEW post_aggregates_mview AS +SELECT + * +FROM + post_aggregates_view; - CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id); +CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id); - CREATE VIEW post_mview AS - with all_post AS ( - SELECT - pa.* - FROM - post_aggregates_mview pa - ) - SELECT - ap.*, - u.id AS user_id, - coalesce(pl.score, 0) AS my_vote, - ( - SELECT - cf.id::bool - FROM - community_follower cf - WHERE - u.id = cf.user_id - AND cf.community_id = ap.community_id) AS subscribed, - ( - SELECT - pr.id::bool - FROM - post_read pr - WHERE - u.id = pr.user_id - AND pr.post_id = ap.id) AS read, - ( - SELECT - ps.id::bool - FROM - post_saved ps - WHERE - u.id = ps.user_id - AND ps.post_id = ap.id) AS saved - FROM - user_ u - CROSS JOIN all_post ap - LEFT JOIN post_like pl ON u.id = pl.user_id - AND ap.id = pl.post_id - UNION ALL - SELECT - ap.*, - NULL AS user_id, - NULL AS my_vote, - NULL AS subscribed, - NULL AS read, - NULL AS saved - FROM - all_post ap; +CREATE VIEW post_mview AS +with all_post AS ( + SELECT + pa.* + FROM + post_aggregates_mview pa +) +SELECT + ap.*, + u.id AS user_id, + coalesce(pl.score, 0) AS my_vote, + ( + SELECT + cf.id::bool + FROM + community_follower cf + WHERE + u.id = cf.user_id + AND cf.community_id = ap.community_id) AS subscribed, + ( + SELECT + pr.id::bool + FROM + post_read pr + WHERE + u.id = pr.user_id + AND pr.post_id = ap.id) AS read, + ( + SELECT + ps.id::bool + FROM + post_saved ps + WHERE + u.id = ps.user_id + AND ps.post_id = ap.id) AS saved +FROM + user_ u + CROSS JOIN all_post ap + LEFT JOIN post_like pl ON u.id = pl.user_id + AND ap.id = pl.post_id + UNION ALL + SELECT + ap.*, + NULL AS user_id, + NULL AS my_vote, + NULL AS subscribed, + NULL AS read, + NULL AS saved + FROM + all_post ap; - CREATE VIEW comment_aggregates_view AS - SELECT - c.*, - ( - SELECT - community_id - FROM - post p - WHERE - p.id = c.post_id), ( - SELECT - co.name - FROM - post p, - community co - WHERE - p.id = c.post_id - AND p.community_id = co.id) AS community_name, - ( - SELECT - u.banned - FROM - user_ u - WHERE - c.creator_id = u.id) AS banned, - ( - 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, - ( - SELECT - name - FROM - user_ - WHERE - c.creator_id = user_.id) AS creator_name, - ( - SELECT - avatar - FROM - user_ - WHERE - c.creator_id = user_.id) AS creator_avatar, - coalesce(sum(cl.score), 0) AS score, - count( - CASE WHEN cl.score = 1 THEN - 1 - ELSE - NULL - END) AS upvotes, - count( - CASE WHEN cl.score = - 1 THEN - 1 - ELSE - NULL - END) AS downvotes, - hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank - FROM - comment c - LEFT JOIN comment_like cl ON c.id = cl.comment_id - GROUP BY - c.id; +CREATE VIEW comment_aggregates_view AS +SELECT + c.*, + ( + SELECT + community_id + FROM + post p + WHERE + p.id = c.post_id), ( + SELECT + co.name + FROM + post p, + community co + WHERE + p.id = c.post_id + AND p.community_id = co.id) AS community_name, + ( + SELECT + u.banned + FROM + user_ u + WHERE + c.creator_id = u.id) AS banned, + ( + 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, + ( + SELECT + name + FROM + user_ + WHERE + c.creator_id = user_.id) AS creator_name, + ( + SELECT + avatar + FROM + user_ + WHERE + c.creator_id = user_.id) AS creator_avatar, + coalesce(sum(cl.score), 0) AS score, + count( + CASE WHEN cl.score = 1 THEN + 1 + ELSE + NULL + END) AS upvotes, + count( + CASE WHEN cl.score = - 1 THEN + 1 + ELSE + NULL + END) AS downvotes, + hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank +FROM + comment c + LEFT JOIN comment_like cl ON c.id = cl.comment_id +GROUP BY + c.id; - CREATE MATERIALIZED VIEW comment_aggregates_mview AS - SELECT - * - FROM - comment_aggregates_view; +CREATE MATERIALIZED VIEW comment_aggregates_mview AS +SELECT + * +FROM + comment_aggregates_view; - CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id); +CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id); - CREATE VIEW comment_mview AS - with all_comment AS ( - SELECT - ca.* - FROM - comment_aggregates_mview ca - ) - SELECT - ac.*, - u.id AS user_id, - coalesce(cl.score, 0) AS my_vote, - ( - SELECT - cf.id::boolean - FROM - community_follower cf - WHERE - u.id = cf.user_id - AND ac.community_id = cf.community_id) AS subscribed, - ( - SELECT - cs.id::bool - FROM - comment_saved cs - WHERE - u.id = cs.user_id - AND cs.comment_id = ac.id) AS saved - FROM - user_ u - CROSS JOIN all_comment ac - LEFT JOIN comment_like cl ON u.id = cl.user_id - AND ac.id = cl.comment_id - UNION ALL - SELECT - ac.*, - NULL AS user_id, - NULL AS my_vote, - NULL AS subscribed, - NULL AS saved - FROM - all_comment ac; +CREATE VIEW comment_mview AS +with all_comment AS ( + SELECT + ca.* + FROM + comment_aggregates_mview ca +) +SELECT + ac.*, + u.id AS user_id, + coalesce(cl.score, 0) AS my_vote, + ( + SELECT + cf.id::boolean + FROM + community_follower cf + WHERE + u.id = cf.user_id + AND ac.community_id = cf.community_id) AS subscribed, + ( + SELECT + cs.id::bool + FROM + comment_saved cs + WHERE + u.id = cs.user_id + AND cs.comment_id = ac.id) AS saved +FROM + user_ u + CROSS JOIN all_comment ac + LEFT JOIN comment_like cl ON u.id = cl.user_id + AND ac.id = cl.comment_id + UNION ALL + SELECT + ac.*, + NULL AS user_id, + NULL AS my_vote, + NULL AS subscribed, + NULL AS saved + FROM + all_comment ac; - CREATE VIEW reply_view AS - with closereply AS ( - SELECT - c2.id, - c2.creator_id AS sender_id, - c.creator_id AS recipient_id - FROM - comment c - INNER JOIN comment c2 ON c.id = c2.parent_id - WHERE - c2.creator_id != c.creator_id - -- Do union where post is null - UNION - SELECT - c.id, - c.creator_id AS sender_id, - p.creator_id AS recipient_id - FROM - comment c, - post p - WHERE - c.post_id = p.id - AND c.parent_id IS NULL - AND c.creator_id != p.creator_id - ) - SELECT - cv.*, - closereply.recipient_id - FROM - comment_mview cv, - closereply - WHERE - closereply.id = cv.id; +CREATE VIEW reply_view AS +with closereply AS ( + SELECT + c2.id, + c2.creator_id AS sender_id, + c.creator_id AS recipient_id + FROM + comment c + INNER JOIN comment c2 ON c.id = c2.parent_id + WHERE + c2.creator_id != c.creator_id + -- Do union where post is null + UNION + SELECT + c.id, + c.creator_id AS sender_id, + p.creator_id AS recipient_id + FROM + comment c, + post p + WHERE + c.post_id = p.id + AND c.parent_id IS NULL + AND c.creator_id != p.creator_id +) +SELECT + cv.*, + closereply.recipient_id +FROM + comment_mview cv, + closereply +WHERE + closereply.id = cv.id; - CREATE VIEW user_mention_mview AS - with all_comment AS ( - SELECT - ca.* - FROM - comment_aggregates_mview ca - ) - SELECT - ac.id, - um.id AS user_mention_id, - ac.creator_id, - ac.post_id, - ac.parent_id, - ac.content, - ac.removed, - um.read, - ac.published, - ac.updated, - ac.deleted, - ac.community_id, - ac.community_name, - ac.banned, - ac.banned_from_community, - ac.creator_name, - ac.creator_avatar, - ac.score, - ac.upvotes, - ac.downvotes, - ac.hot_rank, - u.id AS user_id, - coalesce(cl.score, 0) AS my_vote, - ( - SELECT - cs.id::bool - FROM - comment_saved cs - WHERE - u.id = cs.user_id - AND cs.comment_id = ac.id) AS saved, - um.recipient_id - FROM - user_ u - CROSS JOIN all_comment ac - LEFT JOIN comment_like cl ON u.id = cl.user_id - AND ac.id = cl.comment_id - LEFT JOIN user_mention um ON um.comment_id = ac.id - UNION ALL - SELECT - ac.id, - um.id AS user_mention_id, - ac.creator_id, - ac.post_id, - ac.parent_id, - ac.content, - ac.removed, - um.read, - ac.published, - ac.updated, - ac.deleted, - ac.community_id, - ac.community_name, - ac.banned, - ac.banned_from_community, - ac.creator_name, - ac.creator_avatar, - ac.score, - ac.upvotes, - ac.downvotes, - ac.hot_rank, - NULL AS user_id, - NULL AS my_vote, - NULL AS saved, - um.recipient_id - FROM - all_comment ac - LEFT JOIN user_mention um ON um.comment_id = ac.id; +CREATE VIEW user_mention_mview AS +with all_comment AS ( + SELECT + ca.* + FROM + comment_aggregates_mview ca +) +SELECT + ac.id, + um.id AS user_mention_id, + ac.creator_id, + ac.post_id, + ac.parent_id, + ac.content, + ac.removed, + um.read, + ac.published, + ac.updated, + ac.deleted, + ac.community_id, + ac.community_name, + ac.banned, + ac.banned_from_community, + ac.creator_name, + ac.creator_avatar, + ac.score, + ac.upvotes, + ac.downvotes, + ac.hot_rank, + u.id AS user_id, + coalesce(cl.score, 0) AS my_vote, + ( + SELECT + cs.id::bool + FROM + comment_saved cs + WHERE + u.id = cs.user_id + AND cs.comment_id = ac.id) AS saved, + um.recipient_id +FROM + user_ u + CROSS JOIN all_comment ac + LEFT JOIN comment_like cl ON u.id = cl.user_id + AND ac.id = cl.comment_id + LEFT JOIN user_mention um ON um.comment_id = ac.id +UNION ALL +SELECT + ac.id, + um.id AS user_mention_id, + ac.creator_id, + ac.post_id, + ac.parent_id, + ac.content, + ac.removed, + um.read, + ac.published, + ac.updated, + ac.deleted, + ac.community_id, + ac.community_name, + ac.banned, + ac.banned_from_community, + ac.creator_name, + ac.creator_avatar, + ac.score, + ac.upvotes, + ac.downvotes, + ac.hot_rank, + NULL AS user_id, + NULL AS my_vote, + NULL AS saved, + um.recipient_id +FROM + all_comment ac + LEFT JOIN user_mention um ON um.comment_id = ac.id; - CREATE VIEW comment_view AS - with all_comment AS ( - SELECT - ca.* - FROM - comment_aggregates_view ca - ) - SELECT - ac.*, - u.id AS user_id, - coalesce(cl.score, 0) AS my_vote, - ( - SELECT - cf.id::boolean - FROM - community_follower cf - WHERE - u.id = cf.user_id - AND ac.community_id = cf.community_id) AS subscribed, - ( - SELECT - cs.id::bool - FROM - comment_saved cs - WHERE - u.id = cs.user_id - AND cs.comment_id = ac.id) AS saved - FROM - user_ u - CROSS JOIN all_comment ac - LEFT JOIN comment_like cl ON u.id = cl.user_id - AND ac.id = cl.comment_id - UNION ALL - SELECT - ac.*, - NULL AS user_id, - NULL AS my_vote, - NULL AS subscribed, - NULL AS saved - FROM - all_comment ac; +CREATE VIEW comment_view AS +with all_comment AS ( + SELECT + ca.* + FROM + comment_aggregates_view ca +) +SELECT + ac.*, + u.id AS user_id, + coalesce(cl.score, 0) AS my_vote, + ( + SELECT + cf.id::boolean + FROM + community_follower cf + WHERE + u.id = cf.user_id + AND ac.community_id = cf.community_id) AS subscribed, + ( + SELECT + cs.id::bool + FROM + comment_saved cs + WHERE + u.id = cs.user_id + AND cs.comment_id = ac.id) AS saved +FROM + user_ u + CROSS JOIN all_comment ac + LEFT JOIN comment_like cl ON u.id = cl.user_id + AND ac.id = cl.comment_id + UNION ALL + SELECT + ac.*, + NULL AS user_id, + NULL AS my_vote, + NULL AS subscribed, + NULL AS saved + FROM + all_comment ac; - CREATE VIEW user_mention_view AS - SELECT - c.id, - um.id AS user_mention_id, - c.creator_id, - c.post_id, - c.parent_id, - c.content, - c.removed, - um.read, - c.published, - c.updated, - c.deleted, - c.community_id, - c.community_name, - c.banned, - c.banned_from_community, - c.creator_name, - c.creator_avatar, - c.score, - c.upvotes, - c.downvotes, - c.hot_rank, - c.user_id, - c.my_vote, - c.saved, - um.recipient_id - FROM - user_mention um, - comment_view c - WHERE - um.comment_id = c.id; +CREATE VIEW user_mention_view AS +SELECT + c.id, + um.id AS user_mention_id, + c.creator_id, + c.post_id, + c.parent_id, + c.content, + c.removed, + um.read, + c.published, + c.updated, + c.deleted, + c.community_id, + c.community_name, + c.banned, + c.banned_from_community, + c.creator_name, + c.creator_avatar, + c.score, + c.upvotes, + c.downvotes, + c.hot_rank, + c.user_id, + c.my_vote, + c.saved, + um.recipient_id +FROM + user_mention um, + comment_view c +WHERE + um.comment_id = c.id;