Skip to content

Commit 05d172f

Browse files
authored
Merge pull request #11625 from Turbo87/selectable
Derive `Selectable` trait for more model structs
2 parents 5c088d8 + bce2aa9 commit 05d172f

File tree

13 files changed

+42
-17
lines changed

13 files changed

+42
-17
lines changed

crates/crates_io_database/src/models/action.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl From<VersionAction> for String {
3434
}
3535
}
3636

37-
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)]
37+
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Selectable, Associations)]
3838
#[diesel(
3939
table_name = version_owner_actions,
4040
check_for_backend(diesel::pg::Pg),
@@ -66,6 +66,7 @@ impl VersionOwnerAction {
6666
version_owner_actions::table
6767
.filter(version_id.eq(version.id))
6868
.inner_join(users::table)
69+
.select((VersionOwnerAction::as_select(), User::as_select()))
6970
.order(version_owner_actions::dsl::id)
7071
.load(conn)
7172
.boxed()
@@ -77,6 +78,7 @@ impl VersionOwnerAction {
7778
) -> QueryResult<Vec<Vec<(Self, User)>>> {
7879
Ok(Self::belonging_to(versions)
7980
.inner_join(users::table)
81+
.select((VersionOwnerAction::as_select(), User::as_select()))
8082
.order(version_owner_actions::dsl::id)
8183
.load(conn)
8284
.await?

crates/crates_io_database/src/models/crate_owner_invitation.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl NewCrateOwnerInvitation {
4343
// already exists. This does not cause problems with expired invitation as those are
4444
// deleted before doing this INSERT.
4545
.on_conflict_do_nothing()
46+
.returning(CrateOwnerInvitation::as_returning())
4647
.get_result(conn)
4748
.await
4849
.optional()?;
@@ -57,7 +58,7 @@ impl NewCrateOwnerInvitation {
5758
}
5859

5960
/// The model representing a row in the `crate_owner_invitations` database table.
60-
#[derive(Clone, Debug, Identifiable, Queryable)]
61+
#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
6162
#[diesel(primary_key(invited_user_id, crate_id))]
6263
pub struct CrateOwnerInvitation {
6364
pub invited_user_id: i32,
@@ -77,13 +78,15 @@ impl CrateOwnerInvitation {
7778
) -> QueryResult<Self> {
7879
crate_owner_invitations::table
7980
.find((user_id, crate_id))
81+
.select(CrateOwnerInvitation::as_select())
8082
.first::<Self>(conn)
8183
.await
8284
}
8385

8486
pub async fn find_by_token(token: &str, conn: &mut AsyncPgConnection) -> QueryResult<Self> {
8587
crate_owner_invitations::table
8688
.filter(crate_owner_invitations::token.eq(token))
89+
.select(CrateOwnerInvitation::as_select())
8790
.first::<Self>(conn)
8891
.await
8992
}
@@ -103,7 +106,11 @@ impl CrateOwnerInvitation {
103106
}
104107

105108
// Get the user and check if they have a verified email
106-
let user: User = users::table.find(self.invited_user_id).first(conn).await?;
109+
let user: User = users::table
110+
.find(self.invited_user_id)
111+
.select(User::as_select())
112+
.first(conn)
113+
.await?;
107114

108115
let verified_email = user.verified_email(conn).await?;
109116
if verified_email.is_none() {

crates/crates_io_database/src/models/download.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ use chrono::NaiveDate;
44
use crates_io_diesel_helpers::SemverVersion;
55
use diesel::prelude::*;
66

7-
#[derive(Queryable, Identifiable, Associations, Debug, Clone, Copy)]
7+
#[derive(Queryable, Identifiable, Selectable, Associations, Debug, Clone, Copy)]
88
#[diesel(
99
primary_key(version_id, date),
1010
belongs_to(FullVersion, foreign_key=version_id),
1111
belongs_to(Version),
1212
)]
1313
pub struct VersionDownload {
1414
pub version_id: i32,
15-
pub downloads: i32,
16-
pub counted: i32,
1715
pub date: NaiveDate,
18-
pub processed: bool,
16+
pub downloads: i32,
1917
}
2018

2119
/// A subset of the columns of the `versions` table.

crates/crates_io_database/src/models/email.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use bon::Builder;
2-
use chrono::{DateTime, Utc};
32
use diesel::prelude::*;
43
use diesel_async::{AsyncPgConnection, RunQueryDsl};
54
use secrecy::SecretString;
65

76
use crate::models::User;
87
use crate::schema::emails;
98

10-
#[derive(Debug, Queryable, Identifiable, Associations)]
9+
#[derive(Debug, Queryable, Identifiable, Selectable, Associations)]
1110
#[diesel(belongs_to(User))]
1211
pub struct Email {
1312
pub id: i32,
@@ -16,7 +15,6 @@ pub struct Email {
1615
pub verified: bool,
1716
#[diesel(deserialize_as = String, serialize_as = String)]
1817
pub token: SecretString,
19-
pub token_generated_at: Option<DateTime<Utc>>,
2018
}
2119

2220
#[derive(Debug, Insertable, AsChangeset, Builder)]

crates/crates_io_database/src/models/user.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use crates_io_diesel_helpers::lower;
1616
#[derive(Clone, Debug, Queryable, Identifiable, Selectable, Serialize)]
1717
pub struct User {
1818
pub id: i32,
19+
pub name: Option<String>,
20+
pub gh_id: i32,
21+
pub gh_login: String,
22+
pub gh_avatar: Option<String>,
1923
#[diesel(deserialize_as = String)]
2024
#[serde(skip)]
2125
pub gh_access_token: SecretString,
22-
pub gh_login: String,
23-
pub name: Option<String>,
24-
pub gh_avatar: Option<String>,
25-
pub gh_id: i32,
2626
pub account_lock_reason: Option<String>,
2727
pub account_lock_until: Option<DateTime<Utc>>,
2828
pub is_admin: bool,
@@ -31,13 +31,18 @@ pub struct User {
3131

3232
impl User {
3333
pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> QueryResult<User> {
34-
users::table.find(id).first(conn).await
34+
users::table
35+
.find(id)
36+
.select(User::as_select())
37+
.first(conn)
38+
.await
3539
}
3640

3741
pub async fn find_by_login(conn: &mut AsyncPgConnection, login: &str) -> QueryResult<User> {
3842
users::table
3943
.filter(lower(users::gh_login).eq(login.to_lowercase()))
4044
.filter(users::gh_id.ne(-1))
45+
.select(User::as_select())
4146
.order(users::gh_id.desc())
4247
.first(conn)
4348
.await
@@ -96,6 +101,7 @@ impl NewUser<'_> {
96101
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<User> {
97102
diesel::insert_into(users::table)
98103
.values(self)
104+
.returning(User::as_returning())
99105
.get_result(conn)
100106
.await
101107
}
@@ -120,6 +126,7 @@ impl NewUser<'_> {
120126
users::gh_avatar.eq(excluded(users::gh_avatar)),
121127
users::gh_access_token.eq(excluded(users::gh_access_token)),
122128
))
129+
.returning(User::as_returning())
123130
.get_result(conn)
124131
.await
125132
}

crates/crates_io_database/src/models/version.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use serde::Deserialize;
1010
use crate::models::{Crate, TrustpubData, User};
1111
use crate::schema::{readme_renderings, users, versions};
1212

13-
// Queryable has a custom implementation below
1413
#[derive(Clone, Identifiable, Associations, Debug, Queryable, Selectable)]
1514
#[diesel(belongs_to(Crate), belongs_to(crate::models::download::Version, foreign_key=id))]
1615
pub struct Version {
@@ -59,7 +58,12 @@ impl Version {
5958
/// Not for use when you have a group of versions you need the publishers for.
6059
pub async fn published_by(&self, conn: &mut AsyncPgConnection) -> QueryResult<Option<User>> {
6160
match self.published_by {
62-
Some(pb) => users::table.find(pb).first(conn).await.optional(),
61+
Some(pb) => users::table
62+
.find(pb)
63+
.select(User::as_select())
64+
.first(conn)
65+
.await
66+
.optional(),
6367
None => Ok(None),
6468
}
6569
}

src/bin/crates-admin/transfer_crates.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
2727
async fn transfer(opts: Opts, conn: &mut AsyncPgConnection) -> anyhow::Result<()> {
2828
let from: User = users::table
2929
.filter(users::gh_login.eq(opts.from_user))
30+
.select(User::as_select())
3031
.first(conn)
3132
.await?;
3233

3334
let to: User = users::table
3435
.filter(users::gh_login.eq(opts.to_user))
36+
.select(User::as_select())
3537
.first(conn)
3638
.await?;
3739

src/controllers/crate_owner_invitation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ async fn prepare_list(
266266
if !missing_users.is_empty() {
267267
let new_users: Vec<User> = users::table
268268
.filter(users::id.eq_any(missing_users))
269+
.select(User::as_select())
269270
.load(conn)
270271
.await?;
271272
for user in new_users.into_iter() {

src/controllers/krate/downloads.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub async fn get_crate_downloads(
111111
let (downloads, extra_downloads, versions_and_publishers, actions) = tokio::try_join!(
112112
VersionDownload::belonging_to(latest_five)
113113
.filter(version_downloads::date.gt(date(now - 90.days())))
114+
.select(VersionDownload::as_select())
114115
.order((
115116
version_downloads::date.asc(),
116117
version_downloads::version_id.desc(),
@@ -187,6 +188,7 @@ fn load_actions<'a>(
187188
}
188189
VersionOwnerAction::belonging_to(versions)
189190
.inner_join(users::table)
191+
.select((VersionOwnerAction::as_select(), User::as_select()))
190192
.order(version_owner_actions::id)
191193
.load(conn)
192194
.boxed()

src/controllers/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ async fn create_or_update_user(
204204
async fn find_user_by_gh_id(conn: &mut AsyncPgConnection, gh_id: i32) -> QueryResult<Option<User>> {
205205
users::table
206206
.filter(users::gh_id.eq(gh_id))
207+
.select(User::as_select())
207208
.first(conn)
208209
.await
209210
.optional()

0 commit comments

Comments
 (0)