refactor(backend): migrate import/export to use SQL backup

- Reimplement export_config_to_file to use database.export_sql
- Reimplement import_config_from_file to use database.import_sql
- Add sync_current_from_db to sync live configs after import
- Add settings database binding on app initialization
- Remove deprecated JSON-based config import logic
This commit is contained in:
YoVinchen
2025-11-24 00:46:00 +08:00
parent 6443dc897d
commit fd25c9949f
3 changed files with 67 additions and 55 deletions

View File

@@ -12,6 +12,7 @@ use crate::config::{
};
use crate::error::AppError;
use crate::provider::{Provider, UsageData, UsageResult};
use crate::services::mcp::McpService;
use crate::settings::{self, CustomEndpoint};
use crate::store::AppState;
use crate::usage_script;
@@ -550,6 +551,30 @@ impl ProviderService {
Ok(())
}
/// 将数据库中的当前供应商同步到对应 live 配置
pub fn sync_current_from_db(state: &AppState) -> Result<(), AppError> {
for app_type in [AppType::Claude, AppType::Codex, AppType::Gemini] {
let current_id = match state.db.get_current_provider(app_type.as_str())? {
Some(id) => id,
None => continue,
};
let providers = state.db.get_all_providers(app_type.as_str())?;
if let Some(provider) = providers.get(&current_id) {
Self::write_live_snapshot(&app_type, provider)?;
} else {
log::warn!(
"无法同步 live 配置: 当前供应商 {} ({}) 未找到",
current_id,
app_type.as_str()
);
}
}
// MCP 同步
McpService::sync_all_enabled(state)?;
Ok(())
}
/// 列出指定应用下的所有供应商
pub fn list(
state: &AppState,