diff --git a/ee/tabby-db/src/repositories.rs b/ee/tabby-db/src/repositories.rs index 1acc2067cfc2..ad1ecaaa5d76 100644 --- a/ee/tabby-db/src/repositories.rs +++ b/ee/tabby-db/src/repositories.rs @@ -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<()> { diff --git a/ee/tabby-webserver/src/service/repository.rs b/ee/tabby-webserver/src/service/repository.rs index 8ced9f654e25..eca7948a1fb5 100644 --- a/ee/tabby-webserver/src/service/repository.rs +++ b/ee/tabby-webserver/src/service/repository.rs @@ -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" + ); + } +}