Tauri 2 + Vue 3 + Vite 6 桌面应用,Rust workspace 含 13 个 crate (df-ai / df-storage / df-workflow / df-core / df-execute 等)。 核心能力:AI 聊天 agentic 循环(工具调用+人工审批)、工作流引擎、 任务/想法/项目/阶段管理、可追溯性,及配套前端组件。
62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
//! SQLite 数据库连接管理
|
|
|
|
use std::path::Path;
|
|
use std::sync::Arc;
|
|
|
|
use anyhow::Result;
|
|
use rusqlite::Connection;
|
|
use tokio::sync::Mutex;
|
|
|
|
use crate::migrations;
|
|
|
|
/// SQLite 数据库管理器
|
|
pub struct Database {
|
|
/// 数据库连接(线程安全)
|
|
conn: Arc<Mutex<Connection>>,
|
|
}
|
|
|
|
impl Database {
|
|
/// 打开(或创建)数据库文件
|
|
pub async fn open(path: &Path) -> Result<Self> {
|
|
let conn = Connection::open(path)?;
|
|
conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
|
|
|
|
// 执行迁移
|
|
let db = Self {
|
|
conn: Arc::new(Mutex::new(conn)),
|
|
};
|
|
db.run_migrations().await?;
|
|
|
|
tracing::info!("数据库已打开: {}", path.display());
|
|
Ok(db)
|
|
}
|
|
|
|
/// 打开内存数据库(用于测试)
|
|
pub async fn open_in_memory() -> Result<Self> {
|
|
let conn = Connection::open_in_memory()?;
|
|
conn.execute_batch("PRAGMA foreign_keys=ON;")?;
|
|
|
|
let db = Self {
|
|
conn: Arc::new(Mutex::new(conn)),
|
|
};
|
|
db.run_migrations().await?;
|
|
|
|
Ok(db)
|
|
}
|
|
|
|
/// 执行数据库迁移
|
|
async fn run_migrations(&self) -> Result<()> {
|
|
let conn = self.conn.lock().await;
|
|
migrations::run(&conn)?;
|
|
tracing::info!("数据库迁移完成");
|
|
Ok(())
|
|
}
|
|
|
|
/// 获取连接的锁守卫
|
|
///
|
|
/// TODO: 考虑使用 r2d2 连接池替代单连接 Mutex
|
|
pub fn conn(&self) -> Arc<Mutex<Connection>> {
|
|
Arc::clone(&self.conn)
|
|
}
|
|
}
|