Skip to content

Commit 3c92ca2

Browse files
committed
Use (version_id, date) to identify version_downloads
The `id` column will be removed in the next commit
1 parent 44060f3 commit 3c92ca2

File tree

5 files changed

+8
-28
lines changed

5 files changed

+8
-28
lines changed

mirage/fixtures/version-downloads.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ export default [
33
{
44
date: '2017-02-10T00:00:00Z',
55
downloads: 2,
6-
id: 3023900,
76
version: 40905,
87
},
98
{
109
date: '2017-02-10T00:00:00Z',
1110
downloads: 1,
12-
id: 3024236,
1311
version: 18445,
1412
},
1513
{
1614
date: '2017-02-11T00:00:00Z',
1715
downloads: 1,
18-
id: 3027018,
1916
version: 40905,
2017
},
2118
];

src/bin/update-downloads.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use cargo_registry::{
1212

1313
use diesel::prelude::*;
1414

15-
static LIMIT: i64 = 1000;
16-
1715
fn main() -> CargoResult<()> {
1816
let conn = db::connect_now()?;
1917
update(&conn)?;
@@ -25,18 +23,11 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
2523
use diesel::dsl::now;
2624
use diesel::select;
2725

28-
let mut max = Some(0);
29-
while let Some(m) = max {
30-
let rows = version_downloads
31-
.filter(processed.eq(false))
32-
.filter(id.gt(m))
33-
.filter(downloads.ne(counted))
34-
.order(id)
35-
.limit(LIMIT)
36-
.load(conn)?;
37-
collect(conn, &rows)?;
38-
max = rows.last().map(|d| d.id);
39-
}
26+
let rows = version_downloads
27+
.filter(processed.eq(false))
28+
.filter(downloads.ne(counted))
29+
.load(conn)?;
30+
collect(conn, &rows)?;
4031

4132
// Anything older than 24 hours ago will be frozen and will not be queried
4233
// against again.
@@ -62,7 +53,7 @@ fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
6253

6354
conn.transaction::<_, diesel::result::Error, _>(|| {
6455
// increment the number of counted downloads
65-
update(version_downloads::table.find(download.id))
56+
update(version_downloads::table.find(download.id()))
6657
.set(version_downloads::counted.eq(version_downloads::counted + amt))
6758
.execute(conn)?;
6859

src/models/download.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::views::EncodableVersionDownload;
77

88
#[derive(Queryable, Identifiable, Associations, Debug, Clone, Copy)]
99
#[belongs_to(Version)]
10+
#[primary_key(version_id, date)]
1011
pub struct VersionDownload {
11-
pub id: i32,
1212
pub version_id: i32,
1313
pub downloads: i32,
1414
pub counted: i32,
@@ -34,7 +34,6 @@ impl VersionDownload {
3434

3535
pub fn encodable(self) -> EncodableVersionDownload {
3636
EncodableVersionDownload {
37-
id: self.id,
3837
version: self.version_id,
3938
downloads: self.downloads,
4039
date: self.date.to_string(),

src/schema.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,7 @@ table! {
815815
/// Representation of the `version_downloads` table.
816816
///
817817
/// (Automatically generated by Diesel.)
818-
version_downloads (id) {
819-
/// The `id` column of the `version_downloads` table.
820-
///
821-
/// Its SQL type is `Int4`.
822-
///
823-
/// (Automatically generated by Diesel.)
824-
id -> Int4,
818+
version_downloads (version_id, date) {
825819
/// The `version_id` column of the `version_downloads` table.
826820
///
827821
/// Its SQL type is `Int4`.

src/views.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub struct EncodableDependency {
6666

6767
#[derive(Serialize, Deserialize, Debug)]
6868
pub struct EncodableVersionDownload {
69-
pub id: i32,
7069
pub version: i32,
7170
pub downloads: i32,
7271
pub date: String,

0 commit comments

Comments
 (0)