BLOG-133 Unique label name and semantic ID #135

Merged
squid merged 4 commits from BLOG-133_unique_label_name into main 2025-10-12 21:16:36 +08:00
2 changed files with 32 additions and 4 deletions
Showing only changes of commit 78dfca41e6 - Show all commits

View File

@ -32,7 +32,14 @@ impl LabelDbService for LabelDbServiceImpl {
)
.fetch_one(&self.db_pool)
.await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?;
.map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_label_name") {
return PostError::DuplicatedLabelName;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?;
Ok(id)
}
@ -50,7 +57,14 @@ impl LabelDbService for LabelDbServiceImpl {
)
.execute(&self.db_pool)
.await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?
.map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_label_name") {
return PostError::DuplicatedLabelName;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?
.rows_affected();
if affected_rows == 0 {

View File

@ -211,7 +211,14 @@ impl PostDbService for PostDbServiceImpl {
)
.fetch_one(&mut *tx)
.await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?;
.map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_post_semantic_id") {
return PostError::DuplicatedSemanticId;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?;
for (order, &label_id) in label_ids.iter().enumerate() {
sqlx::query!(
@ -266,7 +273,14 @@ impl PostDbService for PostDbServiceImpl {
)
.execute(&mut *tx)
.await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?
.map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_post_semantic_id") {
return PostError::DuplicatedSemanticId;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?
.rows_affected();
if affected_rows == 0 {