]> Untitled Git - lemmy.git/commitdiff
Revert "Removing on_conflict as it may not work with table triggers (user_fast, etc)"
authorDessalines <tyhou13@gmx.com>
Thu, 8 Oct 2020 00:08:06 +0000 (19:08 -0500)
committerDessalines <tyhou13@gmx.com>
Thu, 8 Oct 2020 00:08:06 +0000 (19:08 -0500)
This reverts commit db7027a3674cb1c9f07822d3e56743af4133f523.

lemmy_db/src/comment.rs
lemmy_db/src/community.rs
lemmy_db/src/post.rs
lemmy_db/src/private_message.rs
lemmy_db/src/user.rs

index ff0c61203265cfa24ec8cb57013bb7a3df94ca53..398ea78bf40c624e60af1939d2ce526d31a1240a 100644 (file)
@@ -170,15 +170,13 @@ impl Comment {
   }
 
   pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
-    let existing = Self::read_from_apub_id(
-      conn,
-      comment_form.ap_id.as_ref().unwrap_or(&"none".to_string()),
-    );
-    match existing {
-      Err(NotFound {}) => Ok(Self::create(conn, &comment_form)?),
-      Ok(p) => Ok(Self::update(conn, p.id, &comment_form)?),
-      Err(e) => Err(e),
-    }
+    use crate::schema::comment::dsl::*;
+    insert_into(comment)
+      .values(comment_form)
+      .on_conflict(ap_id)
+      .do_update()
+      .set(comment_form)
+      .get_result::<Self>(conn)
   }
 }
 
index ece96b0c73da8476e4ee62976eb0a6c43580df7d..24cf7e32fd0129fdf6cabd1224cbd5dc2b8923c9 100644 (file)
@@ -166,18 +166,13 @@ impl Community {
   }
 
   pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
-    let existing = Self::read_from_actor_id(
-      conn,
-      community_form
-        .actor_id
-        .as_ref()
-        .unwrap_or(&"none".to_string()),
-    );
-    match existing {
-      Err(NotFound {}) => Ok(Self::create(conn, &community_form)?),
-      Ok(p) => Ok(Self::update(conn, p.id, &community_form)?),
-      Err(e) => Err(e),
-    }
+    use crate::schema::community::dsl::*;
+    insert_into(community)
+      .values(community_form)
+      .on_conflict(actor_id)
+      .do_update()
+      .set(community_form)
+      .get_result::<Self>(conn)
   }
 }
 
index 1ca38b3108ede32f8658c39052242a50cb6848ef..724c342c67c4aeefa686e4665090697e9bf2ecd6 100644 (file)
@@ -179,15 +179,13 @@ impl Post {
   }
 
   pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
-    let existing = Self::read_from_apub_id(
-      conn,
-      post_form.ap_id.as_ref().unwrap_or(&"none".to_string()),
-    );
-    match existing {
-      Err(NotFound {}) => Ok(Self::create(conn, &post_form)?),
-      Ok(p) => Ok(Self::update(conn, p.id, &post_form)?),
-      Err(e) => Err(e),
-    }
+    use crate::schema::post::dsl::*;
+    insert_into(post)
+      .values(post_form)
+      .on_conflict(ap_id)
+      .do_update()
+      .set(post_form)
+      .get_result::<Self>(conn)
   }
 }
 
index 36aa7aa8a81b5cdbef48ba5c8fe16a4f3bc862c5..988d97d3b69b8b139b91e6ab4b751db05082219d 100644 (file)
@@ -124,18 +124,13 @@ impl PrivateMessage {
     conn: &PgConnection,
     private_message_form: &PrivateMessageForm,
   ) -> Result<Self, Error> {
-    let existing = Self::read_from_apub_id(
-      conn,
-      private_message_form
-        .ap_id
-        .as_ref()
-        .unwrap_or(&"none".to_string()),
-    );
-    match existing {
-      Err(NotFound {}) => Ok(Self::create(conn, &private_message_form)?),
-      Ok(p) => Ok(Self::update(conn, p.id, &private_message_form)?),
-      Err(e) => Err(e),
-    }
+    use crate::schema::private_message::dsl::*;
+    insert_into(private_message)
+      .values(private_message_form)
+      .on_conflict(ap_id)
+      .do_update()
+      .set(private_message_form)
+      .get_result::<Self>(conn)
   }
 }
 
index 88bccf6a58c5a6cbaa7b40ed54800602e855e411..83f0559abe2906e3c8c300df89d4c1e367703199 100644 (file)
@@ -161,15 +161,12 @@ impl User_ {
   }
 
   pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
-    let existing = Self::read_from_actor_id(
-      conn,
-      user_form.actor_id.as_ref().unwrap_or(&"none".to_string()),
-    );
-    match existing {
-      Err(NotFound {}) => Ok(Self::create(conn, &user_form)?),
-      Ok(p) => Ok(Self::update(conn, p.id, &user_form)?),
-      Err(e) => Err(e),
-    }
+    insert_into(user_)
+      .values(user_form)
+      .on_conflict(actor_id)
+      .do_update()
+      .set(user_form)
+      .get_result::<Self>(conn)
   }
 }