Skip to content

Commit 8ec7a12

Browse files
authored
Merge pull request #9620 from Turbo87/async-transfer
admin/transfer_crates: Replace `spawn_blocking()` with `diesel_async` usage
2 parents 9d0ff70 + 6f600a6 commit 8ec7a12

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

src/admin/transfer_crates.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::{
55
schema::{crate_owners, crates, users},
66
};
77

8-
use crate::tasks::spawn_blocking;
98
use diesel::prelude::*;
9+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
1010

1111
#[derive(clap::Parser, Debug)]
1212
#[command(
@@ -21,22 +21,21 @@ pub struct Opts {
2121
}
2222

2323
pub async fn run(opts: Opts) -> anyhow::Result<()> {
24-
spawn_blocking(move || {
25-
let conn = &mut db::oneoff_connection()?;
26-
transfer(opts, conn)?;
27-
Ok(())
28-
})
29-
.await
24+
let mut conn = db::oneoff_async_connection().await?;
25+
transfer(opts, &mut conn).await?;
26+
Ok(())
3027
}
3128

32-
fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> {
29+
async fn transfer(opts: Opts, conn: &mut AsyncPgConnection) -> anyhow::Result<()> {
3330
let from: User = users::table
3431
.filter(users::gh_login.eq(opts.from_user))
35-
.first(conn)?;
32+
.first(conn)
33+
.await?;
3634

3735
let to: User = users::table
3836
.filter(users::gh_login.eq(opts.to_user))
39-
.first(conn)?;
37+
.first(conn)
38+
.await?;
4039

4140
if from.gh_id != to.gh_id {
4241
println!("====================================================");
@@ -47,7 +46,7 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> {
4746
println!("from: {:?}", from.gh_id);
4847
println!("to: {:?}", to.gh_id);
4948

50-
if !dialoguer::confirm("continue?")? {
49+
if !dialoguer::async_confirm("continue?").await? {
5150
return Ok(());
5251
}
5352
}
@@ -56,7 +55,7 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> {
5655
"Are you sure you want to transfer crates from {} to {}?",
5756
from.gh_login, to.gh_login
5857
);
59-
if !dialoguer::confirm(&prompt)? {
58+
if !dialoguer::async_confirm(&prompt).await? {
6059
return Ok(());
6160
}
6261

@@ -65,22 +64,30 @@ fn transfer(opts: Opts, conn: &mut PgConnection) -> anyhow::Result<()> {
6564
.filter(crate_owners::owner_kind.eq(OwnerKind::User));
6665
let crates: Vec<Crate> = Crate::all()
6766
.filter(crates::id.eq_any(crate_owners.select(crate_owners::crate_id)))
68-
.load(conn)?;
67+
.load(conn)
68+
.await?;
6969

7070
for krate in crates {
71-
let owners = krate.owners(conn)?;
72-
if owners.len() != 1 {
71+
let num_owners: i64 = crate_owners::table
72+
.count()
73+
.filter(crate_owners::deleted.eq(false))
74+
.filter(crate_owners::crate_id.eq(krate.id))
75+
.get_result(conn)
76+
.await?;
77+
78+
if num_owners != 1 {
7379
println!("warning: not exactly one owner for {}", krate.name);
7480
}
7581
}
7682

77-
if !dialoguer::confirm("commit?")? {
83+
if !dialoguer::async_confirm("commit?").await? {
7884
return Ok(());
7985
}
8086

8187
diesel::update(crate_owners)
8288
.set(crate_owners::owner_id.eq(to.id))
83-
.execute(conn)?;
89+
.execute(conn)
90+
.await?;
8491

8592
Ok(())
8693
}

0 commit comments

Comments
 (0)