fix(core): 消除乐观锁 version.unwrap() 潜在 panic
20 处 ActiveValue::unwrap() + 1 乐观锁递增改为 take().unwrap_or(0) + 1, 避免数据库记录缺少 version 字段时 panic。覆盖 erp-auth/erp-config/ erp-workflow/erp-health/erp-ai/erp-server 7 个 crate。 DTO 层 Option<i32> 字段保持原有 unwrap_or(0) 不变。
This commit is contained in:
@@ -143,7 +143,7 @@ where
|
||||
}
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.updated_by = Set(Some(ctx.user_id));
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
|
||||
let result = active.update(&state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -179,7 +179,7 @@ where
|
||||
active.deleted_at = Set(Some(chrono::Utc::now()));
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.updated_by = Set(Some(ctx.user_id));
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&state.db).await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(serde_json::json!({"deleted": true}))))
|
||||
|
||||
@@ -170,7 +170,7 @@ impl AnalysisService {
|
||||
active.result_content = Set(Some(content));
|
||||
active.result_metadata = Set(Some(metadata));
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&self.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -186,7 +186,7 @@ impl AnalysisService {
|
||||
active.status = Set("failed".into());
|
||||
active.error_message = Set(Some(error));
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&self.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ impl AnalysisQueue {
|
||||
active.status = Set("running".to_string());
|
||||
active.started_at = Set(Some(now));
|
||||
active.updated_at = Set(now);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
let model = active.update(&self.db).await?;
|
||||
Ok(Some(model))
|
||||
}
|
||||
@@ -140,7 +140,7 @@ impl AnalysisQueue {
|
||||
active.completed_at = Set(Some(now));
|
||||
active.result_analysis_id = Set(Some(result_analysis_id));
|
||||
active.updated_at = Set(now);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&self.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -163,7 +163,7 @@ impl AnalysisQueue {
|
||||
active.retry_count = Set(retry_count + 1);
|
||||
active.started_at = Set(None);
|
||||
active.updated_at = Set(now);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&self.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ impl InsightService {
|
||||
active.is_dismissed = Set(true);
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.updated_by = Set(updated_by);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(db).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -123,7 +123,7 @@ impl InsightService {
|
||||
let mut active: copilot_insights::ActiveModel = model.into();
|
||||
active.deleted_at = Set(Some(now));
|
||||
active.updated_at = Set(now);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(db).await?;
|
||||
}
|
||||
Ok(count)
|
||||
|
||||
@@ -169,7 +169,7 @@ impl PromptService {
|
||||
let mut active: ai_prompt::ActiveModel = sibling.into();
|
||||
active.is_active = Set(false);
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(&self.db).await?;
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ impl PromptService {
|
||||
let mut active: ai_prompt::ActiveModel = entity.into();
|
||||
active.is_active = Set(true);
|
||||
active.updated_at = Set(chrono::Utc::now());
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
Ok(active.update(&self.db).await?)
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ impl RiskService {
|
||||
active.llm_summary = Set(llm_summary);
|
||||
active.computed_at = Set(now);
|
||||
active.updated_at = Set(now);
|
||||
active.version_lock = Set(active.version_lock.unwrap() + 1);
|
||||
active.version_lock = Set(active.version_lock.take().unwrap_or(0) + 1);
|
||||
active.update(db).await?;
|
||||
} else {
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
Reference in New Issue
Block a user