Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
10036a3
database/models: Use `HasQuery` for `Keyword`
Turbo87 Oct 17, 2025
1ce6c33
database/models: Use `HasQuery` for `Team`
Turbo87 Oct 17, 2025
cb01cda
database/models: Use `HasQuery` for `GitHubConfig`
Turbo87 Oct 17, 2025
a1481de
database/models: Use `HasQuery` for `GitLabConfig`
Turbo87 Oct 17, 2025
00bc898
database/models: Use `HasQuery` for `CloudFrontInvalidationQueueItem`
Turbo87 Oct 17, 2025
b61c14d
database/models: Use `HasQuery` for `VersionDownload`
Turbo87 Oct 17, 2025
79eeaa5
database/models: Use `HasQuery` for `ApiToken`
Turbo87 Oct 17, 2025
b4f4692
database/models: Use `HasQuery` for `User`
Turbo87 Oct 17, 2025
7da1925
database/models: Use `HasQuery` for `Email`
Turbo87 Oct 17, 2025
4d3fd01
database/models: Use `HasQuery` for `CrateOwnerInvitation`
Turbo87 Oct 17, 2025
bb6ad15
database/models: Use `HasQuery` for `VersionOwnerAction`
Turbo87 Oct 17, 2025
8674944
database/models: Use `HasQuery` for `Dependency`
Turbo87 Oct 17, 2025
2469ec9
database/models: Use `HasQuery` for `Category`
Turbo87 Oct 17, 2025
66b9e4b
database/models: Use `HasQuery` for `Version`
Turbo87 Oct 17, 2025
1af702b
database/models: Use `HasQuery` for `CrateName`
Turbo87 Oct 17, 2025
75a55d9
database/models: Use `HasQuery` for `Version`
Turbo87 Oct 17, 2025
c30b1e6
database/models: Use `HasQuery` for default versions
Turbo87 Oct 17, 2025
9b7e58f
database/models: Use `HasQuery` for `Crate`
Turbo87 Oct 17, 2025
67ecdf3
controllers/summary: Use `HasQuery` for `Record`
Turbo87 Oct 17, 2025
c545078
controllers/admin: Use `HasQuery` for `DatabaseCrateInfo`
Turbo87 Oct 17, 2025
2d18638
jobs/storage: Use `HasQuery` for `BackgroundJob`
Turbo87 Oct 17, 2025
80c05a6
jobs/send_publish_notifications: Use `HasQuery` for `PublishDetails`
Turbo87 Oct 17, 2025
21ddf71
jobs/generate_og_image: Use `HasQuery` for `QueryRow`
Turbo87 Oct 17, 2025
8b9c76b
jobs/rss: Use `HasQuery` for `VersionUpdate`
Turbo87 Oct 17, 2025
2538bbb
jobs/rss: Use `HasQuery` for `NewCrate`
Turbo87 Oct 17, 2025
dca75b4
jobs/rss: Use `HasQuery` for `VersionUpdate`
Turbo87 Oct 17, 2025
b960683
crates-admin: Use `HasQuery` for `CrateInfo`
Turbo87 Oct 17, 2025
a787c18
rate_limiter: Use `HasQuery` for `Bucket`
Turbo87 Oct 17, 2025
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
8 changes: 2 additions & 6 deletions crates/crates_io_database/src/models/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ impl From<VersionAction> for String {
}
}

#[derive(Debug, Clone, Copy, Queryable, Identifiable, Selectable, Associations)]
#[derive(Debug, Clone, Copy, HasQuery, Identifiable, Associations)]
#[diesel(
table_name = version_owner_actions,
check_for_backend(diesel::pg::Pg),
belongs_to(Version),
belongs_to(User, foreign_key = user_id),
belongs_to(ApiToken, foreign_key = api_token_id),
Expand All @@ -54,10 +53,7 @@ pub struct VersionOwnerAction {

impl VersionOwnerAction {
pub async fn all(conn: &mut AsyncPgConnection) -> QueryResult<Vec<Self>> {
version_owner_actions::table
.select(Self::as_select())
.load(conn)
.await
Self::query().load(conn).await
}

pub fn by_version<'a>(
Expand Down
7 changes: 3 additions & 4 deletions crates/crates_io_database/src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use futures_util::FutureExt;
use futures_util::future::BoxFuture;

#[derive(Clone, Identifiable, Queryable, QueryableByName, Debug, Selectable)]
#[diesel(table_name = categories, check_for_backend(diesel::pg::Pg))]
#[derive(Clone, Identifiable, HasQuery, QueryableByName, Debug)]
#[diesel(table_name = categories)]
pub struct Category {
pub id: i32,
pub category: String,
Expand Down Expand Up @@ -53,9 +53,8 @@ impl Category {
) -> QueryResult<Vec<String>> {
conn.transaction(|conn| {
async move {
let categories: Vec<Category> = categories::table
let categories: Vec<Category> = Category::query()
.filter(categories::slug.eq_any(slugs))
.select(Category::as_select())
.load(conn)
.await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::schema::cloudfront_invalidation_queue;
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};

#[derive(Debug, Identifiable, Queryable, QueryableByName, Selectable)]
#[diesel(table_name = cloudfront_invalidation_queue, check_for_backend(diesel::pg::Pg))]
#[derive(Debug, Identifiable, HasQuery, QueryableByName)]
#[diesel(table_name = cloudfront_invalidation_queue)]
pub struct CloudFrontInvalidationQueueItem {
pub id: i64,
pub path: String,
Expand Down Expand Up @@ -36,10 +36,9 @@ impl CloudFrontInvalidationQueueItem {
limit: i64,
) -> QueryResult<Vec<CloudFrontInvalidationQueueItem>> {
// Fetch the oldest entries up to the limit
cloudfront_invalidation_queue::table
Self::query()
.order(cloudfront_invalidation_queue::created_at.asc())
.limit(limit)
.select(Self::as_select())
.load(conn)
.await
}
Expand Down
20 changes: 7 additions & 13 deletions crates/crates_io_database/src/models/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use secrecy::SecretString;

use crate::models::{CrateOwner, User};
use crate::schema::{crate_owner_invitations, crates, users};
use crate::schema::{crate_owner_invitations, crates};

#[derive(Debug)]
pub enum NewCrateOwnerInvitationOutcome {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl NewCrateOwnerInvitation {
}

/// The model representing a row in the `crate_owner_invitations` database table.
#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
#[derive(Clone, Debug, Identifiable, HasQuery)]
#[diesel(primary_key(invited_user_id, crate_id))]
pub struct CrateOwnerInvitation {
pub invited_user_id: i32,
Expand All @@ -76,18 +76,16 @@ impl CrateOwnerInvitation {
crate_id: i32,
conn: &mut AsyncPgConnection,
) -> QueryResult<Self> {
crate_owner_invitations::table
CrateOwnerInvitation::query()
.find((user_id, crate_id))
.select(CrateOwnerInvitation::as_select())
.first::<Self>(conn)
.first(conn)
.await
}

pub async fn find_by_token(token: &str, conn: &mut AsyncPgConnection) -> QueryResult<Self> {
crate_owner_invitations::table
CrateOwnerInvitation::query()
.filter(crate_owner_invitations::token.eq(token))
.select(CrateOwnerInvitation::as_select())
.first::<Self>(conn)
.first(conn)
.await
}

Expand All @@ -106,11 +104,7 @@ impl CrateOwnerInvitation {
}

// Get the user and check if they have a verified email
let user: User = users::table
.find(self.invited_user_id)
.select(User::as_select())
.first(conn)
.await?;
let user = User::query().find(self.invited_user_id).first(conn).await?;

let verified_email = user.verified_email(conn).await?;
if verified_email.is_none() {
Expand Down
8 changes: 3 additions & 5 deletions crates/crates_io_database/src/models/default_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use tracing::{debug, instrument, warn};
/// It implements [Ord] in a way that sorts versions by the criteria specified
/// in the [update_default_version] function documentation. The default version
/// will be the "maximum" element in a sorted list of versions.
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Selectable)]
#[derive(Clone, Debug, PartialEq, Eq, HasQuery)]
#[diesel(table_name = versions)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Version {
pub id: i32,
#[diesel(deserialize_as = SemverVersion)]
Expand Down Expand Up @@ -123,10 +122,9 @@ async fn calculate_default_version(
use diesel::result::Error::NotFound;

debug!("Loading all versions for the crate…");
let versions = versions::table
let versions = Version::query()
.filter(versions::crate_id.eq(crate_id))
.select(Version::as_returning())
.load::<Version>(conn)
.load(conn)
.await?;

debug!("Found {} versions", versions.len());
Expand Down
3 changes: 1 addition & 2 deletions crates/crates_io_database/src/models/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crates_io_index::DependencyKind as IndexDependencyKind;
use diesel::prelude::*;
use diesel::sql_types::{BigInt, Text};

#[derive(Identifiable, Associations, Debug, Queryable, QueryableByName, Selectable)]
#[derive(Identifiable, Associations, Debug, HasQuery, QueryableByName)]
#[diesel(
table_name = dependencies,
check_for_backend(diesel::pg::Pg),
belongs_to(Version),
belongs_to(Crate),
)]
Expand Down
4 changes: 2 additions & 2 deletions crates/crates_io_database/src/models/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chrono::NaiveDate;
use crates_io_diesel_helpers::SemverVersion;
use diesel::prelude::*;

#[derive(Queryable, Identifiable, Selectable, Associations, Debug, Clone, Copy)]
#[derive(HasQuery, Identifiable, Associations, Debug, Clone, Copy)]
#[diesel(
primary_key(version_id, date),
belongs_to(FullVersion, foreign_key=version_id),
Expand All @@ -21,7 +21,7 @@ pub struct VersionDownload {
/// This struct is used to load all versions of a crate from the database,
/// without loading the additional data that is unnecessary for download version resolution.
///
#[derive(Queryable, Selectable, Identifiable)]
#[derive(HasQuery, Identifiable)]
#[diesel(table_name = versions)]
pub struct Version {
pub id: i32,
Expand Down
2 changes: 1 addition & 1 deletion crates/crates_io_database/src/models/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use secrecy::SecretString;
use crate::models::User;
use crate::schema::emails;

#[derive(Debug, Queryable, Identifiable, Selectable, Associations)]
#[derive(Debug, HasQuery, Identifiable, Associations)]
#[diesel(belongs_to(User))]
pub struct Email {
pub id: i32,
Expand Down
8 changes: 3 additions & 5 deletions crates/crates_io_database/src/models/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::models::Crate;
use crate::schema::*;
use crates_io_diesel_helpers::lower;

#[derive(Clone, Identifiable, Queryable, Debug, Selectable)]
#[derive(Clone, Identifiable, HasQuery, Debug)]
pub struct Keyword {
pub id: i32,
pub keyword: String,
Expand All @@ -30,9 +30,8 @@ pub struct CrateKeyword {

impl Keyword {
pub async fn find_by_keyword(conn: &mut AsyncPgConnection, name: &str) -> QueryResult<Keyword> {
keywords::table
Keyword::query()
.filter(keywords::keyword.eq(lower(name)))
.select(Keyword::as_select())
.first(conn)
.await
}
Expand All @@ -54,9 +53,8 @@ impl Keyword {
.execute(conn)
.await?;

keywords::table
Keyword::query()
.filter(keywords::keyword.eq_any(&lowercase_names))
.select(Keyword::as_select())
.load(conn)
.await
}
Expand Down
8 changes: 4 additions & 4 deletions crates/crates_io_database/src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use tracing::instrument;

use super::Team;

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = crates, check_for_backend(diesel::pg::Pg))]
#[derive(Debug, Clone, HasQuery)]
#[diesel(table_name = crates)]
pub struct CrateName {
pub name: String,
}

#[derive(Debug, Clone, Queryable, Identifiable, AsChangeset, Selectable, Serialize)]
#[diesel(table_name = crates, check_for_backend(diesel::pg::Pg))]
#[derive(Debug, Clone, Identifiable, AsChangeset, HasQuery, Serialize)]
#[diesel(table_name = crates)]
pub struct Crate {
pub id: i32,
pub name: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/crates_io_database/src/models/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::schema::{crate_owners, teams};

/// For now, just a GitHub Team. Can be upgraded to other teams
/// later if desirable.
#[derive(Queryable, Identifiable, serde::Serialize, serde::Deserialize, Debug, Selectable)]
#[derive(HasQuery, Identifiable, serde::Serialize, serde::Deserialize, Debug)]
pub struct Team {
/// Unique table id
pub id: i32,
Expand Down
4 changes: 1 addition & 3 deletions crates/crates_io_database/src/models/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ impl NewApiToken {
}

/// The model representing a row in the `api_tokens` database table.
#[derive(
Debug, Identifiable, Queryable, Selectable, Associations, serde::Serialize, utoipa::ToSchema,
)]
#[derive(Debug, Identifiable, HasQuery, Associations, serde::Serialize, utoipa::ToSchema)]
#[diesel(belongs_to(User))]
pub struct ApiToken {
/// An opaque unique identifier for the token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use serde::Serialize;

#[derive(Debug, Identifiable, Queryable, Selectable, Serialize)]
#[diesel(table_name = trustpub_configs_github, check_for_backend(diesel::pg::Pg))]
#[derive(Debug, Identifiable, HasQuery, Serialize)]
#[diesel(table_name = trustpub_configs_github)]
pub struct GitHubConfig {
pub id: i32,
pub created_at: DateTime<Utc>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use serde::Serialize;

#[derive(Debug, Identifiable, Queryable, Selectable, Serialize)]
#[diesel(table_name = trustpub_configs_gitlab, check_for_backend(diesel::pg::Pg))]
#[derive(Debug, Identifiable, HasQuery, Serialize)]
#[diesel(table_name = trustpub_configs_gitlab)]
pub struct GitLabConfig {
pub id: i32,
pub created_at: DateTime<Utc>,
Expand Down Expand Up @@ -71,10 +71,9 @@ mod tests {
let inserted_config = new_config.insert(&mut conn).await.unwrap();

// Retrieve the config
let retrieved_config = trustpub_configs_gitlab::table
let retrieved_config = GitLabConfig::query()
.filter(trustpub_configs_gitlab::id.eq(inserted_config.id))
.select(GitLabConfig::as_select())
.first::<GitLabConfig>(&mut conn)
.first(&mut conn)
.await
.unwrap();

Expand Down
11 changes: 3 additions & 8 deletions crates/crates_io_database/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::schema::{crate_owners, emails, users};
use crates_io_diesel_helpers::lower;

/// The model representing a row in the `users` database table.
#[derive(Clone, Debug, Queryable, Identifiable, Selectable, Serialize)]
#[derive(Clone, Debug, HasQuery, Identifiable, Serialize)]
pub struct User {
pub id: i32,
pub name: Option<String>,
Expand All @@ -29,18 +29,13 @@ pub struct User {

impl User {
pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> QueryResult<User> {
users::table
.find(id)
.select(User::as_select())
.first(conn)
.await
User::query().find(id).first(conn).await
}

pub async fn find_by_login(conn: &mut AsyncPgConnection, login: &str) -> QueryResult<User> {
users::table
User::query()
.filter(lower(users::gh_login).eq(login.to_lowercase()))
.filter(users::gh_id.ne(-1))
.select(User::as_select())
.order(users::gh_id.desc())
.first(conn)
.await
Expand Down
11 changes: 3 additions & 8 deletions crates/crates_io_database/src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl};
use serde::Deserialize;

use crate::models::{Crate, TrustpubData, User};
use crate::schema::{readme_renderings, users, versions};
use crate::schema::{readme_renderings, versions};

#[derive(Clone, Identifiable, Associations, Debug, Queryable, Selectable)]
#[derive(Clone, Identifiable, Associations, Debug, HasQuery)]
#[diesel(belongs_to(Crate), belongs_to(crate::models::download::Version, foreign_key=id))]
pub struct Version {
pub id: i32,
Expand Down Expand Up @@ -59,12 +59,7 @@ impl Version {
/// Not for use when you have a group of versions you need the publishers for.
pub async fn published_by(&self, conn: &mut AsyncPgConnection) -> QueryResult<Option<User>> {
match self.published_by {
Some(pb) => users::table
.find(pb)
.select(User::as_select())
.first(conn)
.await
.optional(),
Some(pb) => User::query().find(pb).first(conn).await.optional(),
None => Ok(None),
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/crates_io_worker/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::sql_types::{Bool, Integer, Interval};
use diesel::{delete, update};
use diesel_async::{AsyncPgConnection, RunQueryDsl};

#[derive(Queryable, Selectable, Identifiable, Debug, Clone)]
#[derive(HasQuery, Identifiable, Debug, Clone)]
pub(super) struct BackgroundJob {
pub(super) id: i64,
pub(super) job_type: String,
Expand All @@ -30,14 +30,13 @@ pub(super) async fn find_next_unlocked_job(
conn: &mut AsyncPgConnection,
job_types: &[String],
) -> QueryResult<BackgroundJob> {
background_jobs::table
.select(BackgroundJob::as_select())
BackgroundJob::query()
.filter(background_jobs::job_type.eq_any(job_types))
.filter(retriable())
.order((background_jobs::priority.desc(), background_jobs::id))
.for_update()
.skip_locked()
.first::<BackgroundJob>(conn)
.first(conn)
.await
}

Expand Down
Loading