Skip to content

Commit

Permalink
store date of nightly version in separate field
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Oct 18, 2024
1 parent 1943e68 commit 0c4dfcc
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
4 changes: 4 additions & 0 deletions migrations/20241018052241_builds-rustc-nightly-date.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP INDEX builds_nightly_date_idx;

ALTER TABLE builds
DROP COLUMN rustc_nightly_date;
4 changes: 4 additions & 0 deletions migrations/20241018052241_builds-rustc-nightly-date.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE builds
ADD COLUMN rustc_nightly_date DATE;

CREATE INDEX builds_nightly_date_idx ON builds USING btree (rustc_nightly_date);
74 changes: 69 additions & 5 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
error::Result,
registry_api::{CrateData, CrateOwner, ReleaseData},
storage::CompressionAlgorithm,
utils::MetadataPackage,
utils::{rustc_version::parse_rustc_date, MetadataPackage},
web::crate_details::{latest_release, releases_for_crate},
};
use anyhow::Context;
Expand All @@ -17,7 +17,7 @@ use std::{
io::{BufRead, BufReader},
path::Path,
};
use tracing::{debug, info, instrument};
use tracing::{debug, error, info, instrument};

/// Adds a package into database.
///
Expand Down Expand Up @@ -244,6 +244,17 @@ pub(crate) async fn finish_build(
debug!("updating build after finishing");
let hostname = hostname::get()?;

let rustc_date = match parse_rustc_date(rustc_version) {
Ok(date) => Some(date),
Err(err) => {
error!(
"Failed to parse date from rustc version \"{}\": {:?}",
rustc_version, err
);
None
}
};

let release_id = sqlx::query_scalar!(
"UPDATE builds
SET
Expand All @@ -252,15 +263,17 @@ pub(crate) async fn finish_build(
build_status = $3,
build_server = $4,
errors = $5,
rustc_nightly_date = $6,
build_time = NOW()
WHERE
id = $6
id = $7
RETURNING rid",
rustc_version,
docsrs_version,
build_status as BuildStatus,
hostname.to_str().unwrap_or(""),
errors,
rustc_date,
build_id,
)
.fetch_one(&mut *conn)
Expand Down Expand Up @@ -606,6 +619,7 @@ mod test {
use crate::registry_api::OwnerKind;
use crate::test::*;
use crate::utils::CargoMetadata;
use chrono::NaiveDate;
use test_case::test_case;

#[test]
Expand Down Expand Up @@ -641,7 +655,55 @@ mod test {
}

#[test]
fn test_finish_build_success() {
fn test_finish_build_success_valid_rustc_date() {
async_wrapper(|env| async move {
let mut conn = env.async_db().await.async_conn().await;
let crate_id = initialize_crate(&mut conn, "krate").await?;
let release_id = initialize_release(&mut conn, crate_id, "0.1.0").await?;
let build_id = initialize_build(&mut conn, release_id).await?;

finish_build(
&mut conn,
build_id,
"rustc 1.84.0-nightly (e7c0d2750 2024-10-15)",
"docsrs_version",
BuildStatus::Success,
None,
)
.await?;

let row = sqlx::query!(
r#"SELECT
rustc_version,
docsrs_version,
build_status as "build_status: BuildStatus",
errors,
rustc_nightly_date
FROM builds
WHERE id = $1"#,
build_id
)
.fetch_one(&mut *conn)
.await?;

assert_eq!(
row.rustc_version,
Some("rustc 1.84.0-nightly (e7c0d2750 2024-10-15)".into())
);
assert_eq!(row.docsrs_version, Some("docsrs_version".into()));
assert_eq!(row.build_status, BuildStatus::Success);
assert_eq!(
row.rustc_nightly_date,
Some(NaiveDate::from_ymd_opt(2024, 10, 15).unwrap())
);
assert!(row.errors.is_none());

Ok(())
})
}

#[test]
fn test_finish_build_success_invalid_rustc_date() {
async_wrapper(|env| async move {
let mut conn = env.async_db().await.async_conn().await;
let crate_id = initialize_crate(&mut conn, "krate").await?;
Expand All @@ -663,7 +725,8 @@ mod test {
rustc_version,
docsrs_version,
build_status as "build_status: BuildStatus",
errors
errors,
rustc_nightly_date
FROM builds
WHERE id = $1"#,
build_id
Expand All @@ -674,6 +737,7 @@ mod test {
assert_eq!(row.rustc_version, Some("rustc_version".into()));
assert_eq!(row.docsrs_version, Some("docsrs_version".into()));
assert_eq!(row.build_status, BuildStatus::Success);
assert!(row.rustc_nightly_date.is_none());
assert!(row.errors.is_none());

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod daemon;
mod html;
mod queue;
pub(crate) mod queue_builder;
mod rustc_version;
pub(crate) mod rustc_version;
use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::Serialize;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rustc_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
))
}

fn parse_rustc_date<S: AsRef<str>>(version: S) -> Result<NaiveDate> {
pub(crate) fn parse_rustc_date<S: AsRef<str>>(version: S) -> Result<NaiveDate> {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r" (\d+)-(\d+)-(\d+)\)$").unwrap());

let cap = RE
Expand Down

0 comments on commit 0c4dfcc

Please sign in to comment.