Skip to content

Commit 6f600a6

Browse files
committed
admin/transfer_crates: Replace spawn_blocking() with diesel_async usage
1 parent d3b3ae1 commit 6f600a6

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/admin/transfer_crates.rs

Lines changed: 18 additions & 16 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,27 +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 {
7171
let num_owners: i64 = crate_owners::table
7272
.count()
7373
.filter(crate_owners::deleted.eq(false))
7474
.filter(crate_owners::crate_id.eq(krate.id))
75-
.get_result(conn)?;
75+
.get_result(conn)
76+
.await?;
7677

7778
if num_owners != 1 {
7879
println!("warning: not exactly one owner for {}", krate.name);
7980
}
8081
}
8182

82-
if !dialoguer::confirm("commit?")? {
83+
if !dialoguer::async_confirm("commit?").await? {
8384
return Ok(());
8485
}
8586

8687
diesel::update(crate_owners)
8788
.set(crate_owners::owner_id.eq(to.id))
88-
.execute(conn)?;
89+
.execute(conn)
90+
.await?;
8991

9092
Ok(())
9193
}

0 commit comments

Comments
 (0)