Skip to content

Commit 2e9f46e

Browse files
committed
Don't select the versions::published_by_email column ever
A workaround is needed to get the all columns pattern to work with the boxed query pattern, see diesel-rs/diesel#1979 I don't like how many places needed to be changed, going to see if I can refactor a bit.
1 parent efcd9e9 commit 2e9f46e

File tree

10 files changed

+84
-19
lines changed

10 files changed

+84
-19
lines changed

src/bin/delete-version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn delete(conn: &PgConnection) {
4747
let krate = Crate::by_name(&name).first::<Crate>(conn).unwrap();
4848
let v = Version::belonging_to(&krate)
4949
.filter(versions::num.eq(&version))
50+
.select(cargo_registry::models::version::ALL_COLUMNS)
5051
.first::<Version>(conn)
5152
.unwrap();
5253
print!(

src/bin/render-readmes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn main() {
112112
let versions = versions::table
113113
.inner_join(crates::table)
114114
.filter(versions::id.eq(any(ids)))
115-
.select((versions::all_columns, crates::name))
115+
.select((cargo_registry::models::version::ALL_COLUMNS, crates::name))
116116
.load::<(Version, String)>(&conn)
117117
.expect("error loading versions");
118118

src/bin/update-downloads.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ mod test {
288288

289289
let version_before = versions::table
290290
.find(version.id)
291+
.select(cargo_registry::models::version::ALL_COLUMNS)
291292
.first::<Version>(&conn)
292293
.unwrap();
293294
let krate_before = Crate::all()
@@ -297,6 +298,7 @@ mod test {
297298
crate::update(&conn).unwrap();
298299
let version2 = versions::table
299300
.find(version.id)
301+
.select(cargo_registry::models::version::ALL_COLUMNS)
300302
.first::<Version>(&conn)
301303
.unwrap();
302304
assert_eq!(version2.downloads, 2);
@@ -311,6 +313,7 @@ mod test {
311313
crate::update(&conn).unwrap();
312314
let version3 = versions::table
313315
.find(version.id)
316+
.select(cargo_registry::models::version::ALL_COLUMNS)
314317
.first::<Version>(&conn)
315318
.unwrap();
316319
assert_eq!(version3.downloads, 2);

src/controllers/krate/metadata.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ pub fn show(req: &mut dyn Request) -> CargoResult<Response> {
110110
let mut versions_and_publishers: Vec<(Version, Option<User>)> = krate
111111
.all_versions()
112112
.left_outer_join(users::table)
113-
.select((versions::all_columns, users::all_columns.nullable()))
113+
.select((
114+
crate::models::version::ALL_COLUMNS,
115+
users::all_columns.nullable(),
116+
))
114117
.load(&*conn)?;
115118
versions_and_publishers.sort_by(|a, b| b.0.num.cmp(&a.0.num));
116119
let ids = versions_and_publishers.iter().map(|v| v.0.id).collect();
@@ -193,7 +196,10 @@ pub fn versions(req: &mut dyn Request) -> CargoResult<Response> {
193196
let mut versions_and_publishers: Vec<(Version, Option<User>)> = krate
194197
.all_versions()
195198
.left_outer_join(users::table)
196-
.select((versions::all_columns, users::all_columns.nullable()))
199+
.select((
200+
crate::models::version::ALL_COLUMNS,
201+
users::all_columns.nullable(),
202+
))
197203
.load(&*conn)?;
198204
versions_and_publishers.sort_by(|a, b| b.0.num.cmp(&a.0.num));
199205
let versions = versions_and_publishers
@@ -229,7 +235,7 @@ pub fn reverse_dependencies(req: &mut dyn Request) -> CargoResult<Response> {
229235
.inner_join(crates::table)
230236
.left_outer_join(users::table)
231237
.select((
232-
versions::all_columns,
238+
crate::models::version::ALL_COLUMNS,
233239
crates::name,
234240
users::all_columns.nullable(),
235241
))

src/controllers/user/me.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn updates(req: &mut dyn Request) -> CargoResult<Response> {
5959
.filter(crates::id.eq(any(followed_crates)))
6060
.order(versions::created_at.desc())
6161
.select((
62-
versions::all_columns,
62+
crate::models::version::ALL_COLUMNS,
6363
crates::name,
6464
users::all_columns.nullable(),
6565
))

src/controllers/version/deprecated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn index(req: &mut dyn Request) -> CargoResult<Response> {
2626
.inner_join(crates::table)
2727
.left_outer_join(users::table)
2828
.select((
29-
versions::all_columns,
29+
crate::models::version::ALL_COLUMNS,
3030
crates::name,
3131
users::all_columns.nullable(),
3232
))
@@ -55,7 +55,7 @@ pub fn show_by_id(req: &mut dyn Request) -> CargoResult<Response> {
5555
.inner_join(crates::table)
5656
.left_outer_join(users::table)
5757
.select((
58-
versions::all_columns,
58+
crate::models::version::ALL_COLUMNS,
5959
crate::models::krate::ALL_COLUMNS,
6060
users::all_columns.nullable(),
6161
))

src/models/krate.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use chrono::{NaiveDate, NaiveDateTime};
22
use diesel::associations::Identifiable;
3+
use diesel::dsl::SqlTypeOf;
34
use diesel::pg::Pg;
45
use diesel::prelude::*;
6+
use diesel::query_builder::BoxedSelectStatement;
57
use url::Url;
68

79
use crate::app::App;
@@ -559,28 +561,42 @@ mod tests {
559561
}
560562
}
561563

564+
// This type definition is a workaround to make the BoxedQuery `all_version` methods on Crate work
565+
// with the version `AllColumns` type definition; see this diesel issue:
566+
// https://github.com/diesel-rs/diesel/issues/1979
567+
pub type BoxedVersionQuery<'a, Backend> = BoxedSelectStatement<
568+
'a,
569+
SqlTypeOf<crate::models::version::AllColumns>,
570+
versions::table,
571+
Backend,
572+
>;
573+
562574
pub trait CrateVersions {
563-
fn versions(&self) -> versions::BoxedQuery<'_, Pg> {
575+
fn versions(&self) -> BoxedVersionQuery<'_, Pg> {
564576
self.all_versions().filter(versions::yanked.eq(false))
565577
}
566578

567-
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg>;
579+
fn all_versions(&self) -> BoxedVersionQuery<'_, Pg>;
568580
}
569581

570582
impl CrateVersions for Crate {
571-
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
572-
Version::belonging_to(self).into_boxed()
583+
fn all_versions(&self) -> BoxedVersionQuery<'_, Pg> {
584+
Version::belonging_to(self)
585+
.select(crate::models::version::ALL_COLUMNS)
586+
.into_boxed()
573587
}
574588
}
575589

576590
impl CrateVersions for Vec<Crate> {
577-
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
591+
fn all_versions(&self) -> BoxedVersionQuery<'_, Pg> {
578592
self.as_slice().all_versions()
579593
}
580594
}
581595

582596
impl CrateVersions for [Crate] {
583-
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
584-
Version::belonging_to(self).into_boxed()
597+
fn all_versions(&self) -> BoxedVersionQuery<'_, Pg> {
598+
Version::belonging_to(self)
599+
.select(crate::models::version::ALL_COLUMNS)
600+
.into_boxed()
585601
}
586602
}

src/models/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ mod rights;
3030
mod team;
3131
mod token;
3232
mod user;
33-
mod version;
33+
pub mod version;

src/models/version.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ pub struct Version {
2626
pub published_by: Option<i32>,
2727
}
2828

29+
/// The database stores the verified email address of the person who published the version at the
30+
/// time the version was published, so that even if someone removes the verified email address from
31+
/// their account, we still have a way to try to contact them in case of a DMCA complaint. However,
32+
/// this information is private, only for crates.io administrator access, and should never be
33+
/// returned from any API calls. This type excludes the `published_by_email` column.
34+
pub type AllColumns = (
35+
versions::id,
36+
versions::crate_id,
37+
versions::num,
38+
versions::updated_at,
39+
versions::created_at,
40+
versions::downloads,
41+
versions::features,
42+
versions::yanked,
43+
versions::license,
44+
versions::crate_size,
45+
versions::published_by,
46+
);
47+
48+
pub const ALL_COLUMNS: AllColumns = (
49+
versions::id,
50+
versions::crate_id,
51+
versions::num,
52+
versions::updated_at,
53+
versions::created_at,
54+
versions::downloads,
55+
versions::features,
56+
versions::yanked,
57+
versions::license,
58+
versions::crate_size,
59+
versions::published_by,
60+
);
61+
62+
type All = diesel::dsl::Select<versions::table, AllColumns>;
63+
2964
#[derive(Insertable, Debug)]
3065
#[table_name = "versions"]
3166
pub struct NewVersion {
@@ -38,6 +73,10 @@ pub struct NewVersion {
3873
}
3974

4075
impl Version {
76+
pub fn all() -> All {
77+
versions::table.select(ALL_COLUMNS)
78+
}
79+
4180
pub fn encodable(self, crate_name: &str, published_by: Option<User>) -> EncodableVersion {
4281
let Version {
4382
id,
@@ -114,10 +153,8 @@ impl Version {
114153
/// Gets the User who ran `cargo publish` for this version, if recorded.
115154
/// Not for use when you have a group of versions you need the publishers for.
116155
pub fn published_by(&self, conn: &PgConnection) -> Option<User> {
117-
match self.published_by {
118-
Some(pb) => users::table.find(pb).first(conn).ok(),
119-
None => None,
120-
}
156+
self.published_by
157+
.and_then(|pb| users::table.find(pb).first(conn).ok())
121158
}
122159
}
123160

@@ -168,6 +205,7 @@ impl NewVersion {
168205

169206
let version = insert_into(versions)
170207
.values(self)
208+
.returning(ALL_COLUMNS)
171209
.get_result::<Version>(conn)?;
172210

173211
let new_authors = authors

src/tests/builders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl<'a> VersionBuilder<'a> {
9595
if self.yanked {
9696
vers = update(&vers)
9797
.set(versions::yanked.eq(true))
98+
.returning(cargo_registry::models::version::ALL_COLUMNS)
9899
.get_result(connection)?;
99100
}
100101

0 commit comments

Comments
 (0)