refactor(startup): improve first-launch import logic with per-table detection

- Replace global `is_empty_for_first_import()` with independent checks:
  - `is_mcp_table_empty()` for MCP server imports
  - `is_prompts_table_empty()` for prompt imports
  - Skills and providers already have built-in idempotency checks

- Fix misleading logs in provider import:
  - Change `import_default_config` return type from `Result<()>` to `Result<bool>`
  - Return `true` when actually imported, `false` when skipped
  - Only log success message when import actually occurred

- Add idempotency protection to `import_from_file_on_first_launch`

This allows each data type to be independently recovered if deleted,
rather than requiring all tables to be empty for any import to trigger.
This commit is contained in:
Jason
2025-11-28 12:00:32 +08:00
parent 7db4b8d976
commit 1ee1e9cb2e
6 changed files with 79 additions and 98 deletions
+6
View File
@@ -176,6 +176,12 @@ impl PromptService {
state: &AppState,
app: AppType,
) -> Result<usize, AppError> {
// 幂等性保护:该应用已有提示词则跳过
let existing = state.db.get_prompts(app.as_str())?;
if !existing.is_empty() {
return Ok(0);
}
let file_path = prompt_file_path(&app)?;
// 检查文件是否存在
+6 -3
View File
@@ -215,11 +215,14 @@ pub fn read_live_settings(app_type: AppType) -> Result<Value, AppError> {
}
/// Import default configuration from live files
pub fn import_default_config(state: &AppState, app_type: AppType) -> Result<(), AppError> {
///
/// Returns `Ok(true)` if a provider was actually imported,
/// `Ok(false)` if skipped (providers already exist for this app).
pub fn import_default_config(state: &AppState, app_type: AppType) -> Result<bool, AppError> {
{
let providers = state.db.get_all_providers(app_type.as_str())?;
if !providers.is_empty() {
return Ok(());
return Ok(false); // 已有供应商,跳过
}
}
@@ -298,7 +301,7 @@ pub fn import_default_config(state: &AppState, app_type: AppType) -> Result<(),
.db
.set_current_provider(app_type.as_str(), &provider.id)?;
Ok(())
Ok(true) // 真正导入了
}
/// Write Gemini live configuration with authentication handling
+3 -1
View File
@@ -225,7 +225,9 @@ impl ProviderService {
}
/// Import default configuration from live files (re-export)
pub fn import_default_config(state: &AppState, app_type: AppType) -> Result<(), AppError> {
///
/// Returns `Ok(true)` if imported, `Ok(false)` if skipped.
pub fn import_default_config(state: &AppState, app_type: AppType) -> Result<bool, AppError> {
import_default_config(state, app_type)
}