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 #2534, although `id` was ambiguous thus using
`count(*)`.
  • Loading branch information
pflanze committed Jun 25, 2024
1 parent 1018223 commit 8fc46ab
Showing 1 changed file with 23 additions and 5 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

0 comments on commit 8fc46ab

Please sign in to comment.