@@ -2,6 +2,7 @@ use crate::schema::{default_versions, versions};
22use crate :: sql:: SemverVersion ;
33use crate :: util:: diesel:: prelude:: * ;
44use crate :: util:: diesel:: Conn ;
5+ use diesel_async:: AsyncPgConnection ;
56
67/// A subset of the columns of the `versions` table.
78///
@@ -45,6 +46,44 @@ impl Ord for Version {
4546 }
4647}
4748
49+ /// Updates the `default_versions` table entry for the specified crate.
50+ ///
51+ /// This function first loads all versions of the crate from the database,
52+ /// then determines the default version based on the following criteria:
53+ ///
54+ /// 1. The highest non-prerelease version that is not yanked.
55+ /// 2. The highest non-yanked version.
56+ /// 3. The highest version.
57+ ///
58+ /// The default version is then written to the `default_versions` table.
59+ #[ instrument( skip( conn) ) ]
60+ pub async fn async_update_default_version (
61+ crate_id : i32 ,
62+ conn : & mut AsyncPgConnection ,
63+ ) -> QueryResult < ( ) > {
64+ use diesel_async:: RunQueryDsl ;
65+
66+ let default_version = async_calculate_default_version ( crate_id, conn) . await ?;
67+
68+ debug ! (
69+ "Updating default version to {} (id: {})…" ,
70+ default_version. num, default_version. id
71+ ) ;
72+
73+ diesel:: insert_into ( default_versions:: table)
74+ . values ( (
75+ default_versions:: crate_id. eq ( crate_id) ,
76+ default_versions:: version_id. eq ( default_version. id ) ,
77+ ) )
78+ . on_conflict ( default_versions:: crate_id)
79+ . do_update ( )
80+ . set ( default_versions:: version_id. eq ( default_version. id ) )
81+ . execute ( conn)
82+ . await ?;
83+
84+ Ok ( ( ) )
85+ }
86+
4887/// Updates the `default_versions` table entry for the specified crate.
4988///
5089/// This function first loads all versions of the crate from the database,
@@ -79,6 +118,42 @@ pub fn update_default_version(crate_id: i32, conn: &mut impl Conn) -> QueryResul
79118 Ok ( ( ) )
80119}
81120
121+ /// Verifies that the default version for the specified crate is up-to-date.
122+ #[ instrument( skip( conn) ) ]
123+ pub async fn async_verify_default_version (
124+ crate_id : i32 ,
125+ conn : & mut AsyncPgConnection ,
126+ ) -> QueryResult < ( ) > {
127+ use diesel_async:: RunQueryDsl ;
128+
129+ let calculated = async_calculate_default_version ( crate_id, conn) . await ?;
130+
131+ let saved = default_versions:: table
132+ . select ( default_versions:: version_id)
133+ . filter ( default_versions:: crate_id. eq ( crate_id) )
134+ . first :: < i32 > ( conn)
135+ . await
136+ . optional ( ) ?;
137+
138+ if let Some ( saved) = saved {
139+ if saved == calculated. id {
140+ debug ! ( "Default version for crate {crate_id} is up to date" ) ;
141+ } else {
142+ warn ! (
143+ "Default version for crate {crate_id} is outdated (expected: {saved}, actual: {})" ,
144+ calculated. id,
145+ ) ;
146+ }
147+ } else {
148+ warn ! (
149+ "Default version for crate {crate_id} is missing (expected: {})" ,
150+ calculated. id
151+ ) ;
152+ }
153+
154+ Ok ( ( ) )
155+ }
156+
82157/// Verifies that the default version for the specified crate is up-to-date.
83158#[ instrument( skip( conn) ) ]
84159pub fn verify_default_version ( crate_id : i32 , conn : & mut impl Conn ) -> QueryResult < ( ) > {
@@ -111,6 +186,25 @@ pub fn verify_default_version(crate_id: i32, conn: &mut impl Conn) -> QueryResul
111186 Ok ( ( ) )
112187}
113188
189+ async fn async_calculate_default_version (
190+ crate_id : i32 ,
191+ conn : & mut AsyncPgConnection ,
192+ ) -> QueryResult < Version > {
193+ use diesel:: result:: Error :: NotFound ;
194+ use diesel_async:: RunQueryDsl ;
195+
196+ debug ! ( "Loading all versions for the crate…" ) ;
197+ let versions = versions:: table
198+ . filter ( versions:: crate_id. eq ( crate_id) )
199+ . select ( Version :: as_returning ( ) )
200+ . load :: < Version > ( conn)
201+ . await ?;
202+
203+ debug ! ( "Found {} versions" , versions. len( ) ) ;
204+
205+ versions. into_iter ( ) . max ( ) . ok_or ( NotFound )
206+ }
207+
114208fn calculate_default_version ( crate_id : i32 , conn : & mut impl Conn ) -> QueryResult < Version > {
115209 use diesel:: result:: Error :: NotFound ;
116210 use diesel:: RunQueryDsl ;
0 commit comments