@@ -5,11 +5,12 @@ use serialize::hex::ToHex;
55use time:: Timespec ;
66
77use conduit:: { Request , Response } ;
8- use conduit_router:: RequestParams ;
98use conduit_json_parser;
10- use pg:: { PostgresRow , PostgresStatement } ;
11- use pg:: types:: ToSql ;
9+ use conduit_router:: RequestParams ;
1210use curl:: http;
11+ use pg:: types:: ToSql ;
12+ use pg:: { PostgresRow , PostgresStatement } ;
13+ use semver;
1314
1415use app:: { App , RequestApp } ;
1516use db:: { Connection , RequestTransaction } ;
@@ -29,6 +30,7 @@ pub struct Package {
2930 pub updated_at : Timespec ,
3031 pub created_at : Timespec ,
3132 pub downloads : i32 ,
33+ pub max_version : semver:: Version ,
3234}
3335
3436#[ deriving( Encodable , Decodable ) ]
@@ -39,17 +41,20 @@ pub struct EncodablePackage {
3941 pub updated_at : String ,
4042 pub created_at : String ,
4143 pub downloads : i32 ,
44+ pub max_version : String ,
4245}
4346
4447impl Package {
4548 pub fn from_row ( row : & PostgresRow ) -> Package {
49+ let max: String = row. get ( "max_version" ) ;
4650 Package {
4751 id : row. get ( "id" ) ,
4852 name : row. get ( "name" ) ,
4953 user_id : row. get ( "user_id" ) ,
5054 updated_at : row. get ( "updated_at" ) ,
5155 created_at : row. get ( "created_at" ) ,
5256 downloads : row. get ( "downloads" ) ,
57+ max_version : semver:: Version :: parse ( max. as_slice ( ) ) . unwrap ( ) ,
5358 }
5459 }
5560
@@ -83,8 +88,8 @@ impl Package {
8388 }
8489 let stmt = try!( conn. prepare ( "INSERT INTO packages \
8590 (name, user_id, created_at,
86- updated_at, downloads) \
87- VALUES ($1, $2, $3, $4, 0) \
91+ updated_at, downloads, max_version ) \
92+ VALUES ($1, $2, $3, $4, 0, '0.0.0' ) \
8893 RETURNING *") ) ;
8994 let now = :: now ( ) ;
9095 let mut rows = try!( stmt. query ( & [ & name as & ToSql , & user_id, & now, & now] ) ) ;
@@ -99,14 +104,16 @@ impl Package {
99104 }
100105
101106 fn encodable ( self , versions : Vec < i32 > ) -> EncodablePackage {
102- let Package { name, created_at, updated_at, downloads, .. } = self ;
107+ let Package { name, created_at, updated_at, downloads,
108+ max_version, .. } = self ;
103109 EncodablePackage {
104110 id : name. clone ( ) ,
105111 name : name,
106112 versions : versions,
107113 updated_at : :: encode_time ( updated_at) ,
108114 created_at : :: encode_time ( created_at) ,
109115 downloads : downloads,
116+ max_version : max_version. to_string ( ) ,
110117 }
111118 }
112119
@@ -144,6 +151,17 @@ impl Package {
144151 p. encodable ( map. pop ( & id) . unwrap ( ) )
145152 } ) . collect ( ) )
146153 }
154+
155+ pub fn add_version ( & self , conn : & Connection , num : & str )
156+ -> CargoResult < Version > {
157+ let ver = semver:: Version :: parse ( num) . unwrap ( ) ;
158+ let max = if ver > self . max_version { & ver} else { & self . max_version } ;
159+ let max = max. to_string ( ) ;
160+ try!( conn. execute ( "UPDATE packages SET updated_at = $1, max_version = $2
161+ WHERE id = $3" ,
162+ & [ & :: now ( ) , & max, & self . id ] ) ) ;
163+ Version :: insert ( conn, self . id , num)
164+ }
147165}
148166
149167pub fn index ( req : & mut Request ) -> CargoResult < Response > {
@@ -296,8 +314,7 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
296314 }
297315 None => { }
298316 }
299- let vers = try!( Version :: insert ( try!( req. tx ( ) ) , pkg. id ,
300- new_pkg. vers . as_slice ( ) ) ) ;
317+ let vers = try!( pkg. add_version ( try!( req. tx ( ) ) , new_pkg. vers . as_slice ( ) ) ) ;
301318
302319 // Link this new version to all dependencies
303320 for dep in new_pkg. deps . iter ( ) {
0 commit comments