@@ -126,6 +126,12 @@ fn batch_update(batch_size: i64, conn: &mut PgConnection) -> QueryResult<i64> {
126
126
SET downloads = crates.downloads + crate_downloads_batch.downloads
127
127
FROM crate_downloads_batch
128
128
WHERE crates.id = crate_downloads_batch.crate_id
129
+ ), updated_crate_downloads AS (
130
+ -- Update the `downloads` count for each crate in the `crate_downloads` table.
131
+ UPDATE crate_downloads
132
+ SET downloads = crate_downloads.downloads + crate_downloads_batch.downloads
133
+ FROM crate_downloads_batch
134
+ WHERE crate_downloads.crate_id = crate_downloads_batch.crate_id
129
135
), updated_metadata AS (
130
136
-- Update the `total_downloads` count in the `metadata` table.
131
137
UPDATE metadata
@@ -170,7 +176,7 @@ mod tests {
170
176
use super :: * ;
171
177
use crate :: email:: Emails ;
172
178
use crate :: models:: { Crate , NewCrate , NewUser , NewVersion , User , Version } ;
173
- use crate :: schema:: { crates, versions} ;
179
+ use crate :: schema:: { crate_downloads , crates, versions} ;
174
180
use crate :: test_util:: test_db_connection;
175
181
use std:: collections:: BTreeMap ;
176
182
@@ -224,17 +230,27 @@ mod tests {
224
230
. unwrap ( ) ;
225
231
226
232
super :: update ( conn) . unwrap ( ) ;
233
+
227
234
let version_downloads = versions:: table
228
235
. find ( version. id )
229
236
. select ( versions:: downloads)
230
237
. first ( conn) ;
231
238
assert_eq ! ( version_downloads, Ok ( 1 ) ) ;
239
+
232
240
let crate_downloads = crates:: table
233
241
. find ( krate. id )
234
242
. select ( crates:: downloads)
235
243
. first ( conn) ;
236
244
assert_eq ! ( crate_downloads, Ok ( 1 ) ) ;
245
+
246
+ let crate_downloads = crate_downloads:: table
247
+ . find ( krate. id )
248
+ . select ( crate_downloads:: downloads)
249
+ . first ( conn) ;
250
+ assert_eq ! ( crate_downloads, Ok ( 1 ) ) ;
251
+
237
252
super :: update ( conn) . unwrap ( ) ;
253
+
238
254
let version_downloads = versions:: table
239
255
. find ( version. id )
240
256
. select ( versions:: downloads)
@@ -330,17 +346,29 @@ mod tests {
330
346
. filter ( crates:: id. eq ( krate. id ) )
331
347
. first ( conn)
332
348
. unwrap ( ) ;
349
+
333
350
super :: update ( conn) . unwrap ( ) ;
351
+
334
352
let version2: Version = versions:: table. find ( version. id ) . first ( conn) . unwrap ( ) ;
335
353
assert_eq ! ( version2. downloads, 2 ) ;
336
354
assert_eq ! ( version2. updated_at, version_before. updated_at) ;
355
+
337
356
let krate2: Crate = Crate :: all ( )
338
357
. filter ( crates:: id. eq ( krate. id ) )
339
358
. first ( conn)
340
359
. unwrap ( ) ;
341
360
assert_eq ! ( krate2. downloads, 2 ) ;
342
361
assert_eq ! ( krate2. updated_at, krate_before. updated_at) ;
362
+
363
+ let krate2_downloads: i64 = crate_downloads:: table
364
+ . find ( krate. id )
365
+ . select ( crate_downloads:: downloads)
366
+ . first ( conn)
367
+ . unwrap ( ) ;
368
+ assert_eq ! ( krate2_downloads, 2 ) ;
369
+
343
370
super :: update ( conn) . unwrap ( ) ;
371
+
344
372
let version3: Version = versions:: table. find ( version. id ) . first ( conn) . unwrap ( ) ;
345
373
assert_eq ! ( version3. downloads, 2 ) ;
346
374
}
0 commit comments