add on_conflict clauses to common unique constraint failures (#1264)

* add on_conflict clauses to common unique constraint failures

* user mention: change create conflict to do_update
This commit is contained in:
eiknat 2020-12-16 09:42:57 -05:00 committed by GitHub
parent c947539301
commit 036161cb38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 4 deletions

View file

@ -209,6 +209,9 @@ impl Likeable<CommentLikeForm> for CommentLike {
use crate::schema::comment_like::dsl::*;
insert_into(comment_like)
.values(comment_like_form)
.on_conflict((comment_id, user_id))
.do_update()
.set(comment_like_form)
.get_result::<Self>(conn)
}
fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
@ -244,6 +247,9 @@ impl Saveable<CommentSavedForm> for CommentSaved {
use crate::schema::comment_saved::dsl::*;
insert_into(comment_saved)
.values(comment_saved_form)
.on_conflict((comment_id, user_id))
.do_update()
.set(comment_saved_form)
.get_result::<Self>(conn)
}
fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {

View file

@ -309,6 +309,9 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
use crate::schema::community_follower::dsl::*;
insert_into(community_follower)
.values(community_follower_form)
.on_conflict((community_id, user_id))
.do_update()
.set(community_follower_form)
.get_result::<Self>(conn)
}
fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>

View file

@ -240,6 +240,9 @@ impl Likeable<PostLikeForm> for PostLike {
use crate::schema::post_like::dsl::*;
insert_into(post_like)
.values(post_like_form)
.on_conflict((post_id, user_id))
.do_update()
.set(post_like_form)
.get_result::<Self>(conn)
}
fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
@ -275,6 +278,9 @@ impl Saveable<PostSavedForm> for PostSaved {
use crate::schema::post_saved::dsl::*;
insert_into(post_saved)
.values(post_saved_form)
.on_conflict((post_id, user_id))
.do_update()
.set(post_saved_form)
.get_result::<Self>(conn)
}
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {

View file

@ -29,8 +29,13 @@ impl Crud<UserMentionForm> for UserMention {
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*;
// since the return here isnt utilized, we dont need to do an update
// but get_result doesnt return the existing row here
insert_into(user_mention)
.values(user_mention_form)
.on_conflict((recipient_id, comment_id))
.do_update()
.set(user_mention_form)
.get_result::<Self>(conn)
}

View file

@ -98,10 +98,7 @@ fn do_send_local_notifs(
// Allow this to fail softly, since comment edits might re-update or replace it
// Let the uniqueness handle this fail
match UserMention::create(&conn, &user_mention_form) {
Ok(_mention) => (),
Err(_e) => error!("{}", &_e),
};
let _ = UserMention::create(&conn, &user_mention_form);
// Send an email to those users that have notifications on
if do_send_email && mention_user.send_notifications_to_email {