Revert "Remove mat views, no fast tables or caching added yet."

This reverts commit 97e088dcbf.
This commit is contained in:
Dessalines 2020-06-29 21:00:28 -04:00
parent c10a05cb68
commit 80bca8610e
8 changed files with 230 additions and 55 deletions

View File

@ -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;

View File

@ -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;

View File

@ -38,6 +38,41 @@ table! {
}
}
table! {
comment_mview (id) {
id -> Int4,
creator_id -> Int4,
post_id -> Int4,
parent_id -> Nullable<Int4>,
content -> Text,
removed -> Bool,
read -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
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<Text>,
score -> BigInt,
upvotes -> BigInt,
downvotes -> BigInt,
hot_rank -> Int4,
user_id -> Nullable<Int4>,
my_vote -> Nullable<Int4>,
subscribed -> Nullable<Bool>,
saved -> Nullable<Bool>,
}
}
#[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<i32>,
@ -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<Vec<CommentView>, 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<i32>,
) -> Result<Self, Error> {
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 {

View File

@ -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<Text>,
category_id -> Int4,
creator_id -> Int4,
removed -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
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<Text>,
category_name -> Varchar,
number_of_subscribers -> BigInt,
number_of_posts -> BigInt,
number_of_comments -> BigInt,
hot_rank -> Int4,
user_id -> Nullable<Int4>,
subscribed -> Nullable<Bool>,
}
}
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<Vec<CommunityView>, 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<i32>,
) -> Result<Self, Error> {
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));

View File

@ -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<Text>,
body -> Nullable<Text>,
creator_id -> Int4,
community_id -> Int4,
removed -> Bool,
locked -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
deleted -> Bool,
nsfw -> Bool,
stickied -> Bool,
embed_title -> Nullable<Text>,
embed_description -> Nullable<Text>,
embed_html -> Nullable<Text>,
thumbnail_url -> Nullable<Text>,
ap_id -> Text,
local -> Bool,
banned -> Bool,
banned_from_community -> Bool,
creator_actor_id -> Text,
creator_local -> Bool,
creator_name -> Varchar,
creator_avatar -> Nullable<Text>,
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<Int4>,
my_vote -> Nullable<Int4>,
subscribed -> Nullable<Bool>,
read -> Nullable<Bool>,
saved -> Nullable<Bool>,
}
}
#[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<Vec<PostView>, 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<i32>,
) -> Result<Self, Error> {
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));

View File

@ -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<Timestamp>,
ap_id -> Text,
local -> Bool,
creator_name -> Varchar,
creator_avatar -> Nullable<Text>,
creator_actor_id -> Text,
creator_local -> Bool,
recipient_name -> Varchar,
recipient_avatar -> Nullable<Text>,
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<i64>,
@ -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<Vec<PrivateMessageView>, 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));

View File

@ -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<Int4>,
content -> Text,
removed -> Bool,
read -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
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<Text>,
score -> BigInt,
upvotes -> BigInt,
downvotes -> BigInt,
hot_rank -> Int4,
user_id -> Nullable<Int4>,
my_vote -> Nullable<Int4>,
saved -> Nullable<Bool>,
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<Vec<UserMentionView>, Error> {
use super::user_mention_view::user_mention_view::dsl::*;
use super::user_mention_view::user_mention_mview::dsl::*;
let mut query = self.query;

View File

@ -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<Text>,
email -> Nullable<Text>,
matrix_user_id -> Nullable<Text>,
bio -> Nullable<Text>,
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<T: MaybeOptional<String>>(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<Vec<UserView>, 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<Self, Error> {
use super::user_view::user_view::dsl::*;
user_view.find(from_user_id).first::<Self>(conn)
use super::user_view::user_mview::dsl::*;
user_mview.find(from_user_id).first::<Self>(conn)
}
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use super::user_view::user_view::dsl::*;
user_view.filter(admin.eq(true)).load::<Self>(conn)
use super::user_view::user_mview::dsl::*;
user_mview.filter(admin.eq(true)).load::<Self>(conn)
}
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use super::user_view::user_view::dsl::*;
user_view.filter(banned.eq(true)).load::<Self>(conn)
use super::user_view::user_mview::dsl::*;
user_mview.filter(banned.eq(true)).load::<Self>(conn)
}
}