]> Untitled Git - lemmy.git/commitdiff
add on_conflict clauses to common unique constraint failures (#1264)
authoreiknat <68170752+eiknat@users.noreply.github.com>
Wed, 16 Dec 2020 14:42:57 +0000 (09:42 -0500)
committerGitHub <noreply@github.com>
Wed, 16 Dec 2020 14:42:57 +0000 (09:42 -0500)
* add on_conflict clauses to common unique constraint failures

* user mention: change create conflict to do_update

lemmy_db/src/comment.rs
lemmy_db/src/community.rs
lemmy_db/src/post.rs
lemmy_db/src/user_mention.rs
lemmy_structs/src/lib.rs

index f54ddd10f203def7fc3820ec4887a1196a379edb..3e7d06be289afb3e7af56b43d377caffec50dfbf 100644 (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> {
index be40da349246692b0e56714294e2fb9fa7e6e361..35d54c3f182b9edfe967b59c28840d2a16625b4b 100644 (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>
index 530f475b4c35b68299db7162b5a7052cb11402d0..b42c23c72145aa47dd9eff1615198198e2b30488 100644 (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> {
index 68f566332dd8969f30c7fb141dc24eb10644bbed..b382a24e029f21f980a7ec546d0b20e5fbe52b1c 100644 (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)
   }
 
index 5d2e42733e193985105ba8bf65fc89b5c98cddc8..a0dbdab651e2a675fda724ea8aa9dd0174313ed8 100644 (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 {