Skip to content

Update to Diesel master #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ diesel = { version = "0.16.0", features = ["postgres"] }
[features]
unstable = []
lint = ["clippy"]

[replace]
"diesel:0.16.0" = { git = "https://github.com/diesel-rs/diesel.git" }
"diesel_codegen:0.16.0" = { git = "https://github.com/diesel-rs/diesel.git" }
26 changes: 10 additions & 16 deletions src/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ impl Badge {
krate: &Crate,
badges: Option<&'a HashMap<String, HashMap<String, String>>>,
) -> QueryResult<Vec<&'a str>> {
use diesel::{delete, insert};

#[derive(Insertable, Debug)]
#[table_name = "badges"]
struct NewBadge<'a> {
crate_id: i32,
badge_type: &'a str,
attributes: serde_json::Value,
}
use diesel::{delete, insert_into};

let mut invalid_badges = vec![];
let mut new_badges = vec![];
Expand All @@ -100,20 +92,22 @@ impl Badge {

let json = json!({"badge_type": k, "attributes": attributes_json});
if serde_json::from_value::<Badge>(json).is_ok() {
new_badges.push(NewBadge {
crate_id: krate.id,
badge_type: &**k,
attributes: attributes_json,
});
new_badges.push((
badges::crate_id.eq(krate.id),
badges::badge_type.eq(k),
badges::attributes.eq(attributes_json),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

));
} else {
invalid_badges.push(&**k);
}
}
}

conn.transaction(|| {
delete(badges::table.filter(badges::crate_id.eq(krate.id))).execute(conn)?;
insert(&new_badges).into(badges::table).execute(conn)?;
delete(badges::table)
.filter(badges::crate_id.eq(krate.id))
.execute(conn)?;
insert_into(badges::table).values(&new_badges).execute(conn)?;
Ok(invalid_badges)
})
}
Expand Down
29 changes: 8 additions & 21 deletions src/bin/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
#![deny(warnings)]

extern crate cargo_registry;
extern crate chrono;
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate rand;

use chrono::{Duration, NaiveDate, Utc};
use diesel::prelude::*;
use rand::{Rng, StdRng};
use std::env;
Expand All @@ -27,6 +22,8 @@ fn main() {
}

fn update(conn: &PgConnection) -> QueryResult<()> {
use diesel::dsl::*;

let ids = env::args()
.skip(1)
.filter_map(|arg| arg.parse::<i32>().ok());
Expand All @@ -35,26 +32,16 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
let mut dls = rng.gen_range(5000i32, 10000);

for day in 0..90 {
let moment = Utc::now().date().naive_utc() + Duration::days(-day);
dls += rng.gen_range(-100, 100);

let version_download = VersionDownload {
version_id: id,
downloads: dls,
date: moment,
};
diesel::insert(&version_download)
.into(version_downloads::table)
diesel::insert_into(version_downloads::table)
.values((
version_downloads::version_id.eq(id),
version_downloads::downloads.eq(dls),
version_downloads::date.eq(date(now - day.days())),
))
.execute(conn)?;
}
}
Ok(())
}

#[derive(Insertable)]
#[table_name = "version_downloads"]
struct VersionDownload {
version_id: i32,
downloads: i32,
date: NaiveDate,
}
2 changes: 1 addition & 1 deletion src/bin/render-readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern crate url;
use curl::easy::{Easy, List};
use chrono::{TimeZone, Utc};
use diesel::prelude::*;
use diesel::expression::any;
use diesel::dsl::any;
use docopt::Docopt;
use flate2::read::GzDecoder;
use itertools::Itertools;
Expand Down
Loading