Repository navigation
fix(db): sqlite migrations check real schema instead of schema_migrations records #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| use crate::error::AppError; | ||
| use sqlx::{Pool, Sqlite}; | ||
| use std::collections::HashSet; | ||
|
|
||
| const MIGRATIONS: &[(i64, &str)] = &[ | ||
| (1, include_str!("../../migrations/001_initial.sql")), | ||
|
|
@@ -48,60 +47,173 @@ const MIGRATIONS: &[(i64, &str)] = &[ | |
| ), | ||
| ]; | ||
|
|
||
| pub async fn run_migrations(pool: &Pool<Sqlite>) -> Result<(), AppError> { | ||
| sqlx::query( | ||
| "CREATE TABLE IF NOT EXISTS schema_migrations ( | ||
| version INTEGER PRIMARY KEY, | ||
| applied_at TEXT NOT NULL DEFAULT (datetime('now')) | ||
| )", | ||
| ) | ||
| .execute(pool) | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("创建 schema_migrations 表失败: {e}")))?; | ||
| /// 迁移是否已实际生效(表/列已存在)。 | ||
| /// 必须基于真实 schema 检查而不是 schema_migrations 记录:旧版本数据库 | ||
| /// 曾被一次性标记为"全部已应用"却未执行(缺 auth_source 列、 | ||
| /// redis_command_logs 表),只看记录会继续漏掉迁移。 | ||
| async fn migration_applied(pool: &Pool<Sqlite>, version: i64) -> Result<bool, AppError> { | ||
| let table_exists = |name: &str| { | ||
| format!("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='{name}')") | ||
| }; | ||
| let column_exists = |table: &str, column: &str| { | ||
| format!( | ||
| "SELECT EXISTS(SELECT 1 FROM pragma_table_info('{table}') WHERE name='{column}')" | ||
| ) | ||
| }; | ||
|
|
||
| let applied: Vec<i64> = | ||
| sqlx::query_scalar("SELECT version FROM schema_migrations ORDER BY version") | ||
| .fetch_all(pool) | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("查询已执行迁移失败: {e}")))?; | ||
| let sql = match version { | ||
| 1 => table_exists("connections"), | ||
| 2 => table_exists("saved_queries"), | ||
| 3 => column_exists("saved_queries", "database"), | ||
| 4 => column_exists("connections", "ssh_enabled"), | ||
| 5 => table_exists("ai_providers"), | ||
| 6 => table_exists("ai_conversations"), | ||
| 7 => table_exists("ai_messages"), | ||
| 8 => "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='index' AND name='idx_ai_providers_provider_type_unique')".to_string(), | ||
| // 009 SQL 全幂等,直接执行(与旧逻辑一致) | ||
| 9 => return Ok(false), | ||
| 10 => table_exists("sql_execution_logs"), | ||
| 11 => column_exists("connections", "ssl_mode"), | ||
| 12 => column_exists("connections", "mode"), | ||
| 13 => column_exists("connections", "auth_mode"), | ||
| 14 => column_exists("connections", "service_name"), | ||
|
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Skipped columns in checks migration_applied treats several multi-column migrations as “applied” when only a single column exists (e.g., versions 4, 11–14), so a partially-migrated/poisoned DB can skip the migration and still crash later when code queries missing columns. Worse, if some of the later columns already exist, the migration may run and then fail mid-way on “duplicate column”, leaving the DB in a broken state. Agent Prompt
|
||
| 15 => column_exists("connections", "auth_source"), | ||
| 16 => table_exists("redis_command_logs"), | ||
| _ => return Ok(true), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Unknown versions treated applied migration_applied returns Ok(true) for unknown migration versions, so if a new migration is added to MIGRATIONS but the match-arm isn’t updated, the migration will be silently skipped. That failure mode reintroduces “missing schema at runtime” issues without any obvious error. Agent Prompt
|
||
| }; | ||
|
|
||
| if applied.is_empty() { | ||
| let has_connections_table: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='connections')", | ||
| ) | ||
| sqlx::query_scalar::<_, bool>(&sql) | ||
| .fetch_one(pool) | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("检测旧数据库失败: {e}")))?; | ||
|
|
||
| if has_connections_table { | ||
| for &(version, _) in MIGRATIONS { | ||
| sqlx::query("INSERT OR IGNORE INTO schema_migrations (version) VALUES (?)") | ||
| .bind(version) | ||
| .execute(pool) | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("记录迁移版本 {version} 失败: {e}")))?; | ||
| } | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
|
||
| let applied_set: HashSet<i64> = applied.into_iter().collect(); | ||
| .map_err(|e| AppError::internal(format!("检查迁移 {version:03} 状态失败: {e}"))) | ||
| } | ||
|
|
||
| pub async fn run_migrations(pool: &Pool<Sqlite>) -> Result<(), AppError> { | ||
| for &(version, sql) in MIGRATIONS { | ||
| if applied_set.contains(&version) { | ||
| if migration_applied(pool, version).await? { | ||
| continue; | ||
| } | ||
| sqlx::query(sql) | ||
| .execute(pool) | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("迁移 {version:03} 执行失败: {e}")))?; | ||
| } | ||
|
|
||
| sqlx::query("INSERT INTO schema_migrations (version) VALUES (?)") | ||
| .bind(version) | ||
| .execute(pool) | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::run_migrations; | ||
|
|
||
| async fn mem_pool() -> sqlx::Pool<sqlx::Sqlite> { | ||
| sqlx::sqlite::SqlitePoolOptions::new() | ||
| .max_connections(1) | ||
| .connect("sqlite::memory:") | ||
| .await | ||
| .map_err(|e| AppError::internal(format!("记录迁移版本 {version} 失败: {e}")))?; | ||
| .expect("connect sqlite memory db") | ||
| } | ||
|
|
||
| Ok(()) | ||
| #[tokio::test] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: printf '%s\n' '--- diff ---'
git diff -- src-tauri/src/db/migrations.rs
printf '%s\n' '--- relevant source ---'
sed -n '1,210p' src-tauri/src/db/migrations.rs
printf '%s\n' '--- test configuration and nearby async tests ---'
rg -n -C 3 '#\[tokio::test\]|#\[test\]|cargo test --lib|tokio' src-tauri/Cargo.toml src-tauri/src/db/migrations.rs src-tauri/src -g '*.rs' -g 'Cargo.toml' | head -240Repository: codeErrorSleep/dbpaw Length of output: 21508 Use The 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| async fn fresh_db_applies_everything_and_is_idempotent() { | ||
| let pool = mem_pool().await; | ||
| run_migrations(&pool).await.expect("fresh db migrations"); | ||
| let auth_source: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM pragma_table_info('connections') WHERE name='auth_source')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(auth_source, "auth_source column should exist after fresh migration"); | ||
| let redis_logs: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='redis_command_logs')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(redis_logs, "redis_command_logs table should exist after fresh migration"); | ||
| run_migrations(&pool).await.expect("rerun should be idempotent"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn legacy_db_without_migration_records_gets_missing_columns() { | ||
| let pool = mem_pool().await; | ||
| // 模拟旧版本数据库:只有早期 connections 表,没有 schema_migrations 表 | ||
| sqlx::query( | ||
| "CREATE TABLE connections ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, uuid TEXT, type TEXT, | ||
| name TEXT, host TEXT, port INTEGER, database TEXT, username TEXT, | ||
| password TEXT, ssl INTEGER DEFAULT 0, file_path TEXT, | ||
| created_at TEXT, updated_at TEXT | ||
| )", | ||
| ) | ||
| .execute(&pool) | ||
| .await | ||
| .expect("create legacy connections table"); | ||
|
|
||
| run_migrations(&pool).await.expect("legacy db migrations"); | ||
| let auth_source: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM pragma_table_info('connections') WHERE name='auth_source')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(auth_source, "legacy db should be healed with auth_source column"); | ||
| let redis_logs: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='redis_command_logs')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(redis_logs, "legacy db should be healed with redis_command_logs table"); | ||
|
|
||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn poisoned_db_with_all_records_marked_gets_healed() { | ||
| let pool = mem_pool().await; | ||
| // 复现旧 bug:schema_migrations 全标记,但 015 列 / 016 表缺失 | ||
| sqlx::query( | ||
| "CREATE TABLE connections ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, uuid TEXT, type TEXT, | ||
| name TEXT, host TEXT, port INTEGER, database TEXT, username TEXT, | ||
| password TEXT, ssl INTEGER DEFAULT 0, file_path TEXT, | ||
| ssl_mode TEXT, mode TEXT, auth_mode TEXT, service_name TEXT, | ||
| created_at TEXT, updated_at TEXT | ||
| )", | ||
| ) | ||
| .execute(&pool) | ||
| .await | ||
| .expect("create poisoned connections table"); | ||
| sqlx::query( | ||
| "CREATE TABLE schema_migrations (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (datetime('now')))", | ||
| ) | ||
| .execute(&pool) | ||
| .await | ||
| .expect("create schema_migrations"); | ||
| for v in 1..=16i64 { | ||
| sqlx::query("INSERT INTO schema_migrations (version) VALUES (?)") | ||
| .bind(v) | ||
| .execute(&pool) | ||
| .await | ||
| .expect("mark all migrations applied"); | ||
| } | ||
|
|
||
| run_migrations(&pool).await.expect("poisoned db migrations"); | ||
| let auth_source: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM pragma_table_info('connections') WHERE name='auth_source')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(auth_source, "poisoned db should be healed with auth_source column"); | ||
| let redis_logs: bool = sqlx::query_scalar( | ||
| "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='redis_command_logs')", | ||
| ) | ||
| .fetch_one(&pool) | ||
| .await | ||
| .unwrap(); | ||
| assert!(redis_logs, "poisoned db should be healed with redis_command_logs table"); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. Indexes not validated
🐞 Bug➹ PerformanceAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools