Skip to content

Commit

Permalink
admin/transfer_crates: Remove unnecessary database transaction
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Turbo87 committed Oct 4, 2024
1 parent 429e1be commit 30c52a2
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/admin/transfer_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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)?;

Check warning on line 26 in src/admin/transfer_crates.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/transfer_crates.rs#L26

Added line #L26 was not covered by tests
Ok(())
})
.await
Expand All @@ -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(());
}

Check warning on line 52 in src/admin/transfer_crates.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/transfer_crates.rs#L50-L52

Added lines #L50 - L52 were not covered by tests
}

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(());
}

Check warning on line 61 in src/admin/transfer_crates.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/transfer_crates.rs#L59-L61

Added lines #L59 - L61 were not covered by tests

let crate_owners = crate_owners::table
.filter(crate_owners::owner_id.eq(from.id))
Expand All @@ -71,17 +74,13 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> {
}
}

if !dialoguer::confirm("commit?") {
return Ok(());
}

Check warning on line 80 in src/admin/transfer_crates.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/transfer_crates.rs#L77-L80

Added lines #L77 - L80 were not covered by tests
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);
}
}

0 comments on commit 30c52a2

Please sign in to comment.