44
55use crate :: models:: { Crate , CrateVersions , Dependency , Version } ;
66use crate :: schema:: { crates, dependencies} ;
7- use crate :: util:: diesel:: Conn ;
87use anyhow:: Context ;
98use crates_io_index:: features:: split_features;
109use diesel:: prelude:: * ;
10+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
1111use sentry:: Level ;
1212
1313#[ instrument( skip_all, fields( krate. name = ?name) ) ]
14- pub fn get_index_data ( name : & str , conn : & mut impl Conn ) -> anyhow:: Result < Option < String > > {
14+ pub async fn get_index_data (
15+ name : & str ,
16+ conn : & mut AsyncPgConnection ,
17+ ) -> anyhow:: Result < Option < String > > {
1518 debug ! ( "Looking up crate by name" ) ;
1619 let krate = crates:: table
1720 . select ( Crate :: as_select ( ) )
1821 . filter ( crates:: name. eq ( name) )
1922 . first :: < Crate > ( conn)
23+ . await
2024 . optional ( ) ;
2125
2226 let Some ( krate) = krate? else {
2327 return Ok ( None ) ;
2428 } ;
2529
2630 debug ! ( "Gathering remaining index data" ) ;
27- let crates = index_metadata ( & krate, conn) . context ( "Failed to gather index metadata" ) ?;
31+ let crates = index_metadata ( & krate, conn)
32+ . await
33+ . context ( "Failed to gather index metadata" ) ?;
2834
2935 // This can sometimes happen when we delete versions upon owner request
3036 // but don't realize that the crate is now left with no versions at all.
@@ -49,11 +55,11 @@ pub fn get_index_data(name: &str, conn: &mut impl Conn) -> anyhow::Result<Option
4955}
5056
5157/// Gather all the necessary data to write an index metadata file
52- pub fn index_metadata (
58+ pub async fn index_metadata (
5359 krate : & Crate ,
54- conn : & mut impl Conn ,
60+ conn : & mut AsyncPgConnection ,
5561) -> QueryResult < Vec < crates_io_index:: Crate > > {
56- let mut versions: Vec < Version > = krate. all_versions ( ) . load ( conn) ?;
62+ let mut versions: Vec < Version > = krate. all_versions ( ) . load ( conn) . await ?;
5763
5864 // We sort by `created_at` by default, but since tests run within a
5965 // single database transaction the versions will all have the same
@@ -63,7 +69,8 @@ pub fn index_metadata(
6369 let deps: Vec < ( Dependency , String ) > = Dependency :: belonging_to ( & versions)
6470 . inner_join ( crates:: table)
6571 . select ( ( dependencies:: all_columns, crates:: name) )
66- . load ( conn) ?;
72+ . load ( conn)
73+ . await ?;
6774
6875 let deps = deps. grouped_by ( & versions) ;
6976
@@ -133,12 +140,14 @@ mod tests {
133140 use crate :: tests:: builders:: { CrateBuilder , VersionBuilder } ;
134141 use chrono:: { Days , Utc } ;
135142 use crates_io_test_db:: TestDatabase ;
143+ use diesel_async:: AsyncConnection ;
136144 use insta:: assert_json_snapshot;
137145
138- #[ test]
139- fn test_index_metadata ( ) {
146+ #[ tokio :: test]
147+ async fn test_index_metadata ( ) {
140148 let test_db = TestDatabase :: new ( ) ;
141149 let mut conn = test_db. connect ( ) ;
150+ let mut async_conn = AsyncPgConnection :: establish ( test_db. url ( ) ) . await . unwrap ( ) ;
142151
143152 let user_id = diesel:: insert_into ( users:: table)
144153 . values ( (
@@ -148,7 +157,8 @@ mod tests {
148157 users:: gh_access_token. eq ( "some random token" ) ,
149158 ) )
150159 . returning ( users:: id)
151- . get_result :: < i32 > ( & mut conn)
160+ . get_result :: < i32 > ( & mut async_conn)
161+ . await
152162 . unwrap ( ) ;
153163
154164 let created_at_1 = Utc :: now ( )
@@ -165,7 +175,7 @@ mod tests {
165175 . version ( VersionBuilder :: new ( "0.1.0" ) )
166176 . expect_build ( & mut conn) ;
167177
168- let metadata = index_metadata ( & fooo, & mut conn ) . unwrap ( ) ;
178+ let metadata = index_metadata ( & fooo, & mut async_conn ) . await . unwrap ( ) ;
169179 assert_json_snapshot ! ( metadata) ;
170180
171181 let bar = CrateBuilder :: new ( "bar" , user_id)
@@ -183,7 +193,7 @@ mod tests {
183193 . version ( VersionBuilder :: new ( "1.0.1" ) . checksum ( "0123456789abcdef" ) )
184194 . expect_build ( & mut conn) ;
185195
186- let metadata = index_metadata ( & bar, & mut conn ) . unwrap ( ) ;
196+ let metadata = index_metadata ( & bar, & mut async_conn ) . await . unwrap ( ) ;
187197 assert_json_snapshot ! ( metadata) ;
188198 }
189199}
0 commit comments