Skip to content

Commit

Permalink
start migration more web endpoints to sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Nov 15, 2023
1 parent 4af81bf commit 4a1f478
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 174 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/db/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use postgres_types::{FromSql, ToSql};
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, FromSql, ToSql)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, FromSql, ToSql, sqlx::Type)]
#[postgres(name = "feature")]
pub struct Feature {
pub(crate) name: String,
Expand All @@ -17,3 +17,9 @@ impl Feature {
self.name.starts_with('_')
}
}

impl sqlx::postgres::PgHasArrayType for Feature {
fn array_type_info() -> sqlx::postgres::PgTypeInfo {
sqlx::postgres::PgTypeInfo::with_name("_feature")
}
}
67 changes: 29 additions & 38 deletions src/web/build_details.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
db::Pool,
impl_axum_webpage,
utils::spawn_blocking,
web::{
error::{AxumNope, AxumResult},
file::File,
Expand Down Expand Up @@ -47,52 +46,44 @@ pub(crate) async fn build_details_handler(
) -> AxumResult<impl IntoResponse> {
let id: i32 = id.parse().map_err(|_| AxumNope::BuildNotFound)?;

let (row, output, metadata) = spawn_blocking(move || {
let mut conn = pool.get()?;
let row = conn
.query_opt(
"SELECT
builds.rustc_version,
builds.docsrs_version,
builds.build_status,
builds.build_time,
builds.output,
releases.default_target
FROM builds
INNER JOIN releases ON releases.id = builds.rid
INNER JOIN crates ON releases.crate_id = crates.id
WHERE builds.id = $1 AND crates.name = $2 AND releases.version = $3",
&[&id, &name, &version],
)?
.ok_or(AxumNope::BuildNotFound)?;

let output: Option<String> = row.get("output");

Ok((
row,
output,
MetaData::from_crate(&mut conn, &name, &version, &version)?,
))
})
.await?;

let output = if let Some(output) = output {
let mut conn = pool.get_async().await?;

let row = sqlx::query!(
"SELECT
builds.rustc_version,
builds.docsrs_version,
builds.build_status,
builds.build_time,
builds.output,
releases.default_target
FROM builds
INNER JOIN releases ON releases.id = builds.rid
INNER JOIN crates ON releases.crate_id = crates.id
WHERE builds.id = $1 AND crates.name = $2 AND releases.version = $3",
id,
name,
version,
)
.fetch_optional(&mut *conn)
.await?
.ok_or(AxumNope::BuildNotFound)?;

let output = if let Some(output) = row.output {
output
} else {
let target: String = row.get("default_target");
let path = format!("build-logs/{id}/{target}.txt");
let path = format!("build-logs/{id}/{}.txt", row.default_target);
let file = File::from_path(&storage, &path, &config).await?;
String::from_utf8(file.0.content).context("non utf8")?
};

Ok(BuildDetailsPage {
metadata,
metadata: MetaData::from_crate(&mut conn, &name, &version, &version).await?,
build_details: BuildDetails {
id,
rustc_version: row.get("rustc_version"),
docsrs_version: row.get("docsrs_version"),
build_status: row.get("build_status"),
build_time: row.get("build_time"),
rustc_version: row.rustc_version,
docsrs_version: row.docsrs_version,
build_status: row.build_status,
build_time: row.build_time,
output,
},
use_direct_platform_links: true,
Expand Down
Loading

0 comments on commit 4a1f478

Please sign in to comment.