From cbe41b17ba371f6092a609020d42337672d0cbb7 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 27 Mar 2023 22:14:47 +0200 Subject: [PATCH] fix tests --- crates/db_schema/src/impls/actor_language.rs | 48 ++++++++++++++------ crates/db_schema/src/impls/community.rs | 26 ++++------- crates/db_schema/src/impls/site.rs | 15 +++--- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/crates/db_schema/src/impls/actor_language.rs b/crates/db_schema/src/impls/actor_language.rs index 01ffdce629..4d4fb31027 100644 --- a/crates/db_schema/src/impls/actor_language.rs +++ b/crates/db_schema/src/impls/actor_language.rs @@ -25,7 +25,11 @@ use diesel::{ ExpressionMethods, QueryDsl, }; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::{ + pooled_connection::deadpool::Object as PooledConnection, + AsyncPgConnection, + RunQueryDsl, +}; use lemmy_utils::error::LemmyError; use tokio::sync::OnceCell; @@ -115,15 +119,22 @@ impl SiteLanguage { .await } - pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - - let langs = site_language::table + async fn read_internal( + conn: &mut PooledConnection, + for_site_id: SiteId, + ) -> Result, Error> { + site_language::table .filter(site_language::site_id.eq(for_site_id)) .order(site_language::language_id) .select(site_language::language_id) .load(conn) - .await?; + .await + } + + pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let langs = Self::read_internal(conn, for_site_id).await?; + convert_read_languages(conn, langs).await } @@ -233,19 +244,25 @@ impl CommunityLanguage { Ok(()) } - pub async fn read( - pool: &DbPool, + async fn read_internal( + conn: &mut PooledConnection, for_community_id: CommunityId, ) -> Result, Error> { use crate::schema::community_language::dsl::{community_id, community_language, language_id}; - let conn = &mut get_conn(pool).await?; - - let langs = community_language + community_language .filter(community_id.eq(for_community_id)) .order(language_id) .select(language_id) .get_results(conn) - .await?; + .await + } + + pub async fn read( + pool: &DbPool, + for_community_id: CommunityId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let langs = Self::read_internal(conn, for_community_id).await?; convert_read_languages(conn, langs).await } @@ -258,10 +275,11 @@ impl CommunityLanguage { if language_ids.is_empty() { language_ids = SiteLanguage::read_local(pool).await?; } + let lang_ids = convert_update_languages(conn, language_ids).await?; // No need to update if languages are unchanged - let current = CommunityLanguage::read(pool, for_community_id).await?; - if current == language_ids { + let current = CommunityLanguage::read_internal(conn, for_community_id).await?; + if current == lang_ids { return Ok(()); } @@ -275,7 +293,7 @@ impl CommunityLanguage { .execute(conn) .await?; - for l in language_ids { + for l in lang_ids { let form = CommunityLanguageForm { community_id: for_community_id, language_id: l, diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 1c1f116866..fe41d4d587 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -2,7 +2,7 @@ use crate::{ newtypes::{CommunityId, DbUrl, PersonId}, schema::community::dsl::{actor_id, community, deleted, local, name, removed}, source::{ - actor_language::{CommunityLanguage, SiteLanguage}, + actor_language::CommunityLanguage, community::{ Community, CommunityFollower, @@ -21,7 +21,6 @@ use crate::{ }; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel_async::RunQueryDsl; -use futures::future::OptionFuture; #[async_trait] impl Crud for Community { @@ -42,13 +41,11 @@ impl Crud for Community { async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; - let existing = OptionFuture::from( - form - .actor_id - .as_ref() - .map(|id| Community::read_from_apub_id(pool, id)), - ) - .await; + let is_new_community = match &form.actor_id { + Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(), + None => true, + }; + // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible let community_ = insert_into(community) .values(form) @@ -59,15 +56,8 @@ impl Crud for Community { .await?; // Initialize languages for new community - if existing.is_none() { - let site_languages = SiteLanguage::read_local(pool).await; - if let Ok(langs) = site_languages { - // if site exists, init community with site languages - CommunityLanguage::update(pool, langs, community_.id).await?; - } else { - // otherwise, init with all languages (this only happens during tests) - CommunityLanguage::update(pool, vec![], community_.id).await?; - } + if is_new_community { + CommunityLanguage::update(pool, vec![], community_.id).await?; } Ok(community_) diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index 1c182d19a9..8355887f78 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -10,7 +10,6 @@ use crate::{ }; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; -use futures::future::OptionFuture; use url::Url; #[async_trait] @@ -26,13 +25,11 @@ impl Crud for Site { async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; - let existing = OptionFuture::from( - form - .actor_id - .as_ref() - .map(|id_| Site::read_from_apub_id(pool, id_)), - ) - .await; + let is_new_site = match &form.actor_id { + Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(), + None => true, + }; + // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible let site_ = insert_into(site) .values(form) @@ -43,7 +40,7 @@ impl Crud for Site { .await?; // initialize languages if site is newly created - if existing.is_none() { + if !is_new_site { // initialize with all languages SiteLanguage::update(pool, vec![], &site_).await?; }