From 16d226ffd0c0d6bd6ceb5fdd3d42a0465acc7ed0 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 13 Oct 2023 11:29:35 +0800 Subject: [PATCH] chore: update insert user sql --- libs/database/src/user.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/libs/database/src/user.rs b/libs/database/src/user.rs index 6a03484f..137ea395 100644 --- a/libs/database/src/user.rs +++ b/libs/database/src/user.rs @@ -32,13 +32,8 @@ pub async fn create_user_if_not_exists( let affected_rows = sqlx::query!( r#" INSERT INTO af_user (uuid, email, name) - SELECT $1, $2, $3 - WHERE NOT EXISTS ( - SELECT 1 FROM public.af_user WHERE email = $2 - ) - AND NOT EXISTS ( - SELECT 1 FROM public.af_user WHERE uuid = $1 - ) + VALUES ($1, $2, $3) + ON CONFLICT (email) DO NOTHING; "#, user_uuid, email, @@ -47,7 +42,7 @@ pub async fn create_user_if_not_exists( .execute(pool) .await .context(format!( - "Fail to insert user to db. uuid: {}, name: {}, email: {}", + "Fail to insert user with uuid: {}, name: {}, email: {}", user_uuid, name, email ))? .rows_affected();