Skip to content

Commit

Permalink
web/builds: add crate_version_exists
Browse files Browse the repository at this point in the history
As per suggestion on rust-lang#2534, although `id` was ambiguous thus using
`count(*)`.
  • Loading branch information
pflanze committed Jun 27, 2024
1 parent 5d8d1ad commit 449ec34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
impl_axum_webpage,
utils::spawn_blocking,
web::{
crate_details::CrateDetails,
error::AxumResult,
extractors::{DbConnection, Path},
match_version, MetaData, ReqVersion,
Expand Down Expand Up @@ -123,15 +122,34 @@ pub(crate) async fn build_list_json_handler(
.into_response())
}

async fn build_trigger_check(
async fn crate_version_exists(
mut conn: DbConnection,
name: &String,
version: &Version,
) -> Result<bool, anyhow::Error> {
let row = sqlx::query!(
r#"
SELECT COUNT(*) as "count!"
FROM releases
INNER JOIN crates ON crates.id = releases.crate_id
WHERE crates.name = $1 AND releases.version = $2"#,
name,
version.to_string(),
)
.fetch_one(&mut *conn)
.await?;
Ok(row.count > 0)
}

async fn build_trigger_check(
conn: DbConnection,
name: &String,
version: &Version,
build_queue: &Arc<BuildQueue>,
) -> AxumResult<impl IntoResponse> {
let _ = CrateDetails::new(&mut *conn, &name, &version, None, vec![])
.await?
.ok_or(AxumNope::VersionNotFound)?;
if !crate_version_exists(conn, name, version).await? {
return Err(AxumNope::VersionNotFound);
}

let crate_version_is_in_queue = spawn_blocking({
let name = name.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl CrateDetails {
.unwrap())
}

pub(crate) async fn new(
async fn new(
conn: &mut sqlx::PgConnection,
name: &str,
version: &Version,
Expand Down

0 comments on commit 449ec34

Please sign in to comment.