mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-31 19:22:15 +08:00
fix: improve database schema handling and add migration feature gate
- Fix column name quoting in ALTER TABLE statements to prevent SQL syntax errors with reserved keywords - Use case-insensitive table name matching in table_exists() - Change type declarations from INTEGER to BOOLEAN for semantic clarity - Add CC_SWITCH_ENABLE_JSON_DB_MIGRATION env var to gate JSON→SQLite migration (disabled by default until feature is stable) - Refactor tests with shared legacy schema and column info helpers - Add comprehensive test for column types and default values alignment
This commit is contained in:
+20
-4
@@ -113,6 +113,17 @@ const TRAY_SECTIONS: [TrayAppSection; 3] = [
|
||||
},
|
||||
];
|
||||
|
||||
/// 读取类似布尔的环境变量(1/true/yes/on 视为开启)
|
||||
fn env_flag_enabled(key: &str) -> bool {
|
||||
match std::env::var(key) {
|
||||
Ok(val) => matches!(
|
||||
val.trim().to_ascii_lowercase().as_str(),
|
||||
"1" | "true" | "yes" | "on"
|
||||
),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn append_provider_section<'a>(
|
||||
app: &'a tauri::AppHandle,
|
||||
mut menu_builder: MenuBuilder<'a, tauri::Wry, tauri::AppHandle<tauri::Wry>>,
|
||||
@@ -542,8 +553,9 @@ pub fn run() {
|
||||
let db_path = app_config_dir.join("cc-switch.db");
|
||||
let json_path = app_config_dir.join("config.json");
|
||||
|
||||
// Check if migration is needed (DB doesn't exist but JSON does)
|
||||
let migration_needed = !db_path.exists() && json_path.exists();
|
||||
// 检查是否需要从 config.json 迁移到 SQLite,功能尚未正式发布,默认关闭
|
||||
let migration_opt_in = env_flag_enabled("CC_SWITCH_ENABLE_JSON_DB_MIGRATION");
|
||||
let migration_needed = !db_path.exists() && json_path.exists() && migration_opt_in;
|
||||
|
||||
let db = match crate::database::Database::init() {
|
||||
Ok(db) => Arc::new(db),
|
||||
@@ -555,8 +567,12 @@ pub fn run() {
|
||||
}
|
||||
};
|
||||
|
||||
if migration_needed {
|
||||
log::info!("Starting migration from config.json to SQLite...");
|
||||
if !migration_opt_in && !db_path.exists() && json_path.exists() {
|
||||
log::warn!(
|
||||
"检测到 config.json,但 JSON→数据库迁移功能尚未正式发布,默认跳过。设置 CC_SWITCH_ENABLE_JSON_DB_MIGRATION=1 后再尝试。"
|
||||
);
|
||||
} else if migration_needed {
|
||||
log::info!("Starting migration from config.json to SQLite (opt-in)...");
|
||||
match crate::app_config::MultiAppConfig::load() {
|
||||
Ok(config) => {
|
||||
if let Err(e) = db.migrate_from_json(&config) {
|
||||
|
||||
Reference in New Issue
Block a user