Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start migration more web endpoints to sqlx #2311

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

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")
}
}
69 changes: 29 additions & 40 deletions src/web/build_details.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
db::Pool,
impl_axum_webpage,
utils::spawn_blocking,
web::{
error::{AxumNope, AxumResult},
extractors::DbConnection,
file::File,
MetaData,
},
Expand Down Expand Up @@ -41,58 +40,48 @@ impl_axum_webpage! {

pub(crate) async fn build_details_handler(
Path((name, version, id)): Path<(String, String, String)>,
Extension(pool): Extension<Pool>,
mut conn: DbConnection,
Extension(config): Extension<Arc<Config>>,
Extension(storage): Extension<Arc<AsyncStorage>>,
) -> 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 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
Loading