fix(migration): robust database migration for show_context_icon

Problem:
- Users updating from previous versions would crash on startup
- Table created with new column caused "no column named show_context_icon" error
- Existing users had old schema without the column

Solution:
- Step 1: Create table with old schema (backwards compatible)
- Step 2: Check if show_context_icon column exists using pragma_table_info
- Step 3: Add column ONLY if it doesn't exist (safe migration)
- Step 4: Insert default row

This ensures:
✅ New installs work (table created fresh)
✅ Existing users migrate seamlessly (column added, settings preserved)
✅ No data loss
✅ Idempotent (can run multiple times safely)

Tested scenarios:
- Fresh install (no DB) → creates table, adds column, inserts defaults
  Result: id=1, autoplay_mode='continue', show_context_icon=1
- Existing DB without column → adds column, preserves user settings
  Result: id=1, autoplay_mode='track_only' (preserved), show_context_icon=1
- Existing DB with column → no changes (idempotent)

Migration logs added for debugging.

cp
This commit is contained in:
vicrodh committed 2026-01-18 03:57:28 -06:00
1 parent 584650fee1
commit fdf288b01e
1 file changed
+33 -9
+33 -9
View File
@@ -5,6 +5,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use log::info;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
@@ -69,21 +70,44 @@ impl PlaybackPreferencesStore {
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open playback preferences database: {}", e))?;
// Step 1: Create table with old schema (for new installs) or do nothing if exists
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS playback_preferences (
id INTEGER PRIMARY KEY CHECK (id = 1),
autoplay_mode TEXT NOT NULL DEFAULT 'continue',
show_context_icon INTEGER NOT NULL DEFAULT 1
);
INSERT OR IGNORE INTO playback_preferences (id, autoplay_mode, show_context_icon)
VALUES (1, 'continue', 1);"
autoplay_mode TEXT NOT NULL DEFAULT 'continue'
);"
).map_err(|e| format!("Failed to create playback preferences table: {}", e))?;
// Migration: Add show_context_icon column if it doesn't exist
let _ = conn.execute(
"ALTER TABLE playback_preferences ADD COLUMN show_context_icon INTEGER NOT NULL DEFAULT 1",
// Step 2: Check if show_context_icon column exists
let column_exists: bool = conn
.query_row(
"SELECT COUNT(*) FROM pragma_table_info('playback_preferences') WHERE name='show_context_icon'",
[],
|row| {
let count: i32 = row.get(0)?;
Ok(count > 0)
}
)
.unwrap_or(false);
info!("[PlaybackPrefs] Column show_context_icon exists: {}", column_exists);
// Step 3: Add column if it doesn't exist (migration for existing users)
if !column_exists {
info!("[PlaybackPrefs] Migrating: adding show_context_icon column");
conn.execute(
"ALTER TABLE playback_preferences ADD COLUMN show_context_icon INTEGER NOT NULL DEFAULT 1",
[]
).map_err(|e| format!("Failed to add show_context_icon column: {}", e))?;
info!("[PlaybackPrefs] Migration successful");
}
// Step 4: Insert default row if it doesn't exist
conn.execute(
"INSERT OR IGNORE INTO playback_preferences (id, autoplay_mode, show_context_icon)
VALUES (1, 'continue', 1)",
[]
);
).map_err(|e| format!("Failed to insert default preferences: {}", e))?;
Ok(Self { conn })
}