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.
This commit is contained in:
vicrodh committed 2026-02-03 17:11:38 -06:00
1 parent 2818ac4d29
commit 9341e9ee19
1 file changed
+43
+43
View File
@@ -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)