mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 15:04:14 +00:00
work around race condition on community fetch (#3414)
This commit is contained in:
parent
164f4b93d9
commit
2938b50908
1 changed files with 21 additions and 9 deletions
|
@ -275,25 +275,37 @@ impl CommunityLanguage {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let form = lang_ids
|
||||||
|
.into_iter()
|
||||||
|
.map(|language_id| CommunityLanguageForm {
|
||||||
|
community_id: for_community_id,
|
||||||
|
language_id,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
conn
|
conn
|
||||||
.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};
|
||||||
|
use diesel::result::DatabaseErrorKind::UniqueViolation;
|
||||||
// Clear the current languages
|
// Clear the current languages
|
||||||
delete(community_language.filter(community_id.eq(for_community_id)))
|
delete(community_language.filter(community_id.eq(for_community_id)))
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
for l in lang_ids {
|
let insert_res = insert_into(community_language)
|
||||||
let form = CommunityLanguageForm {
|
|
||||||
community_id: for_community_id,
|
|
||||||
language_id: l,
|
|
||||||
};
|
|
||||||
insert_into(community_language)
|
|
||||||
.values(form)
|
.values(form)
|
||||||
.get_result::<Self>(conn)
|
.get_result::<Self>(conn)
|
||||||
.await?;
|
.await;
|
||||||
|
|
||||||
|
if let Err(Error::DatabaseError(UniqueViolation, _info)) = insert_res {
|
||||||
|
// 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(());
|
||||||
|
} else {
|
||||||
|
insert_res?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}) as _
|
}) as _
|
||||||
|
|
Loading…
Reference in a new issue