Skip to content

Commit f032ff0

Browse files
committed
Don't update counts for 0 downloads
If the amount is 0 then those queries will just spuriously update the updated_at timestamp. Closes #237
1 parent aa810c4 commit f032ff0

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/bin/update-downloads.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ fn collect(tx: &postgres::Transaction,
8888
}
8989
let amt = download.downloads - download.counted;
9090

91+
// Flag this row as having been processed if we're passed the cutoff,
92+
// and unconditionally increment the number of counted downloads.
93+
try!(tx.execute("UPDATE version_downloads
94+
SET processed = $2, counted = counted + $3
95+
WHERE id = $1",
96+
&[id, &(download.date < cutoff), &amt]));
97+
total += amt as i64;
98+
99+
if amt == 0 {
100+
continue
101+
}
102+
91103
let crate_id = Version::find(tx, download.version_id).unwrap().crate_id;
92104

93105
// Update the total number of version downloads
@@ -110,14 +122,6 @@ fn collect(tx: &postgres::Transaction,
110122
VALUES ($1, $2, $3)",
111123
&[&crate_id, &amt, &download.date]));
112124
}
113-
114-
// Flag this row as having been processed if we're passed the cutoff,
115-
// and unconditionally increment the number of counted downloads.
116-
try!(tx.execute("UPDATE version_downloads
117-
SET processed = $2, counted = counted + $3
118-
WHERE id = $1",
119-
&[id, &(download.date < cutoff), &amt]));
120-
total += amt as i64;
121125
}
122126

123127
// After everything else is done, update the global counter of total
@@ -267,4 +271,35 @@ mod test {
267271
::update(&tx).unwrap();
268272
assert_eq!(Version::find(&tx, version.id).unwrap().downloads, 2);
269273
}
274+
275+
#[test]
276+
fn set_processed_no_set_updated_at() {
277+
let conn = conn();
278+
let tx = conn.transaction().unwrap();
279+
let user = user(&tx);
280+
let krate = Crate::find_or_insert(&tx, "foo", user.id, &None,
281+
&None, &None, &None, &[], &None,
282+
&None, &None, None).unwrap();
283+
let version = Version::insert(&tx, krate.id,
284+
&semver::Version::parse("1.0.0").unwrap(),
285+
&HashMap::new(), &[]).unwrap();
286+
tx.execute("UPDATE versions
287+
SET updated_at = current_date - interval '2 days'",
288+
&[]).unwrap();
289+
tx.execute("UPDATE crates
290+
SET updated_at = current_date - interval '2 days'",
291+
&[]).unwrap();
292+
tx.execute("INSERT INTO version_downloads \
293+
(version_id, downloads, counted, date, processed)
294+
VALUES ($1, 2, 2, current_date - interval '2 days', false)",
295+
&[&version.id]).unwrap();
296+
297+
let version_before = Version::find(&tx, version.id).unwrap();
298+
let krate_before = Crate::find(&tx, krate.id).unwrap();
299+
::update(&tx).unwrap();
300+
let version2 = Version::find(&tx, version.id).unwrap();
301+
assert_eq!(version2.updated_at, version_before.updated_at);
302+
let krate2 = Crate::find(&tx, krate.id).unwrap();
303+
assert_eq!(krate2.updated_at, krate_before.updated_at);
304+
}
270305
}

0 commit comments

Comments
 (0)