@@ -5,11 +5,12 @@ use serialize::hex::ToHex;
5
5
use time:: Timespec ;
6
6
7
7
use conduit:: { Request , Response } ;
8
- use conduit_router:: RequestParams ;
9
8
use conduit_json_parser;
10
- use pg:: { PostgresRow , PostgresStatement } ;
11
- use pg:: types:: ToSql ;
9
+ use conduit_router:: RequestParams ;
12
10
use curl:: http;
11
+ use pg:: types:: ToSql ;
12
+ use pg:: { PostgresRow , PostgresStatement } ;
13
+ use semver;
13
14
14
15
use app:: { App , RequestApp } ;
15
16
use db:: { Connection , RequestTransaction } ;
@@ -29,6 +30,7 @@ pub struct Package {
29
30
pub updated_at : Timespec ,
30
31
pub created_at : Timespec ,
31
32
pub downloads : i32 ,
33
+ pub max_version : semver:: Version ,
32
34
}
33
35
34
36
#[ deriving( Encodable , Decodable ) ]
@@ -39,17 +41,20 @@ pub struct EncodablePackage {
39
41
pub updated_at : String ,
40
42
pub created_at : String ,
41
43
pub downloads : i32 ,
44
+ pub max_version : String ,
42
45
}
43
46
44
47
impl Package {
45
48
pub fn from_row ( row : & PostgresRow ) -> Package {
49
+ let max: String = row. get ( "max_version" ) ;
46
50
Package {
47
51
id : row. get ( "id" ) ,
48
52
name : row. get ( "name" ) ,
49
53
user_id : row. get ( "user_id" ) ,
50
54
updated_at : row. get ( "updated_at" ) ,
51
55
created_at : row. get ( "created_at" ) ,
52
56
downloads : row. get ( "downloads" ) ,
57
+ max_version : semver:: Version :: parse ( max. as_slice ( ) ) . unwrap ( ) ,
53
58
}
54
59
}
55
60
@@ -83,8 +88,8 @@ impl Package {
83
88
}
84
89
let stmt = try!( conn. prepare ( "INSERT INTO packages \
85
90
(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' ) \
88
93
RETURNING *") ) ;
89
94
let now = :: now ( ) ;
90
95
let mut rows = try!( stmt. query ( & [ & name as & ToSql , & user_id, & now, & now] ) ) ;
@@ -99,14 +104,16 @@ impl Package {
99
104
}
100
105
101
106
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 ;
103
109
EncodablePackage {
104
110
id : name. clone ( ) ,
105
111
name : name,
106
112
versions : versions,
107
113
updated_at : :: encode_time ( updated_at) ,
108
114
created_at : :: encode_time ( created_at) ,
109
115
downloads : downloads,
116
+ max_version : max_version. to_string ( ) ,
110
117
}
111
118
}
112
119
@@ -144,6 +151,17 @@ impl Package {
144
151
p. encodable ( map. pop ( & id) . unwrap ( ) )
145
152
} ) . collect ( ) )
146
153
}
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
+ }
147
165
}
148
166
149
167
pub fn index ( req : & mut Request ) -> CargoResult < Response > {
@@ -296,8 +314,7 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
296
314
}
297
315
None => { }
298
316
}
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 ( ) ) ) ;
301
318
302
319
// Link this new version to all dependencies
303
320
for dep in new_pkg. deps . iter ( ) {
0 commit comments