@@ -5,8 +5,8 @@ use crate::{admin::dialoguer, db, schema::crates};
55use anyhow:: Context ;
66use crates_io_worker:: BackgroundJob ;
77use diesel:: dsl:: sql;
8- use diesel:: prelude:: * ;
98use diesel:: sql_types:: Text ;
9+ use diesel:: { ExpressionMethods , JoinOnDsl , QueryDsl } ;
1010use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
1111use std:: collections:: HashMap ;
1212
@@ -27,39 +27,45 @@ pub struct Opts {
2727}
2828
2929pub async fn run ( opts : Opts ) -> anyhow:: Result < ( ) > {
30- let conn = db:: oneoff_async_connection ( )
30+ let mut conn = db:: oneoff_async_connection ( )
3131 . await
3232 . context ( "Failed to establish database connection" ) ?;
3333
34+ let mut crate_names = opts. crate_names ;
35+ crate_names. sort ( ) ;
36+
37+ let query_result = {
38+ use diesel_async:: RunQueryDsl ;
39+
40+ crates:: table
41+ . select ( (
42+ crates:: name,
43+ crates:: id,
44+ sql :: < Text > (
45+ "CASE WHEN crate_owners.owner_kind = 1 THEN teams.login ELSE users.gh_login END" ,
46+ ) ,
47+ ) )
48+ . left_join ( crate_owners:: table. on ( crate_owners:: crate_id. eq ( crates:: id) ) )
49+ . left_join ( teams:: table. on ( teams:: id. eq ( crate_owners:: owner_id) ) )
50+ . left_join ( users:: table. on ( users:: id. eq ( crate_owners:: owner_id) ) )
51+ . filter ( crates:: name. eq_any ( & crate_names) )
52+ . load :: < ( String , i32 , String ) > ( & mut conn) . await
53+ . context ( "Failed to look up crate name from the database" )
54+ } ?;
55+
56+ let mut existing_crates: HashMap < String , ( i32 , Vec < String > ) > = HashMap :: new ( ) ;
57+ for ( name, id, login) in query_result {
58+ let entry = existing_crates
59+ . entry ( name)
60+ . or_insert_with ( || ( id, Vec :: new ( ) ) ) ;
61+
62+ entry. 1 . push ( login) ;
63+ }
64+
3465 spawn_blocking ( move || {
35- let conn : & mut AsyncConnectionWrapper < _ > = & mut conn . into ( ) ;
66+ use diesel :: RunQueryDsl ;
3667
37- let mut crate_names = opts. crate_names ;
38- crate_names. sort ( ) ;
39-
40- let query_result = crates:: table
41- . select ( (
42- crates:: name,
43- crates:: id,
44- sql :: < Text > (
45- "CASE WHEN crate_owners.owner_kind = 1 THEN teams.login ELSE users.gh_login END" ,
46- ) ,
47- ) )
48- . left_join ( crate_owners:: table. on ( crate_owners:: crate_id. eq ( crates:: id) ) )
49- . left_join ( teams:: table. on ( teams:: id. eq ( crate_owners:: owner_id) ) )
50- . left_join ( users:: table. on ( users:: id. eq ( crate_owners:: owner_id) ) )
51- . filter ( crates:: name. eq_any ( & crate_names) )
52- . load :: < ( String , i32 , String ) > ( conn)
53- . context ( "Failed to look up crate name from the database" ) ?;
54-
55- let mut existing_crates: HashMap < String , ( i32 , Vec < String > ) > = HashMap :: new ( ) ;
56- for ( name, id, login) in query_result {
57- let entry = existing_crates
58- . entry ( name)
59- . or_insert_with ( || ( id, Vec :: new ( ) ) ) ;
60-
61- entry. 1 . push ( login) ;
62- }
68+ let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
6369
6470 println ! ( "Deleting the following crates:" ) ;
6571 println ! ( ) ;
0 commit comments