update code level

This commit is contained in:
lychang
2026-07-21 04:46:01 +08:00
parent f6c7baee5e
commit 7c9c80fe39

View File

@@ -38,16 +38,24 @@ pub struct SearchDb {
impl SearchDb {
pub async fn new(db_path: &str) -> Result<Self, sqlx::Error> {
if let Some(parent) = std::path::Path::new(db_path).parent() {
// 确保父目录存在
let path = std::path::Path::new(db_path);
if let Some(parent) = path.parent() {
if !parent.exists() {
std::fs::create_dir_all(parent)?;
}
}
if !std::path::Path::new(db_path).exists() {
std::fs::File::create(db_path)?;
}
let url = format!("sqlite:{}", db_path.replace('\\', "/"));
// 转换为绝对路径并创建文件(如果不存在)
let abs_path = std::fs::canonicalize(db_path)
.unwrap_or_else(|_| {
// canonicalize 要求文件存在,所以先创建再转换
std::fs::File::create(db_path).ok();
std::fs::canonicalize(db_path).unwrap_or_else(|_| path.to_path_buf())
});
// 使用 sqlite:/// 前缀 + 绝对路径,避免相对路径解析错误
let url = format!("sqlite:///{}", abs_path.to_string_lossy().replace('\\', "/"));
let pool = SqlitePoolOptions::new()
.max_connections(2)