From 30c52a2a0c4676895a188dbbdb9b05f7ed812902 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 4 Oct 2024 15:49:32 +0200 Subject: [PATCH] admin/transfer_crates: Remove unnecessary database transaction There is only a single write query in this code path, so the transaction is somewhat unnecessary. This also simplifies the confirmation code a little bit since it doesn't have to rely on `exit(0)` anymore. --- src/admin/transfer_crates.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/admin/transfer_crates.rs b/src/admin/transfer_crates.rs index 3f6ff12d32b..2dec50d5bb8 100644 --- a/src/admin/transfer_crates.rs +++ b/src/admin/transfer_crates.rs @@ -4,7 +4,6 @@ use crate::{ models::{Crate, OwnerKind, User}, schema::{crate_owners, crates, users}, }; -use std::process::exit; use crate::tasks::spawn_blocking; use diesel::prelude::*; @@ -24,7 +23,7 @@ pub struct Opts { pub async fn run(opts: Opts) -> anyhow::Result<()> { spawn_blocking(move || { let conn = &mut db::oneoff_connection()?; - conn.transaction(|conn| transfer(opts, conn))?; + transfer(opts, conn)?; Ok(()) }) .await @@ -48,14 +47,18 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> { println!("from: {:?}", from.gh_id); println!("to: {:?}", to.gh_id); - get_confirm("continue?"); + if !dialoguer::confirm("continue?") { + return Ok(()); + } } let prompt = format!( "Are you sure you want to transfer crates from {} to {}?", from.gh_login, to.gh_login ); - get_confirm(&prompt); + if !dialoguer::confirm(&prompt) { + return Ok(()); + } let crate_owners = crate_owners::table .filter(crate_owners::owner_id.eq(from.id)) @@ -71,17 +74,13 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> { } } + if !dialoguer::confirm("commit?") { + return Ok(()); + } + diesel::update(crate_owners) .set(crate_owners::owner_id.eq(to.id)) .execute(conn)?; - get_confirm("commit?"); - Ok(()) } - -fn get_confirm(msg: &str) { - if !dialoguer::confirm(msg) { - exit(0); - } -}