From 8f58c08d0dc9c15d4c1119880cdab833f784756d Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 25 Dec 2025 16:04:34 +0800 Subject: [PATCH] fix(database): add backward compatibility check for proxy_config seed insert Add has_column check before inserting seed data into proxy_config table. This prevents SQL errors when upgrading from older databases where proxy_config was a singleton table without the app_type column. The migration function will handle the table structure upgrade and insert the three rows after converting to the new schema. --- src-tauri/src/database/schema.rs | 60 ++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/database/schema.rs b/src-tauri/src/database/schema.rs index 70ab96d68..0a1429110 100644 --- a/src-tauri/src/database/schema.rs +++ b/src-tauri/src/database/schema.rs @@ -113,33 +113,39 @@ impl Database { )", []).map_err(|e| AppError::Database(e.to_string()))?; // 初始化三行数据(每应用不同默认值) - conn.execute( - "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, - streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, - circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, - circuit_error_rate_threshold, circuit_min_requests) - VALUES ('claude', 6, 45, 90, 300, 8, 3, 90, 0.6, 15)", - [], - ) - .map_err(|e| AppError::Database(e.to_string()))?; - conn.execute( - "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, - streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, - circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, - circuit_error_rate_threshold, circuit_min_requests) - VALUES ('codex', 3, 30, 60, 300, 5, 2, 60, 0.5, 10)", - [], - ) - .map_err(|e| AppError::Database(e.to_string()))?; - conn.execute( - "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, - streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, - circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, - circuit_error_rate_threshold, circuit_min_requests) - VALUES ('gemini', 5, 30, 60, 300, 5, 2, 60, 0.5, 10)", - [], - ) - .map_err(|e| AppError::Database(e.to_string()))?; + // + // 兼容旧数据库: + // - 老版本 proxy_config 是单例表(没有 app_type 列),此时不能执行三行 seed insert; + // - 旧表会在 apply_schema_migrations() 中迁移为三行结构后再插入。 + if Self::has_column(conn, "proxy_config", "app_type")? { + conn.execute( + "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, + streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, + circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, + circuit_error_rate_threshold, circuit_min_requests) + VALUES ('claude', 6, 45, 90, 300, 8, 3, 90, 0.6, 15)", + [], + ) + .map_err(|e| AppError::Database(e.to_string()))?; + conn.execute( + "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, + streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, + circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, + circuit_error_rate_threshold, circuit_min_requests) + VALUES ('codex', 3, 30, 60, 300, 5, 2, 60, 0.5, 10)", + [], + ) + .map_err(|e| AppError::Database(e.to_string()))?; + conn.execute( + "INSERT OR IGNORE INTO proxy_config (app_type, max_retries, + streaming_first_byte_timeout, streaming_idle_timeout, non_streaming_timeout, + circuit_failure_threshold, circuit_success_threshold, circuit_timeout_seconds, + circuit_error_rate_threshold, circuit_min_requests) + VALUES ('gemini', 5, 30, 60, 300, 5, 2, 60, 0.5, 10)", + [], + ) + .map_err(|e| AppError::Database(e.to_string()))?; + } // 9. Provider Health 表 conn.execute("CREATE TABLE IF NOT EXISTS provider_health (