From 9341e9ee19f68bcd859b5a52f563af4751251aaa Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Tue, 3 Feb 2026 17:11:38 -0600 Subject: [PATCH] fix: migrate cached_artists table to include locale column Old database schemas created before locale support was added caused "no such column: locale" errors when fetching artists. The migration detects this and drops the old table so it gets recreated with the correct schema. Fixes Top Artists section not appearing in Home view. --- src-tauri/src/api_cache/mod.rs | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src-tauri/src/api_cache/mod.rs b/src-tauri/src/api_cache/mod.rs index 49e4d2464..5d9562718 100644 --- a/src-tauri/src/api_cache/mod.rs +++ b/src-tauri/src/api_cache/mod.rs @@ -52,6 +52,9 @@ impl ApiCache { } fn init(&self) -> Result<(), String> { + // Run migrations first + self.migrate_artists_table()?; + self.conn .execute_batch( r#" @@ -83,6 +86,46 @@ impl ApiCache { Ok(()) } + /// Migrate old cached_artists table (without locale) to new schema + fn migrate_artists_table(&self) -> Result<(), String> { + // Check if cached_artists table exists + let table_exists: bool = self + .conn + .query_row( + "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='cached_artists'", + [], + |row| row.get::<_, i32>(0), + ) + .map(|count| count > 0) + .unwrap_or(false); + + if !table_exists { + return Ok(()); // Table doesn't exist, will be created fresh + } + + // Check if locale column exists + let has_locale: bool = self + .conn + .prepare("PRAGMA table_info(cached_artists)") + .map_err(|e| format!("Failed to query table info: {}", e))? + .query_map([], |row| row.get::<_, String>(1)) + .map_err(|e| format!("Failed to read table info: {}", e))? + .filter_map(Result::ok) + .any(|col| col == "locale"); + + if has_locale { + return Ok(()); // Already migrated + } + + // Old table without locale column - drop it (cache can be regenerated) + log::info!("Migrating cached_artists table: dropping old schema without locale column"); + self.conn + .execute("DROP TABLE cached_artists", []) + .map_err(|e| format!("Failed to drop old cached_artists table: {}", e))?; + + Ok(()) + } + fn current_timestamp() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH)