From b4c730e537bc9b44478f4396e34338607be6d6b5 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 7 Oct 2020 19:08:06 -0500 Subject: [PATCH] Revert "Removing on_conflict as it may not work with table triggers (user_fast, etc)" This reverts commit db7027a3674cb1c9f07822d3e56743af4133f523. --- lemmy_db/src/comment.rs | 16 +++++++--------- lemmy_db/src/community.rs | 19 +++++++------------ lemmy_db/src/post.rs | 16 +++++++--------- lemmy_db/src/private_message.rs | 19 +++++++------------ lemmy_db/src/user.rs | 15 ++++++--------- 5 files changed, 34 insertions(+), 51 deletions(-) diff --git a/lemmy_db/src/comment.rs b/lemmy_db/src/comment.rs index ff0c61203..398ea78bf 100644 --- a/lemmy_db/src/comment.rs +++ b/lemmy_db/src/comment.rs @@ -170,15 +170,13 @@ impl Comment { } pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result { - 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::(conn) } } diff --git a/lemmy_db/src/community.rs b/lemmy_db/src/community.rs index ece96b0c7..24cf7e32f 100644 --- a/lemmy_db/src/community.rs +++ b/lemmy_db/src/community.rs @@ -166,18 +166,13 @@ impl Community { } pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result { - 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::(conn) } } diff --git a/lemmy_db/src/post.rs b/lemmy_db/src/post.rs index 1ca38b310..724c342c6 100644 --- a/lemmy_db/src/post.rs +++ b/lemmy_db/src/post.rs @@ -179,15 +179,13 @@ impl Post { } pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { - 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::(conn) } } diff --git a/lemmy_db/src/private_message.rs b/lemmy_db/src/private_message.rs index 36aa7aa8a..988d97d3b 100644 --- a/lemmy_db/src/private_message.rs +++ b/lemmy_db/src/private_message.rs @@ -124,18 +124,13 @@ impl PrivateMessage { conn: &PgConnection, private_message_form: &PrivateMessageForm, ) -> Result { - 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::(conn) } } diff --git a/lemmy_db/src/user.rs b/lemmy_db/src/user.rs index 88bccf6a5..83f0559ab 100644 --- a/lemmy_db/src/user.rs +++ b/lemmy_db/src/user.rs @@ -161,15 +161,12 @@ impl User_ { } pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result { - 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::(conn) } }