Skip to content

Commit

Permalink
feat(db): Improve error message for duplicate repository creation (#1423
Browse files Browse the repository at this point in the history
)

* feat(db): Improve error message for duplicate repository creation

* Add test to verify error message
  • Loading branch information
boxbeam authored Feb 9, 2024
1 parent 23661f7 commit 2542fde
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ee/tabby-db/src/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ impl DbConn {
git_url
)
.execute(&self.pool)
.await?;
.await;

Ok(res.last_insert_rowid() as i32)
match res {
Ok(output) => Ok(output.last_insert_rowid() as i32),
Err(sqlx::Error::Database(db_err)) if db_err.is_unique_violation() => Err(anyhow!(
"A repository with the same name or URL already exists",
)),
Err(e) => Err(e.into()),
}
}

pub async fn update_repository(&self, id: i32, name: String, git_url: String) -> Result<()> {
Expand Down
33 changes: 33 additions & 0 deletions ee/tabby-webserver/src/service/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,36 @@ impl RepositoryService for DbConn {
.map(|_| true)
}
}

#[cfg(test)]
mod tests {
use tabby_db::DbConn;

use super::*;

#[tokio::test]
pub async fn test_duplicate_repository_error() {
let db = DbConn::new_in_memory().await.unwrap();

RepositoryService::create_repository(
&db,
"example".into(),
"https://github.com/example/example".into(),
)
.await
.unwrap();

let err = RepositoryService::create_repository(
&db,
"example".into(),
"https://github.com/example/example".into(),
)
.await
.unwrap_err();

assert_eq!(
err.to_string(),
"A repository with the same name or URL already exists"
);
}
}

0 comments on commit 2542fde

Please sign in to comment.