Skip to content
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
32 changes: 28 additions & 4 deletions app/src/domain/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ fn from_row(m: SnapshotPage, texts: &HashMap<String, String>) -> Page {
/// # Errors
/// On a database error, or when a text snapshot's file is missing.
pub async fn of_many(store: Option<&BlobStore>, list: &[&Snapshot]) -> laravel::Result<HashMap<i64, Vec<Page>>> {
read(store, list, true).await
}

/// Each snapshot's pages ([`of_many`]) without any text: their positions, states and images, for a list (a build's
/// page counts and thumbnails) that mustn't hold every page's text of a large build in memory. Texts read as `None`.
///
/// # Errors
/// On a database error.
pub async fn of_many_without_text(list: &[&Snapshot]) -> laravel::Result<HashMap<i64, Vec<Page>>> {
read(None, list, false).await
}

async fn read(
store: Option<&BlobStore>,
list: &[&Snapshot],
stored_text: bool,
) -> laravel::Result<HashMap<i64, Vec<Page>>> {
let own: Vec<i64> = list.iter().filter(|s| !s.sourced).map(|s| s.id).collect();
let mut rows: Vec<SnapshotPage> = Vec::new();
for chunk in own.chunks(10_000) {
Expand All @@ -133,11 +150,18 @@ pub async fn of_many(store: Option<&BlobStore>, list: &[&Snapshot]) -> laravel::
.await?;
rows.extend(found);
}
let shas: Vec<String> =
rows.iter().flat_map(|r| [r.head_text_sha.clone(), r.base_text_sha.clone()]).flatten().collect();
let stored_texts = texts::load(&shas).await?;
let stored_texts = if stored_text {
let shas: Vec<String> =
rows.iter().flat_map(|r| [r.head_text_sha.clone(), r.base_text_sha.clone()]).flatten().collect();
texts::load(&shas).await?
} else {
HashMap::new()
};
let mut out: HashMap<i64, Vec<Page>> = HashMap::new();
for r in rows {
for mut r in rows {
if !stored_text {
(r.head_text_sha, r.base_text_sha) = (None, None);
}
out.entry(r.snapshot_id).or_default().push(from_row(r, &stored_texts));
}

Expand Down
38 changes: 32 additions & 6 deletions app/src/domain/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,53 @@ pub struct SettingsChange {
pub ignore_rules: Option<Vec<ScopedIgnoreRule>>,
}

/// Why a project wasn't made.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Refused {
/// The slug isn't lowercase letters, digits and dashes (or is `billing`): the message says so.
InvalidSlug(&'static str),
/// The organization already has a project with the slug, found first or by the unique index when two requests
/// raced. Each caller words it as today's server did.
Taken,
}

/// The message for a slug that isn't valid.
pub const INVALID_SLUG: &str = "project slugs use lowercase letters, digits and dashes, and can't be billing";

/// Whether a database error is a unique violation (PostgreSQL's 23505): a row another request inserted first.
#[must_use]
pub fn is_unique_violation(e: &laravel::Error) -> bool {
let text = format!("{e:#}");
text.contains("23505") || text.contains("duplicate key value violates unique constraint")
}

/// Make a project in an organization, with the default settings.
///
/// # Errors
/// `Ok(Err(message))` for a slug that isn't valid or is taken; `Err` on a database error.
pub async fn create(organization_id: i64, slug: &str, name: &str) -> laravel::Result<Result<Project, String>> {
/// `Ok(Err(_))` for a slug that isn't valid or is taken; `Err` on a database error.
pub async fn create(organization_id: i64, slug: &str, name: &str) -> laravel::Result<Result<Project, Refused>> {
if !valid_slug(slug) {
return Ok(Err("project slugs use lowercase letters, digits and dashes, and can't be billing".into()));
return Ok(Err(Refused::InvalidSlug(INVALID_SLUG)));
}
let taken =
Project::query().filter(Project::organization_id.eq(organization_id) & Project::slug.eq(slug)).exists().await?;
if taken {
return Ok(Err(format!("the organization already has a project called {slug}")));
return Ok(Err(Refused::Taken));
}
let name = name.trim();
Ok(Ok(laravel::model::create!(Project {
match laravel::model::create!(Project {
pid: laravel::str::uuid7(),
organization_id: organization_id,
slug: slug,
name: name,
})
.await?))
.await
{
Ok(project) => Ok(Ok(project)),
// Another request made the same slug between the check and the insert.
Err(e) if is_unique_violation(&e) => Ok(Err(Refused::Taken)),
Err(e) => Err(e),
}
}

/// The repository its newest CI build named (`environment_info.repository_url`), for suggesting a link.
Expand Down
1 change: 1 addition & 0 deletions app/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod gallery;
pub mod hosts;
pub mod renderer;
pub mod status;
pub mod ui;

/// Whether a request failed because every pooled database connection was in use for `DB_ACQUIRE_TIMEOUT`: the
/// answer is a 503 with Retry-After, so clients (the CLI retries) back off instead of piling on.
Expand Down
Loading
Loading