A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io
Rust ai-toolsclaude-codecodexdeepseek-harnessdesktop-appgrokgrokbuildhermeskimi-codemcpopen-sourceopencodepiprovider-managementrustskillsskills-managementtauriwsl-support
//! 数据库模块 - SQLite 数据持久化
//!
//! 此模块提供应用的核心数据存储功能,包括:
//! - 供应商配置管理
//! - MCP 服务器配置
//! - 提示词管理
//! - Skills 管理
//! - 通用设置存储
//!
//! ## 架构设计
//!
//! ```text
//! database/
//! ├── mod.rs - Database 结构体 + 初始化
//! ├── schema.rs - 表结构定义 + Schema 迁移
//! ├── backup.rs - SQL 导入导出 + 快照备份
//! ├── migration.rs - JSON → SQLite 数据迁移
//! └── dao/ - 数据访问对象
//! ├── providers.rs
//! ├── mcp.rs
//! ├── prompts.rs
//! ├── skills.rs
//! └── settings.rs
//! ```
pub(crate) mod backup;
mod dao;
mod migration;
mod read_cache;
mod schema;
#[cfg(test)]
mod tests;
// DAO 类型导出供外部使用
pub use dao::custom_icons::{CustomIconEntry, CUSTOM_ICONS_KEY};
pub(crate) use dao::providers_seed::{
is_official_seed_id, CLAUDE_DESKTOP_OFFICIAL_PROVIDER_ID, CODEX_OFFICIAL_PROVIDER_ID,
GROKBUILD_OFFICIAL_PROVIDER_ID,
};
pub(crate) use dao::proxy::{PRICING_SOURCE_REQUEST, PRICING_SOURCE_RESPONSE};
pub use dao::FailoverQueueItem;
pub use dao::Profile;
pub use dao::UnreadableEntry;
use crate::app_config::AppType;
use crate::config::get_app_config_dir;
use crate::error::AppError;
use rusqlite::{hooks::Action, Connection};
use serde::Serialize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
// DAO 方法通过 impl Database 提供,无需额外导出
/// 当前 Schema 版本号
/// 每次修改表结构时递增,并在 schema.rs 中添加相应的迁移逻辑
pub(crate) const SCHEMA_VERSION: i32 = 31;
/// 安全地序列化 JSON,避免 unwrap panic
pub(crate) fn to_json_string<T: Serialize>(value: &T) -> Result<String, AppError> {
serde_json::to_string(value)
.map_err(|e| AppError::Config(format!("JSON serialization failed: {e}")))
}
/// 获取数据库的锁,锁中毒时清理后继续使用,见 [`lock_recovering`]。
macro_rules! lock_conn {
($mutex:expr) => {
$crate::database::lock_recovering(&$mutex)?
};
}
// 导出宏供子模块使用
pub(crate) use lock_conn;
/// 持锁的任务 panic 后 `Mutex` 会中毒,之后每次 `lock()` 都失败。整个应用共用一条
/// 数据库连接,中毒后托盘、供应商切换、退出时写回直连全都失败,只能重启进程。
/// 这里取回锁内的值,清理成功后清除中毒标记照常返回;清理失败则保持中毒并报错。
pub(crate) fn lock_recovering<T: RecoverPoisoned>(
mutex: &Mutex<T>,
) -> Result<MutexGuard<'_, T>, AppError> {
mutex.lock().or_else(|poisoned| {
let mut guard = poisoned.into_inner();
guard.recover_poisoned()?;
mutex.clear_poison();
log::warn!("数据库锁因持锁任务 panic 而中毒,已清理后继续使用");
Ok(guard)
})
}
/// 锁中毒后、继续使用锁内的值之前要做的清理。
pub(crate) trait RecoverPoisoned {
fn recover_poisoned(&mut self) -> Result<(), AppError>;
}
impl RecoverPoisoned for Connection {
/// rusqlite 的 `Transaction` 在 panic 展开时会自己回滚;这里兜底手写 `BEGIN` 等还停在
/// 事务里的情况,否则之后的写入都会落进一个永远不会提交的事务。
fn recover_poisoned(&mut self) -> Result<(), AppError> {
if !self.is_autocommit() {
if let Err(e) = self.execute_batch("ROLLBACK") {
log::error!("数据库锁中毒后回滚未完成的事务失败: {e}");
}
}
if self.is_autocommit() {
Ok(())
} else {
Err(AppError::Database(
"Mutex lock failed: connection is still in a transaction after poisoning".into(),
))
}
}
}
impl RecoverPoisoned for Option<crate::services::usage_stats::LogCountCache> {
/// 只是加速用的缓存,清空后下次查询重新统计。
fn recover_poisoned(&mut self) -> Result<(), AppError> {
*self = None;
Ok(())
}
}
/// 数据库连接封装
///
/// 使用 Mutex 包装 Connection 以支持在多线程环境(如 Tauri State)中共享。
/// rusqlite::Connection 本身不是 Sync 的,因此需要这层包装。
#[derive(Clone)]
pub struct Database {
pub(crate) conn: Arc<Mutex<Connection>>,
pub(crate) read_cache: Arc<read_cache::ProxyReadCache>,
pub(crate) route_revisions: Arc<[AtomicU64; AppType::ALL.len()]>,
/// 请求日志总数缓存,见 `services::usage_stats::LogCountCache`
pub(crate) log_count_cache: Arc<Mutex<Option<crate::services::usage_stats::LogCountCache>>>,
}
fn register_db_change_hook(conn: &Connection, cache: Arc<read_cache::ProxyReadCache>) {
// DELETE without WHERE normally skips row hooks. Disable only that optimization
// for cached tables, including when a prepared DELETE statement is reused.
let ddl_cache = cache.clone();
let mut dropping_table = None;
conn.authorizer(Some(
move |context: rusqlite::hooks::AuthContext<'_>| match context.action {
rusqlite::hooks::AuthAction::DropTable { table_name }
| rusqlite::hooks::AuthAction::DropTempTable { table_name } => {
ddl_cache.invalidate_table(table_name);
dropping_table = Some(table_name.to_owned());
rusqlite::hooks::Authorization::Allow
}
// SQLite also authorizes DELETE as part of DROP TABLE. IGNORE there
// would silently skip the DROP and break table-rebuilding migrations.
rusqlite::hooks::AuthAction::Delete { table_name }
if dropping_table.as_deref() == Some(table_name) =>
{
dropping_table = None;
rusqlite::hooks::Authorization::Allow
}
rusqlite::hooks::AuthAction::Delete {
table_name:
"settings"
| "proxy_config"
| "model_pricing"
| "providers"
| "provider_endpoints",
} => rusqlite::hooks::Authorization::Ignore,
_ => rusqlite::hooks::Authorization::Allow,
},
));
let rollback_cache = cache.clone();
conn.rollback_hook(Some(move || rollback_cache.invalidate_all()));
conn.update_hook(Some(
move |action: Action, _database: &str, table: &str, _row_id: i64| match action {
Action::SQLITE_INSERT | Action::SQLITE_UPDATE | Action::SQLITE_DELETE => {
cache.invalidate_table(table);
use crate::services::{auto_sync, s3_auto_sync, webdav_auto_sync};
auto_sync::notify_db_changed::<webdav_auto_sync::WebDavAutoSync>(table);
auto_sync::notify_db_changed::<s3_auto_sync::S3AutoSync>(table);
}
_ => {}
},
));
}
impl Database {
/// Requests capture this before reading routing policy so an overlapping
/// policy change cannot stamp an old selection with the new revision.
pub(crate) fn route_revision(&self, app: &AppType) -> u64 {
self.route_revisions[app.clone() as usize].load(Ordering::Acquire)
}
pub(crate) fn advance_route_revision(&self, app: &AppType) {
self.route_revisions[app.clone() as usize].fetch_add(1, Ordering::AcqRel);
}
fn configure_connection(conn: &Connection) -> Result<(), AppError> {
conn.execute_batch(
"PRAGMA busy_timeout = 5000;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA temp_store = MEMORY;
PRAGMA cache_size = -16000;",
)
.map_err(|e| AppError::Database(format!("Configure SQLite connection: {e}")))
}
/// Run synchronous SQLite/file work away from async executor workers.
pub(crate) async fn run_blocking<T: Send + 'static>(
&self,
work: impl FnOnce(&Database) -> Result<T, AppError> + Send + 'static,
) -> Result<T, AppError> {
let db = self.clone();
// Startup also calls these async DAOs through futures::executor::block_on
// before entering Tokio. Tauri provides a runtime for that synchronous caller.
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime
.spawn_blocking(move || work(&db))
.await
.map_err(|e| AppError::Database(format!("Database worker failed: {e}")))?
} else {
tauri::async_runtime::spawn_blocking(move || work(&db))
.await
.map_err(|e| AppError::Database(format!("Database worker failed: {e}")))?
}
}
/// Call after stopping proxy writes, before explicit process exit/restart.
pub fn checkpoint_wal(&self) -> Result<(), AppError> {
let conn = lock_conn!(self.conn);
let busy: i64 = conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| row.get(0))?;
if busy != 0 {
return Err(AppError::Database("WAL checkpoint is busy".into()));
}
Ok(())
}
/// 初始化数据库连接并创建表
///
/// 数据库文件位于 `~/.cc-switch/cc-switch.db`
pub fn init() -> Result<Self, AppError> {
let db_path = get_app_config_dir().join("cc-switch.db");
let db_exists = db_path.exists();
// 确保父目录存在
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
}
let conn = Connection::open(&db_path).map_err(|e| AppError::Database(e.to_string()))?;
// 数据库包含供应商 API Key 与账号凭据,在 Unix 上始终收紧为仅属主可读写(0600)。
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Err(e) =
std::fs::set_permissions(&db_path, std::fs::Permissions::from_mode(0o600))
{
log::warn!(
"Failed to restrict permissions on {}: {e}",
db_path.display()
);
}
}
// 启用外键约束
conn.execute("PRAGMA foreign_keys = ON;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
if !db_exists {
// For a brand-new database, configure incremental auto-vacuum
// before creating any tables so no rebuild is needed later.
conn.execute("PRAGMA auto_vacuum = INCREMENTAL;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
}
Self::configure_connection(&conn)?;
let read_cache = Arc::new(read_cache::ProxyReadCache {
failover_reader: Some(Mutex::new(dao::failover::FailoverReader::open(&db_path)?)),
..Default::default()
});
register_db_change_hook(&conn, read_cache.clone());
let db = Self {
conn: Arc::new(Mutex::new(conn)),
log_count_cache: Arc::new(Mutex::new(None)),
read_cache,
route_revisions: Arc::default(),
};
db.create_tables()?;
// Pre-migration backup: only when upgrading from an existing database
{
let conn = lock_conn!(db.conn);
let version = Self::get_user_version(&conn)?;
drop(conn);
if version > 0 && version < SCHEMA_VERSION {
log::info!(
"Creating pre-migration database backup (v{version} → v{SCHEMA_VERSION})"
);
if let Err(e) = db.backup_database_file() {
log::warn!("Pre-migration backup failed, continuing migration: {e}");
}
}
}
db.apply_schema_migrations()?;
if let Err(e) = db.ensure_incremental_auto_vacuum() {
log::warn!("Failed to ensure incremental auto-vacuum: {e}");
}
db.ensure_model_pricing_seeded()?;
if let Err(e) = crate::services::model_pricing::sync_local_model_pricing(&db) {
log::warn!("Failed to sync local model pricing file: {e}");
}
// Log rollup/pruning and vacuum run in periodic_backup_if_needed after
// startup recovery, on a blocking worker. Doing them here holds up the
// main window and repeats the same maintenance during startup.
Ok(db)
}
/// 读取磁盘上数据库的 `user_version`;仅当它比应用支持的 [`SCHEMA_VERSION`]
/// 更新时返回 `Some(version)`。
///
/// 用于初始化失败后判断是否为「数据库版本过新(应用过旧,需升级应用)」的可恢复
/// 场景——此时不应反复弹出无效的重试对话框,而应引导用户在应用内升级。
pub fn stored_user_version_exceeds_supported(
db_path: &std::path::Path,
) -> Result<Option<i32>, AppError> {
if !db_path.exists() {
return Ok(None);
}
let conn = Connection::open(db_path).map_err(|e| AppError::Database(e.to_string()))?;
let version = Self::get_user_version(&conn)?;
Ok((version > SCHEMA_VERSION).then_some(version))
}
/// 创建内存数据库(用于测试)
pub fn memory() -> Result<Self, AppError> {
let conn = Connection::open_in_memory().map_err(|e| AppError::Database(e.to_string()))?;
// 启用外键约束
conn.execute("PRAGMA foreign_keys = ON;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
conn.execute("PRAGMA auto_vacuum = INCREMENTAL;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
Self::configure_connection(&conn)?;
let read_cache = Arc::new(read_cache::ProxyReadCache::default());
register_db_change_hook(&conn, read_cache.clone());
let db = Self {
conn: Arc::new(Mutex::new(conn)),
log_count_cache: Arc::new(Mutex::new(None)),
read_cache,
route_revisions: Arc::default(),
};
db.create_tables()?;
db.ensure_model_pricing_seeded()?;
Ok(db)
}
pub(crate) fn get_auto_vacuum_mode(conn: &Connection) -> Result<i32, AppError> {
conn.query_row("PRAGMA auto_vacuum;", [], |row| row.get(0))
.map_err(|e| AppError::Database(format!("读取 auto_vacuum 失败: {e}")))
}
/// 回收全部空闲页。`PRAGMA incremental_vacuum` 每释放一页产出一行结果,
/// 必须把结果读完才会回收完;`execute_batch` 只 step 一次,只能回收 1 页。
pub(crate) fn incremental_vacuum_on_conn(conn: &Connection) -> Result<(), AppError> {
let mut stmt = conn
.prepare("PRAGMA incremental_vacuum;")
.map_err(|e| AppError::Database(format!("执行 incremental_vacuum 失败: {e}")))?;
let mut rows = stmt
.query([])
.map_err(|e| AppError::Database(format!("执行 incremental_vacuum 失败: {e}")))?;
while rows
.next()
.map_err(|e| AppError::Database(format!("执行 incremental_vacuum 失败: {e}")))?
.is_some()
{}
Ok(())
}
fn has_user_tables(conn: &Connection) -> Result<bool, AppError> {
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'",
[],
|row| row.get(0),
)
.map_err(|e| AppError::Database(format!("读取表数量失败: {e}")))?;
Ok(count > 0)
}
pub(crate) fn ensure_incremental_auto_vacuum_on_conn(
conn: &Connection,
) -> Result<bool, AppError> {
let mode = Self::get_auto_vacuum_mode(conn)?;
if mode == 2 {
return Ok(false);
}
let has_tables = Self::has_user_tables(conn)?;
conn.execute("PRAGMA auto_vacuum = INCREMENTAL;", [])
.map_err(|e| AppError::Database(format!("设置 auto_vacuum 失败: {e}")))?;
if !has_tables {
return Ok(false);
}
conn.execute("VACUUM;", [])
.map_err(|e| AppError::Database(format!("执行 VACUUM 失败: {e}")))?;
conn.execute("PRAGMA foreign_keys = ON;", [])
.map_err(|e| AppError::Database(format!("恢复 foreign_keys 失败: {e}")))?;
Ok(true)
}
pub(crate) fn ensure_incremental_auto_vacuum(&self) -> Result<bool, AppError> {
let mode = {
let conn = lock_conn!(self.conn);
Self::get_auto_vacuum_mode(&conn)?
};
if mode == 2 {
return Ok(false);
}
let has_tables = {
let conn = lock_conn!(self.conn);
Self::has_user_tables(&conn)?
};
if has_tables {
log::info!(
"Detected auto_vacuum={mode}, rebuilding database to enable incremental vacuum"
);
self.backup_database_file()?;
}
let rebuilt = {
let conn = lock_conn!(self.conn);
Self::ensure_incremental_auto_vacuum_on_conn(&conn)?
};
if rebuilt {
log::info!("Incremental auto-vacuum enabled after database rebuild");
} else {
log::info!("Incremental auto-vacuum configured for new database");
}
Ok(rebuilt)
}
/// 检查 MCP 服务器表是否为空
pub fn is_mcp_table_empty(&self) -> Result<bool, AppError> {
let conn = lock_conn!(self.conn);
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM mcp_servers", [], |row| row.get(0))
.map_err(|e| AppError::Database(e.to_string()))?;
Ok(count == 0)
}
/// 检查提示词表是否为空
pub fn is_prompts_table_empty(&self) -> Result<bool, AppError> {
let conn = lock_conn!(self.conn);
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM prompts", [], |row| row.get(0))
.map_err(|e| AppError::Database(e.to_string()))?;
Ok(count == 0)
}
}
#[cfg(all(test, unix))]
mod permission_tests {
use super::Database;
use crate::test_support::TempUserHome;
use serial_test::serial;
use std::os::unix::fs::PermissionsExt;
#[test]
#[serial]
fn database_init_restricts_db_file_to_0600_on_unix() {
let _home = TempUserHome::new();
let _db = Database::init().expect("Database::init");
let db_path = crate::config::get_app_config_dir().join("cc-switch.db");
let mode = std::fs::metadata(&db_path)
.expect("db metadata")
.permissions()
.mode()
& 0o777;
assert_eq!(
mode, 0o600,
"cc-switch.db contains credentials and must be 0600 on Unix, got {mode:o}"
);
}
}