|
7 | 7 | #![deny(warnings)]
|
8 | 8 |
|
9 | 9 | extern crate cargo_registry;
|
10 |
| -extern crate postgres; |
11 |
| -extern crate time; |
| 10 | +extern crate chrono; |
| 11 | +#[macro_use] |
| 12 | +extern crate diesel; |
| 13 | +#[macro_use] |
| 14 | +extern crate diesel_codegen; |
12 | 15 | extern crate rand;
|
13 | 16 |
|
14 |
| -use std::env; |
15 |
| -use time::Duration; |
| 17 | +use chrono::{Utc, NaiveDate, Duration}; |
| 18 | +use diesel::pg::PgConnection; |
| 19 | +use diesel::prelude::*; |
16 | 20 | use rand::{StdRng, Rng};
|
| 21 | +use std::env; |
| 22 | + |
| 23 | +use cargo_registry::schema::version_downloads; |
17 | 24 |
|
18 |
| -#[allow(dead_code)] |
19 | 25 | fn main() {
|
20 |
| - let conn = cargo_registry::db::connect_now_old(); |
21 |
| - { |
22 |
| - let tx = conn.transaction().unwrap(); |
23 |
| - update(&tx).unwrap(); |
24 |
| - tx.set_commit(); |
25 |
| - tx.finish().unwrap(); |
26 |
| - } |
| 26 | + let conn = cargo_registry::db::connect_now().unwrap(); |
| 27 | + conn.transaction(|| update(&conn)).unwrap(); |
27 | 28 | }
|
28 | 29 |
|
29 |
| -fn update(tx: &postgres::transaction::Transaction) -> postgres::Result<()> { |
| 30 | +fn update(conn: &PgConnection) -> QueryResult<()> { |
30 | 31 | let ids = env::args().skip(1).filter_map(
|
31 | 32 | |arg| arg.parse::<i32>().ok(),
|
32 | 33 | );
|
33 | 34 | for id in ids {
|
34 |
| - let now = time::now_utc().to_timespec(); |
35 | 35 | let mut rng = StdRng::new().unwrap();
|
36 | 36 | let mut dls = rng.gen_range(5000i32, 10000);
|
37 | 37 |
|
38 | 38 | for day in 0..90 {
|
39 |
| - let moment = now + Duration::days(-day); |
| 39 | + let moment = Utc::now().date().naive_utc() + Duration::days(-day); |
40 | 40 | dls += rng.gen_range(-100, 100);
|
41 |
| - tx.execute( |
42 |
| - "INSERT INTO version_downloads \ |
43 |
| - (version_id, downloads, date) \ |
44 |
| - VALUES ($1, $2, $3)", |
45 |
| - &[&id, &dls, &moment], |
46 |
| - )?; |
| 41 | + |
| 42 | + let version_download = VersionDownload { |
| 43 | + version_id: id, |
| 44 | + downloads: dls, |
| 45 | + date: moment, |
| 46 | + }; |
| 47 | + diesel::insert(&version_download) |
| 48 | + .into(version_downloads::table) |
| 49 | + .execute(conn)?; |
47 | 50 | }
|
48 | 51 | }
|
49 | 52 | Ok(())
|
50 | 53 | }
|
| 54 | + |
| 55 | +#[derive(Insertable)] |
| 56 | +#[table_name = "version_downloads"] |
| 57 | +struct VersionDownload { |
| 58 | + version_id: i32, |
| 59 | + downloads: i32, |
| 60 | + date: NaiveDate, |
| 61 | +} |
0 commit comments