diff options
Diffstat (limited to 'src/db/mod.rs')
| -rw-r--r-- | src/db/mod.rs | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index 5f19d54..3362e33 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,12 @@ -pub use self::models::*; -use self::schema::*; -use crate::db::schema::memes::title; +use std::{ + convert::{ + AsRef, + From, + }, + env, + str::FromStr, +}; + use anyhow::{ anyhow, Error, @@ -19,7 +25,6 @@ use deadpool_postgres::{ }; use diesel::{ prelude::*, - row::NamedRow, BoolExpressionMethods, ExpressionMethods, NotFound, @@ -37,22 +42,18 @@ use diesel_async::{ RunQueryDsl, }; use grate::tracing; -use rand::Rng; use serenity::FutureExt; -use std::{ - convert::{ - AsRef, - From, - }, - env, - str::FromStr, -}; use tokio_postgres::types::FromSql; +use self::schema::*; + pub mod manual_migrate; + mod models; mod schema; +pub use self::models::*; + static MIGRATIONS: diesel_async_migrations::EmbeddedMigrations = diesel_async_migrations::embed_migrations!(); @@ -82,7 +83,7 @@ lazy_static::lazy_static! { #[inline] pub async fn connection() - -> Result<diesel_async::pooled_connection::deadpool::Object<AsyncPgConnection>> { +-> Result<diesel_async::pooled_connection::deadpool::Object<AsyncPgConnection>> { POOL.get() .then(|mut conn| async move { if let Ok(ref mut conn) = conn { @@ -194,8 +195,8 @@ pub async fn query_meme<T: AsRef<str>>( }; let metadata = Metadata { - id: row.get(5), - created: row.get(6), + id: row.get(5), + created: row.get(6), created_by: row.get(7), }; @@ -294,19 +295,19 @@ pub async fn delete_meme<T: AsRef<str>>( } let tombstone = NewTombstone { - guild: guild as i64, - deleted_by: deleted_by as i64, + guild: guild as i64, + deleted_by: deleted_by as i64, metadata_id: deleted.metadata_id, - meme_id: deleted.id, + meme_id: deleted.id, }; let _ = diesel::insert_into(tombstones::table).values(&tombstone).execute(tx).await?; Ok(()) } - .scope_boxed() + .scope_boxed() }) - .await + .await } pub async fn rare_meme( @@ -472,32 +473,32 @@ pub async fn rand_silent_meme(conn: &mut AsyncPgConnection, guild: u64) -> Resul #[derive(Debug, Clone)] pub struct Stats { - pub memes_overall: usize, - pub audio_memes: usize, - pub image_memes: usize, - pub started_recording: Option<DateTime<Utc>>, - pub total_meme_invocations: usize, - pub audio_meme_invocations: usize, + pub memes_overall: usize, + pub audio_memes: usize, + pub image_memes: usize, + pub started_recording: Option<DateTime<Utc>>, + pub total_meme_invocations: usize, + pub audio_meme_invocations: usize, pub random_meme_invocations: usize, - pub most_active_day: Option<NaiveDate>, + pub most_active_day: Option<NaiveDate>, pub most_active_day_count: usize, - pub most_audio_active_day: Option<NaiveDate>, + pub most_audio_active_day: Option<NaiveDate>, pub most_audio_active_count: usize, - pub most_random_meme_user: Option<u64>, - pub most_random_meme_user_count: usize, - pub most_directly_named_meme_user: Option<u64>, + pub most_random_meme_user: Option<u64>, + pub most_random_meme_user_count: usize, + pub most_directly_named_meme_user: Option<u64>, pub most_directly_named_meme_count: usize, - pub most_popular_named_meme: Option<String>, + pub most_popular_named_meme: Option<String>, pub most_popular_named_meme_count: usize, - pub most_popular_random_meme: Option<String>, + pub most_popular_random_meme: Option<String>, pub most_popular_random_meme_count: usize, - pub most_popular_meme_overall: Option<String>, + pub most_popular_meme_overall: Option<String>, pub most_popular_meme_overall_count: usize, } @@ -579,7 +580,7 @@ pub async fn stats(conn: &mut AsyncPgConnection, guild: u64) -> Result<Stats> { #[inline] fn option_first_count<T>(rows: Vec<tokio_postgres::Row>) -> (Option<T>, i64) where - for<'a> T: FromSql<'a>, + for<'a> T: FromSql<'a>, { let Some(row) = <[_]>::first(&rows) else { return (None, 0); @@ -730,10 +731,10 @@ pub async fn stats(conn: &mut AsyncPgConnection, guild: u64) -> Result<Stats> { #[derive(Clone, Debug, Hash, PartialEq, Eq, Default)] pub struct MemerInfo { - pub user_id: u64, - pub random_memes: usize, - pub specific_memes: usize, - pub most_used_meme: String, + pub user_id: u64, + pub random_memes: usize, + pub specific_memes: usize, + pub most_used_meme: String, pub most_used_meme_count: usize, } @@ -790,10 +791,10 @@ pub async fn memers(guild: u64) -> Result<Vec<MemerInfo>> { let most_memed_count: i64 = row.get(4); MemerInfo { - user_id: user_id as u64, - random_memes: random_count as usize, - specific_memes: specific_count as usize, - most_used_meme: most_memed_meme, + user_id: user_id as u64, + random_memes: random_count as usize, + specific_memes: specific_count as usize, + most_used_meme: most_memed_meme, most_used_meme_count: most_memed_count as usize, } }) |
