community

This commit is contained in:
dullbananas 2024-04-20 14:20:20 -07:00 committed by GitHub
parent fad321efc6
commit cea8cf36fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -281,8 +281,8 @@ impl CommunityLanguage {
} }
let form = lang_ids let form = lang_ids
.into_iter() .iter()
.map(|language_id| CommunityLanguageForm { .map(|&language_id| CommunityLanguageForm {
community_id: for_community_id, community_id: for_community_id,
language_id, language_id,
}) })
@ -292,25 +292,22 @@ impl CommunityLanguage {
.build_transaction() .build_transaction()
.run(|conn| { .run(|conn| {
Box::pin(async move { Box::pin(async move {
use crate::schema::community_language::dsl::{community_id, community_language}; use crate::schema::community_language::dsl::{community_id, community_language, language_id};
use diesel::result::DatabaseErrorKind::UniqueViolation; use diesel::result::DatabaseErrorKind::UniqueViolation;
// Clear the current languages // Delete old languages, not including new languages
delete(community_language.filter(community_id.eq(for_community_id))) let delete_old = delete(community_language)
.execute(conn) .filter(community_id.eq(for_community_id))
.await?; .filter(language_id.ne_all(lang_ids))
.execute(conn);
let insert_res = insert_into(community_language) // Insert new languages
let insert_new = insert_into(community_language)
.values(form) .values(form)
.get_result::<Self>(conn) .on_conflict((community_id, language_id))
.await; .do_nothing()
.execute(conn);
if let Err(Error::DatabaseError(UniqueViolation, _info)) = insert_res { tokio::try_join!(delete_old, insert_new)?;
// race condition: this function was probably called simultaneously from another caller. ignore error
// tracing::warn!("unique error: {_info:#?}");
// _info.constraint_name() should be = "community_language_community_id_language_id_key"
return Ok(());
}
insert_res?;
Ok(()) Ok(())
}) as _ }) as _