Skip to content

Commit

Permalink
More tracing, project parsing for maven, put some libraries in releas…
Browse files Browse the repository at this point in the history
…e mode. Because password verification takes really long in debug mode.
  • Loading branch information
wyatt-herkamp committed Aug 14, 2024
1 parent a1f6a1d commit 1af234b
Show file tree
Hide file tree
Showing 22 changed files with 695 additions and 181 deletions.
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ features = [
[workspace.lints.rust]
async_fn_in_trait = "allow"
deprecated = "deny"

[profile.dev.package."argon2"]
opt-level = 3
[profile.dev.package."tokio"]
opt-level = 3
[profile.dev.package."sha2"]
opt-level = 3
[profile.dev.package."tracing"]
opt-level = 3
[profile.dev.package."tracing-subscriber"]
opt-level = 3
[profile.dev.package."tracing-appender"]
opt-level = 3
[profile.dev.package."tracing-opentelemetry"]
opt-level = 3
[profile.dev.package."opentelemetry"]
opt-level = 3
[profile.dev.package."opentelemetry_sdk"]
opt-level = 3
[profile.dev.package."opentelemetry-otlp"]
opt-level = 3
37 changes: 35 additions & 2 deletions crates/core/src/database/project/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use derive_builder::Builder;
use http::version;
use serde::Serialize;
use sqlx::{postgres::PgRow, types::Json, FromRow, PgPool};
use utoipa::ToSchema;
Expand All @@ -17,21 +18,37 @@ pub trait ProjectDBType: for<'r> FromRow<'r, PgRow> + Unpin + Send + Sync {
if let Some(prefix) = prefix {
Self::columns()
.iter()
.map(|column| format!("{}.`{}`", prefix, column))
.map(|column| format!("{}.{}", prefix, column))
.collect::<Vec<String>>()
.join(", ")
} else {
Self::columns().join(", ")
}
}
async fn find_by_project_key(
project_key: &str,
repository: Uuid,
database: &PgPool,
) -> Result<Option<Self>, sqlx::Error> {
let columns = Self::format_columns(None);
let project = sqlx::query_as::<_, Self>(&format!(
"SELECT {} FROM projects WHERE repository = $1 AND LOWER(project_key) = $2",
columns
))
.bind(repository)
.bind(project_key.to_lowercase())
.fetch_optional(database)
.await?;
Ok(project)
}
async fn find_by_project_directory(
directory: &str,
repository: Uuid,
database: &PgPool,
) -> Result<Option<Self>, sqlx::Error> {
let columns = Self::format_columns(None);
let project = sqlx::query_as::<_, Self>(&format!(
"SELECT {} FROM projects WHERE `repository` = $1 AND LOWER(`storage_path`) = $2",
"SELECT {} FROM projects WHERE repository = $1 AND LOWER(storage_path) = $2",
columns
))
.bind(repository)
Expand Down Expand Up @@ -141,3 +158,19 @@ pub struct DBProjectVersion {
pub updated_at: DateTime,
pub created_at: DateTime,
}
impl DBProjectVersion {
pub async fn find_by_version_and_project(
version: &str,
project_id: Uuid,
database: &PgPool,
) -> Result<Option<Self>, sqlx::Error> {
let version = sqlx::query_as::<_, Self>(
r#"SELECT * FROM project_versions WHERE project_id = $1 AND version = $2"#,
)
.bind(project_id)
.bind(version)
.fetch_optional(database)
.await?;
Ok(version)
}
}
124 changes: 124 additions & 0 deletions crates/core/src/database/project/new.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use sqlx::{types::Json, PgPool};
use tracing::info;
use utoipa::ToSchema;
use uuid::Uuid;

use crate::repository::project::{ReleaseType, VersionData};

use super::DBProject;
#[derive(Debug, Clone, PartialEq, Eq, Builder)]
pub struct NewProject {
#[builder(default)]
pub scope: Option<String>,
/// Maven will use something like `{groupId}:{artifactId}`
/// Cargo will use the `name` field
Expand All @@ -17,19 +22,55 @@ pub struct NewProject {
/// NPM will use the `name` field
pub name: String,
/// Latest stable release
#[builder(default)]
pub latest_release: Option<String>,
/// Release is SNAPSHOT in Maven or Alpha, Beta, on any other repository type
/// This is the latest release or pre-release
#[builder(default)]
pub latest_pre_release: Option<String>,
/// A short description of the project
#[builder(default)]
pub description: Option<String>,
/// Can be empty
#[builder(default)]
pub tags: Vec<String>,
/// The repository it belongs to
pub repository: Uuid,
/// Storage Path
pub storage_path: String,
}
impl NewProject {
pub async fn insert(self, db: &sqlx::PgPool) -> Result<DBProject, sqlx::Error> {
let Self {
scope,
project_key,
name,
latest_release,
latest_pre_release,
description,
tags,
repository,
storage_path,
} = self;

let insert = sqlx::query_as::<_,DBProject>(
r#"
INSERT INTO projects (scope, project_key, name, latest_release, latest_pre_release, description, tags, repository, storage_path)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *
"#
).bind(scope)
.bind(project_key)
.bind(name)
.bind(latest_release)
.bind(latest_pre_release)
.bind(description)
.bind(tags)
.bind(repository)
.bind(storage_path)
.fetch_one(db).await?;
Ok(insert)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
pub struct NewProjectMember {
pub user_id: i32,
Expand All @@ -46,6 +87,27 @@ impl NewProjectMember {
can_manage: true,
}
}
pub async fn insert_no_return(self, db: &PgPool) -> Result<(), sqlx::Error> {
let Self {
user_id,
project_id,
can_write,
can_manage,
} = self;
sqlx::query(
r#"
INSERT INTO project_members (user_id, project_id, can_write, can_manage)
VALUES ($1, $2, $3, $4)
"#,
)
.bind(user_id)
.bind(project_id)
.bind(can_write)
.bind(can_manage)
.execute(db)
.await?;
Ok(())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Builder)]
Expand All @@ -60,7 +122,69 @@ pub struct NewVersion {
/// The publisher of the version
pub publisher: i32,
/// The version page. Such as a README
#[builder(default)]
pub version_page: Option<String>,
/// The version data. More data can be added in the future and the data can be repository dependent
pub extra: VersionData,
}
impl NewVersion {
pub async fn insert_no_return(self, db: &PgPool) -> Result<(), sqlx::Error> {
let Self {
project_id,
version,
release_type,
version_path,
publisher,
version_page,
extra,
} = self;
sqlx::query(
r#"
INSERT INTO project_versions (project_id, version, release_type, version_path, publisher, version_page, extra)
VALUES ($1, $2, $3, $4, $5, $6, $7)
"#,
)
.bind(project_id)
.bind(&version)
.bind(release_type.to_string())
.bind(version_path)
.bind(publisher)
.bind(version_page)
.bind(Json(extra))
.execute(db)
.await?;
match release_type {
ReleaseType::Stable => {
sqlx::query(
r#"
UPDATE projects
SET latest_release = $1 AND latest_pre_release = $1
WHERE id = $2
"#,
)
.bind(version)
.bind(project_id)
.execute(db)
.await?;
}
ReleaseType::Unknown => {
info!("Unknown release type for version {}", version);
}
_ => {
sqlx::query(
r#"
UPDATE projects
SET latest_pre_release = $1
WHERE id = $2
"#,
)
.bind(version)
.bind(project_id)
.execute(db)
.await?;
}
}

Ok(())
}
}
8 changes: 4 additions & 4 deletions crates/core/src/database/project/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ impl ProjectLookup {
}
if let Some(scope) = scope {
where_values.push((
format!("LOWER(`scope`) = {}", where_values.len() + 1),
format!("LOWER(scope) = {}", where_values.len() + 1),
scope.to_lowercase(),
));
}
if let Some(name) = name {
where_values.push((
format!("LOWER(`name`) = {}", where_values.len() + 1),
format!("LOWER(name) = {}", where_values.len() + 1),
name.to_lowercase(),
));
}
if let Some(storage_path) = storage_path {
where_values.push((
format!("LOWER(`storage_path`) = {}", where_values.len() + 1),
format!("LOWER(storage_path) = {}", where_values.len() + 1),
storage_path.to_lowercase(),
));
}
Expand All @@ -69,7 +69,7 @@ impl ProjectLookup {
.join(" OR ")
};
let query = format!(
r#"SELECT id, scope, project_key, name, storage_path FROM projects WHERE `repository` = $1 AND ({})"#,
r#"SELECT id, scope, project_key, name, storage_path FROM projects WHERE repository = $1 AND ({})"#,
where_clause
);
debug!(
Expand Down
Loading

0 comments on commit 1af234b

Please sign in to comment.