Skip to content

Commit

Permalink
Show uniqueness errors as warnings and ignore failed insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Feb 9, 2025
1 parent 04b3eac commit cc10ba7
Showing 1 changed file with 73 additions and 3 deletions.
76 changes: 73 additions & 3 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rust_decimal::Decimal;
use secrecy::{ExposeSecret, SecretString};
use sqlx::types::Text;
use sqlx::{SqliteConnection, SqlitePool};
use tracing::info;
use tracing::{info, warn};
use ulid::Ulid;

#[derive(Debug, Clone, sqlx::FromRow)]
Expand Down Expand Up @@ -119,7 +119,9 @@ impl Member {

Self::delete_all(&mut transaction).await?;
for member in members {
member.insert(&mut transaction).await?;
if let Err(error) = member.insert(&mut transaction).await {
warn!("Failed to insert member: {error}");
}
}

transaction.commit().await
Expand Down Expand Up @@ -233,7 +235,9 @@ impl Article {

Self::delete_all(&mut transaction).await?;
for article in articles {
article.insert(&mut transaction).await?;
if let Err(error) = article.insert(&mut transaction).await {
warn!("Failed to insert article: {error}");
}
}

transaction.commit().await
Expand Down Expand Up @@ -333,4 +337,70 @@ mod tests {
check("S2017, A2711, 20€", None);
check("20 Euro", None);
}

#[tokio::test]
async fn test_duplicate_article_insertion() -> anyhow::Result<()> {
let article1 = Article {
id: "1".to_string(),
designation: "Test Artikel 1".to_string(),
barcode: "1".to_string(),
prices: vec![],
};

let article2 = Article {
id: "1".to_string(),
designation: "Test Artikel 2".to_string(),
barcode: "1".to_string(),
prices: vec![],
};

let articles = vec![article1, article2];

let pool = SqlitePool::connect(":memory:").await?;
sqlx::migrate!().run(&pool).await?;

Article::save_all(pool.clone(), articles).await?;

let (count,): (u32,) = sqlx::query_as("SELECT COUNT(*) FROM articles")
.fetch_one(&pool)
.await?;

assert_eq!(count, 1);

Ok(())
}

#[tokio::test]
async fn test_duplicate_member_insertion() -> anyhow::Result<()> {
let member1 = Member {
id: "1".to_string(),
firstname: "John".to_string(),
lastname: "Doe".to_string(),
nickname: "".to_string(),
keycodes: vec!["0005635570".to_string()],
};

let member2 = Member {
id: "1".to_string(),
firstname: "Jane".to_string(),
lastname: "Doe".to_string(),
nickname: "".to_string(),
keycodes: vec!["0005635570".to_string()],
};

let members = vec![member1, member2];

let pool = SqlitePool::connect(":memory:").await?;
sqlx::migrate!().run(&pool).await?;

Member::save_all(pool.clone(), members).await?;

let (count,): (u32,) = sqlx::query_as("SELECT COUNT(*) FROM members")
.fetch_one(&pool)
.await?;

assert_eq!(count, 1);

Ok(())
}
}

0 comments on commit cc10ba7

Please sign in to comment.