From 036161cb38314ec3c3ac9c94de34e8d5bcc043ad Mon Sep 17 00:00:00 2001 From: eiknat <68170752+eiknat@users.noreply.github.com> Date: Wed, 16 Dec 2020 09:42:57 -0500 Subject: [PATCH] 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 --- lemmy_db/src/comment.rs | 6 ++++++ lemmy_db/src/community.rs | 3 +++ lemmy_db/src/post.rs | 6 ++++++ lemmy_db/src/user_mention.rs | 5 +++++ lemmy_structs/src/lib.rs | 5 +---- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lemmy_db/src/comment.rs b/lemmy_db/src/comment.rs index f54ddd10f..3e7d06be2 100644 --- a/lemmy_db/src/comment.rs +++ b/lemmy_db/src/comment.rs @@ -209,6 +209,9 @@ impl Likeable 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::(conn) } fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result { @@ -244,6 +247,9 @@ impl Saveable 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::(conn) } fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result { diff --git a/lemmy_db/src/community.rs b/lemmy_db/src/community.rs index be40da349..35d54c3f1 100644 --- a/lemmy_db/src/community.rs +++ b/lemmy_db/src/community.rs @@ -309,6 +309,9 @@ impl Followable 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::(conn) } fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result diff --git a/lemmy_db/src/post.rs b/lemmy_db/src/post.rs index 530f475b4..b42c23c72 100644 --- a/lemmy_db/src/post.rs +++ b/lemmy_db/src/post.rs @@ -240,6 +240,9 @@ impl Likeable 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::(conn) } fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result { @@ -275,6 +278,9 @@ impl Saveable 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::(conn) } fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result { diff --git a/lemmy_db/src/user_mention.rs b/lemmy_db/src/user_mention.rs index 68f566332..b382a24e0 100644 --- a/lemmy_db/src/user_mention.rs +++ b/lemmy_db/src/user_mention.rs @@ -29,8 +29,13 @@ impl Crud for UserMention { fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result { 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::(conn) } diff --git a/lemmy_structs/src/lib.rs b/lemmy_structs/src/lib.rs index 5d2e42733..a0dbdab65 100644 --- a/lemmy_structs/src/lib.rs +++ b/lemmy_structs/src/lib.rs @@ -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 {