Skip to content

Commit

Permalink
refactor add_package_into_database into pure finish_release for u…
Browse files Browse the repository at this point in the history
…pdating
  • Loading branch information
syphar committed Jan 10, 2025
1 parent 4c38307 commit b70bbe1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 130 deletions.

This file was deleted.

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

91 changes: 40 additions & 51 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
utils::{rustc_version::parse_rustc_date, MetadataPackage},
web::crate_details::{latest_release, releases_for_crate},
};
use anyhow::Context;
use anyhow::{anyhow, Context};
use derive_more::Display;
use futures_util::stream::TryStreamExt;
use serde::Serialize;
Expand Down Expand Up @@ -41,8 +41,10 @@ pub struct BuildId(pub i32);
/// not the files generated by rustdoc.
#[allow(clippy::too_many_arguments)]
#[instrument(skip(conn, compression_algorithms))]
pub(crate) async fn add_package_into_database(
pub(crate) async fn finish_release(
conn: &mut sqlx::PgConnection,
crate_id: CrateId,
release_id: ReleaseId,
metadata_pkg: &MetadataPackage,
source_dir: &Path,
default_target: &str,
Expand All @@ -55,59 +57,42 @@ pub(crate) async fn add_package_into_database(
repository_id: Option<i32>,
archive_storage: bool,
source_size: u64,
) -> Result<ReleaseId> {
debug!("Adding package into database");
let crate_id = initialize_crate(conn, &metadata_pkg.name).await?;
) -> Result<()> {
debug!("updating release data");
let dependencies = convert_dependencies(metadata_pkg);
let rustdoc = get_rustdoc(metadata_pkg, source_dir).unwrap_or(None);
let readme = get_readme(metadata_pkg, source_dir).unwrap_or(None);
let features = get_features(metadata_pkg);
let is_library = metadata_pkg.is_library();

let release_id = sqlx::query_scalar!(
r#"INSERT INTO releases (
crate_id, version, release_time,
dependencies, target_name, yanked,
rustdoc_status, test_status, license, repository_url,
homepage_url, description, description_long, readme,
keywords, have_examples, downloads, files,
doc_targets, is_library,
documentation_url, default_target, features,
repository_id, archive_storage, source_size
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9,
$10, $11, $12, $13, $14, $15, $16, $17, $18,
$19, $20, $21, $22, $23, $24, $25, $26
)
ON CONFLICT (crate_id, version) DO UPDATE
SET release_time = $3,
dependencies = $4,
target_name = $5,
yanked = $6,
rustdoc_status = $7,
test_status = $8,
license = $9,
repository_url = $10,
homepage_url = $11,
description = $12,
description_long = $13,
readme = $14,
keywords = $15,
have_examples = $16,
downloads = $17,
files = $18,
doc_targets = $19,
is_library = $20,
documentation_url = $21,
default_target = $22,
features = $23,
repository_id = $24,
archive_storage = $25,
source_size = $26
RETURNING id as "id: ReleaseId" "#,
crate_id.0,
&metadata_pkg.version,
let result = sqlx::query!(
r#"UPDATE releases
SET release_time = $2,
dependencies = $3,
target_name = $4,
yanked = $5,
rustdoc_status = $6,
test_status = $7,
license = $8,
repository_url = $9,
homepage_url = $10,
description = $11,
description_long = $12,
readme = $13,
keywords = $14,
have_examples = $15,
downloads = $16,
files = $17,
doc_targets = $18,
is_library = $19,
documentation_url = $20,
default_target = $21,
features = $22,
repository_id = $23,
archive_storage = $24,
source_size = $25
WHERE id = $1"#,
release_id.0,
registry_data.release_time,
serde_json::to_value(dependencies)?,
metadata_pkg.package_name(),
Expand All @@ -133,9 +118,13 @@ pub(crate) async fn add_package_into_database(
archive_storage,
source_size as i64,
)
.fetch_one(&mut *conn)
.execute(&mut *conn)
.await?;

if result.rows_affected() < 1 {
return Err(anyhow!("Failed to update release"));
}

add_keywords_into_database(conn, metadata_pkg, release_id).await?;
add_compression_into_database(conn, compression_algorithms.into_iter(), release_id).await?;

Expand All @@ -145,7 +134,7 @@ pub(crate) async fn add_package_into_database(

update_build_status(conn, release_id).await?;

Ok(release_id)
Ok(())
}

pub async fn update_latest_version_id(
Expand Down
2 changes: 1 addition & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sqlx::migrate::{Migrate, Migrator};

pub use self::add_package::update_latest_version_id;
pub(crate) use self::add_package::{
add_doc_coverage, add_package_into_database, finish_build, initialize_build, initialize_crate,
add_doc_coverage, finish_build, finish_release, initialize_build, initialize_crate,
initialize_release, update_build_with_error,
};
pub use self::{
Expand Down
19 changes: 12 additions & 7 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::db::{
add_doc_coverage, add_package_into_database, add_path_into_remote_archive, finish_build,
initialize_build, initialize_crate, initialize_release, types::BuildStatus,
update_build_with_error, update_crate_data_in_database, Pool,
add_doc_coverage, add_path_into_remote_archive, finish_build, finish_release, initialize_build,
initialize_crate, initialize_release, types::BuildStatus, update_build_with_error,
update_crate_data_in_database, Pool,
};
use crate::db::{
file::{add_path_into_database, file_list_to_json},
BuildId,
};
use crate::db::{CrateId, ReleaseId};
use crate::docbuilder::Limits;
use crate::error::Result;
use crate::repositories::RepositoryStatsUpdater;
Expand Down Expand Up @@ -360,15 +361,15 @@ impl RustwideBuilder {
version: &str,
kind: PackageKind<'_>,
) -> Result<BuildPackageSummary> {
let build_id = self.runtime.block_on(async {
let (crate_id, release_id, build_id) = self.runtime.block_on(async {
let mut conn = self.db.get_async().await?;
let crate_id = initialize_crate(&mut conn, name).await?;
let release_id = initialize_release(&mut conn, crate_id, version).await?;
let build_id = initialize_build(&mut conn, release_id).await?;
Ok::<BuildId, Error>(build_id)
Ok::<_, Error>((crate_id, release_id, build_id))
})?;

match self.build_package_inner(name, version, kind, build_id) {
match self.build_package_inner(name, version, kind, crate_id, release_id, build_id) {
Ok(successful) => Ok(BuildPackageSummary {
successful,
should_reattempt: false,
Expand All @@ -395,6 +396,8 @@ impl RustwideBuilder {
name: &str,
version: &str,
kind: PackageKind<'_>,
crate_id: CrateId,
release_id: ReleaseId,
build_id: BuildId,
) -> Result<bool> {
info!("building package {} {}", name, version);
Expand Down Expand Up @@ -610,8 +613,10 @@ impl RustwideBuilder {

let mut async_conn = self.runtime.block_on(self.db.get_async())?;

let release_id = self.runtime.block_on(add_package_into_database(
self.runtime.block_on(finish_release(
&mut async_conn,
crate_id,
release_id,
cargo_metadata,
&build.host_source_dir(),
&res.target,
Expand Down
6 changes: 5 additions & 1 deletion src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,12 @@ impl<'a> FakeRelease<'a> {
// non-linux platforms.
let default_target = self.default_target.unwrap_or("x86_64-unknown-linux-gnu");
let mut async_conn = db.async_conn().await;
let release_id = crate::db::add_package_into_database(
let crate_id = initialize_crate(&mut async_conn, &package.name).await?;
let release_id = initialize_release(&mut async_conn, crate_id, &package.version).await?;
crate::db::finish_release(
&mut async_conn,
crate_id,
release_id,
&package,
crate_dir,
default_target,
Expand Down

0 comments on commit b70bbe1

Please sign in to comment.