From 80bca8610eb4cd826578969b0fdd3e3a6b5bfc90 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 29 Jun 2020 21:00:28 -0400 Subject: [PATCH] Revert "Remove mat views, no fast tables or caching added yet." This reverts commit 97e088dcbfb91bb16bd0190f8d956095755e8cb4. --- .../down.sql | 9 --- .../2020-06-29-222617_remove_mat_views/up.sql | 9 --- server/src/db/comment_view.rs | 47 +++++++++++++-- server/src/db/community_view.rs | 42 +++++++++++-- server/src/db/post_view.rs | 59 +++++++++++++++++-- server/src/db/private_message_view.rs | 31 ++++++++-- server/src/db/user_mention_view.rs | 44 ++++++++++++-- server/src/db/user_view.rs | 44 ++++++++++---- 8 files changed, 230 insertions(+), 55 deletions(-) delete mode 100644 server/migrations/2020-06-29-222617_remove_mat_views/down.sql delete mode 100644 server/migrations/2020-06-29-222617_remove_mat_views/up.sql diff --git a/server/migrations/2020-06-29-222617_remove_mat_views/down.sql b/server/migrations/2020-06-29-222617_remove_mat_views/down.sql deleted file mode 100644 index 7c4908638..000000000 --- a/server/migrations/2020-06-29-222617_remove_mat_views/down.sql +++ /dev/null @@ -1,9 +0,0 @@ -alter table comment enable trigger refresh_comment; -alter table user_ enable trigger refresh_user; -alter table post enable trigger refresh_post; -alter table community enable trigger refresh_community; -alter table private_message enable trigger refresh_private_message; -alter table post_like enable trigger refresh_post_like; -alter table community_follower enable trigger refresh_community_follower; -alter table community_user_ban enable trigger refresh_community_user_ban; -alter table comment_like enable trigger refresh_comment_like; diff --git a/server/migrations/2020-06-29-222617_remove_mat_views/up.sql b/server/migrations/2020-06-29-222617_remove_mat_views/up.sql deleted file mode 100644 index eb0fb9382..000000000 --- a/server/migrations/2020-06-29-222617_remove_mat_views/up.sql +++ /dev/null @@ -1,9 +0,0 @@ -alter table comment disable trigger refresh_comment; -alter table user_ disable trigger refresh_user; -alter table post disable trigger refresh_post; -alter table community disable trigger refresh_community; -alter table private_message disable trigger refresh_private_message; -alter table post_like disable trigger refresh_post_like; -alter table community_follower disable trigger refresh_community_follower; -alter table community_user_ban disable trigger refresh_community_user_ban; -alter table comment_like disable trigger refresh_comment_like; diff --git a/server/src/db/comment_view.rs b/server/src/db/comment_view.rs index f96d9534d..a37cdbcd6 100644 --- a/server/src/db/comment_view.rs +++ b/server/src/db/comment_view.rs @@ -38,6 +38,41 @@ table! { } } +table! { + comment_mview (id) { + id -> Int4, + creator_id -> Int4, + post_id -> Int4, + parent_id -> Nullable, + content -> Text, + removed -> Bool, + read -> Bool, + published -> Timestamp, + updated -> Nullable, + deleted -> Bool, + ap_id -> Text, + local -> Bool, + community_id -> Int4, + community_actor_id -> Text, + community_local -> Bool, + community_name -> Varchar, + banned -> Bool, + banned_from_community -> Bool, + creator_actor_id -> Text, + creator_local -> Bool, + creator_name -> Varchar, + creator_avatar -> Nullable, + score -> BigInt, + upvotes -> BigInt, + downvotes -> BigInt, + hot_rank -> Int4, + user_id -> Nullable, + my_vote -> Nullable, + subscribed -> Nullable, + saved -> Nullable, + } +} + #[derive( Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone, )] @@ -77,7 +112,7 @@ pub struct CommentView { pub struct CommentQueryBuilder<'a> { conn: &'a PgConnection, - query: super::comment_view::comment_view::BoxedQuery<'a, Pg>, + query: super::comment_view::comment_mview::BoxedQuery<'a, Pg>, listing_type: ListingType, sort: &'a SortType, for_community_id: Option, @@ -92,9 +127,9 @@ pub struct CommentQueryBuilder<'a> { impl<'a> CommentQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { - use super::comment_view::comment_view::dsl::*; + use super::comment_view::comment_mview::dsl::*; - let query = comment_view.into_boxed(); + let query = comment_mview.into_boxed(); CommentQueryBuilder { conn, @@ -163,7 +198,7 @@ impl<'a> CommentQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::comment_view::comment_view::dsl::*; + use super::comment_view::comment_mview::dsl::*; let mut query = self.query; @@ -235,8 +270,8 @@ impl CommentView { from_comment_id: i32, my_user_id: Option, ) -> Result { - use super::comment_view::comment_view::dsl::*; - let mut query = comment_view.into_boxed(); + use super::comment_view::comment_mview::dsl::*; + let mut query = comment_mview.into_boxed(); // The view lets you pass a null user_id, if you're not logged in if let Some(my_user_id) = my_user_id { diff --git a/server/src/db/community_view.rs b/server/src/db/community_view.rs index 606ec13ce..ea7b2a7ca 100644 --- a/server/src/db/community_view.rs +++ b/server/src/db/community_view.rs @@ -1,4 +1,4 @@ -use super::community_view::community_view::BoxedQuery; +use super::community_view::community_mview::BoxedQuery; use crate::db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType}; use diesel::{pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -33,6 +33,36 @@ table! { } } +table! { + community_mview (id) { + id -> Int4, + name -> Varchar, + title -> Varchar, + description -> Nullable, + category_id -> Int4, + creator_id -> Int4, + removed -> Bool, + published -> Timestamp, + updated -> Nullable, + deleted -> Bool, + nsfw -> Bool, + actor_id -> Text, + local -> Bool, + last_refreshed_at -> Timestamp, + creator_actor_id -> Text, + creator_local -> Bool, + creator_name -> Varchar, + creator_avatar -> Nullable, + category_name -> Varchar, + number_of_subscribers -> BigInt, + number_of_posts -> BigInt, + number_of_comments -> BigInt, + hot_rank -> Int4, + user_id -> Nullable, + subscribed -> Nullable, + } +} + table! { community_moderator_view (id) { id -> Int4, @@ -126,9 +156,9 @@ pub struct CommunityQueryBuilder<'a> { impl<'a> CommunityQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { - use super::community_view::community_view::dsl::*; + use super::community_view::community_mview::dsl::*; - let query = community_view.into_boxed(); + let query = community_mview.into_boxed(); CommunityQueryBuilder { conn, @@ -173,7 +203,7 @@ impl<'a> CommunityQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::community_view::community_view::dsl::*; + use super::community_view::community_mview::dsl::*; let mut query = self.query; @@ -229,9 +259,9 @@ impl CommunityView { from_community_id: i32, from_user_id: Option, ) -> Result { - use super::community_view::community_view::dsl::*; + use super::community_view::community_mview::dsl::*; - let mut query = community_view.into_boxed(); + let mut query = community_mview.into_boxed(); query = query.filter(id.eq(from_community_id)); diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index 6865402fb..fbbf658d3 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -1,4 +1,4 @@ -use super::post_view::post_view::BoxedQuery; +use super::post_view::post_mview::BoxedQuery; use crate::db::{fuzzy_search, limit_and_offset, ListingType, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -51,6 +51,53 @@ table! { } } +table! { + post_mview (id) { + id -> Int4, + name -> Varchar, + url -> Nullable, + body -> Nullable, + creator_id -> Int4, + community_id -> Int4, + removed -> Bool, + locked -> Bool, + published -> Timestamp, + updated -> Nullable, + deleted -> Bool, + nsfw -> Bool, + stickied -> Bool, + embed_title -> Nullable, + embed_description -> Nullable, + embed_html -> Nullable, + thumbnail_url -> Nullable, + ap_id -> Text, + local -> Bool, + banned -> Bool, + banned_from_community -> Bool, + creator_actor_id -> Text, + creator_local -> Bool, + creator_name -> Varchar, + creator_avatar -> Nullable, + community_actor_id -> Text, + community_local -> Bool, + community_name -> Varchar, + community_removed -> Bool, + community_deleted -> Bool, + community_nsfw -> Bool, + number_of_comments -> BigInt, + score -> BigInt, + upvotes -> BigInt, + downvotes -> BigInt, + hot_rank -> Int4, + newest_activity_time -> Timestamp, + user_id -> Nullable, + my_vote -> Nullable, + subscribed -> Nullable, + read -> Nullable, + saved -> Nullable, + } +} + #[derive( Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone, )] @@ -119,9 +166,9 @@ pub struct PostQueryBuilder<'a> { impl<'a> PostQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { - use super::post_view::post_view::dsl::*; + use super::post_view::post_mview::dsl::*; - let query = post_view.into_boxed(); + let query = post_mview.into_boxed(); PostQueryBuilder { conn, @@ -202,7 +249,7 @@ impl<'a> PostQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::post_view::post_view::dsl::*; + use super::post_view::post_mview::dsl::*; let mut query = self.query; @@ -298,10 +345,10 @@ impl PostView { from_post_id: i32, my_user_id: Option, ) -> Result { - use super::post_view::post_view::dsl::*; + use super::post_view::post_mview::dsl::*; use diesel::prelude::*; - let mut query = post_view.into_boxed(); + let mut query = post_mview.into_boxed(); query = query.filter(id.eq(from_post_id)); diff --git a/server/src/db/private_message_view.rs b/server/src/db/private_message_view.rs index 899a1084d..9a1df4397 100644 --- a/server/src/db/private_message_view.rs +++ b/server/src/db/private_message_view.rs @@ -26,6 +26,29 @@ table! { } } +table! { + private_message_mview (id) { + id -> Int4, + creator_id -> Int4, + recipient_id -> Int4, + content -> Text, + deleted -> Bool, + read -> Bool, + published -> Timestamp, + updated -> Nullable, + ap_id -> Text, + local -> Bool, + creator_name -> Varchar, + creator_avatar -> Nullable, + creator_actor_id -> Text, + creator_local -> Bool, + recipient_name -> Varchar, + recipient_avatar -> Nullable, + recipient_actor_id -> Text, + recipient_local -> Bool, + } +} + #[derive( Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone, )] @@ -53,7 +76,7 @@ pub struct PrivateMessageView { pub struct PrivateMessageQueryBuilder<'a> { conn: &'a PgConnection, - query: super::private_message_view::private_message_view::BoxedQuery<'a, Pg>, + query: super::private_message_view::private_message_mview::BoxedQuery<'a, Pg>, for_recipient_id: i32, unread_only: bool, page: Option, @@ -62,9 +85,9 @@ pub struct PrivateMessageQueryBuilder<'a> { impl<'a> PrivateMessageQueryBuilder<'a> { pub fn create(conn: &'a PgConnection, for_recipient_id: i32) -> Self { - use super::private_message_view::private_message_view::dsl::*; + use super::private_message_view::private_message_mview::dsl::*; - let query = private_message_view.into_boxed(); + let query = private_message_mview.into_boxed(); PrivateMessageQueryBuilder { conn, @@ -92,7 +115,7 @@ impl<'a> PrivateMessageQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::private_message_view::private_message_view::dsl::*; + use super::private_message_view::private_message_mview::dsl::*; let mut query = self.query.filter(deleted.eq(false)); diff --git a/server/src/db/user_mention_view.rs b/server/src/db/user_mention_view.rs index 41ade0bb6..100445b99 100644 --- a/server/src/db/user_mention_view.rs +++ b/server/src/db/user_mention_view.rs @@ -39,6 +39,42 @@ table! { } } +table! { + user_mention_mview (id) { + id -> Int4, + user_mention_id -> Int4, + creator_id -> Int4, + creator_actor_id -> Text, + creator_local -> Bool, + post_id -> Int4, + parent_id -> Nullable, + content -> Text, + removed -> Bool, + read -> Bool, + published -> Timestamp, + updated -> Nullable, + deleted -> Bool, + community_id -> Int4, + community_actor_id -> Text, + community_local -> Bool, + community_name -> Varchar, + banned -> Bool, + banned_from_community -> Bool, + creator_name -> Varchar, + creator_avatar -> Nullable, + score -> BigInt, + upvotes -> BigInt, + downvotes -> BigInt, + hot_rank -> Int4, + user_id -> Nullable, + my_vote -> Nullable, + saved -> Nullable, + recipient_id -> Int4, + recipient_actor_id -> Text, + recipient_local -> Bool, + } +} + #[derive( Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone, )] @@ -79,7 +115,7 @@ pub struct UserMentionView { pub struct UserMentionQueryBuilder<'a> { conn: &'a PgConnection, - query: super::user_mention_view::user_mention_view::BoxedQuery<'a, Pg>, + query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>, for_user_id: i32, sort: &'a SortType, unread_only: bool, @@ -89,9 +125,9 @@ pub struct UserMentionQueryBuilder<'a> { impl<'a> UserMentionQueryBuilder<'a> { pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self { - use super::user_mention_view::user_mention_view::dsl::*; + use super::user_mention_view::user_mention_mview::dsl::*; - let query = user_mention_view.into_boxed(); + let query = user_mention_mview.into_boxed(); UserMentionQueryBuilder { conn, @@ -125,7 +161,7 @@ impl<'a> UserMentionQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::user_mention_view::user_mention_view::dsl::*; + use super::user_mention_view::user_mention_mview::dsl::*; let mut query = self.query; diff --git a/server/src/db/user_view.rs b/server/src/db/user_view.rs index e717e0387..57e2a4c9c 100644 --- a/server/src/db/user_view.rs +++ b/server/src/db/user_view.rs @@ -1,4 +1,4 @@ -use super::user_view::user_view::BoxedQuery; +use super::user_view::user_mview::BoxedQuery; use crate::db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -25,6 +25,28 @@ table! { } } +table! { + user_mview (id) { + id -> Int4, + actor_id -> Text, + name -> Varchar, + avatar -> Nullable, + email -> Nullable, + matrix_user_id -> Nullable, + bio -> Nullable, + local -> Bool, + admin -> Bool, + banned -> Bool, + show_avatars -> Bool, + send_notifications_to_email -> Bool, + published -> Timestamp, + number_of_posts -> BigInt, + post_score -> BigInt, + number_of_comments -> BigInt, + comment_score -> BigInt, + } +} + #[derive( Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone, )] @@ -59,9 +81,9 @@ pub struct UserQueryBuilder<'a> { impl<'a> UserQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { - use super::user_view::user_view::dsl::*; + use super::user_view::user_mview::dsl::*; - let query = user_view.into_boxed(); + let query = user_mview.into_boxed(); UserQueryBuilder { conn, @@ -78,7 +100,7 @@ impl<'a> UserQueryBuilder<'a> { } pub fn search_term>(mut self, search_term: T) -> Self { - use super::user_view::user_view::dsl::*; + use super::user_view::user_mview::dsl::*; if let Some(search_term) = search_term.get_optional() { self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); } @@ -96,7 +118,7 @@ impl<'a> UserQueryBuilder<'a> { } pub fn list(self) -> Result, Error> { - use super::user_view::user_view::dsl::*; + use super::user_view::user_mview::dsl::*; let mut query = self.query; @@ -129,17 +151,17 @@ impl<'a> UserQueryBuilder<'a> { impl UserView { pub fn read(conn: &PgConnection, from_user_id: i32) -> Result { - use super::user_view::user_view::dsl::*; - user_view.find(from_user_id).first::(conn) + use super::user_view::user_mview::dsl::*; + user_mview.find(from_user_id).first::(conn) } pub fn admins(conn: &PgConnection) -> Result, Error> { - use super::user_view::user_view::dsl::*; - user_view.filter(admin.eq(true)).load::(conn) + use super::user_view::user_mview::dsl::*; + user_mview.filter(admin.eq(true)).load::(conn) } pub fn banned(conn: &PgConnection) -> Result, Error> { - use super::user_view::user_view::dsl::*; - user_view.filter(banned.eq(true)).load::(conn) + use super::user_view::user_mview::dsl::*; + user_mview.filter(banned.eq(true)).load::(conn) } }