update hifi db methods to return, rather than quash, sqlite errors

This commit is contained in:
elmerohueso 2026-04-28 18:01:25 -06:00
parent 788b7011d0
commit eedd040318

View file

@ -11609,95 +11609,68 @@ class MusicDatabase:
def get_hifi_instances(self) -> List[Dict[str, Any]]: def get_hifi_instances(self) -> List[Dict[str, Any]]:
"""Get all enabled HiFi instances ordered by priority.""" """Get all enabled HiFi instances ordered by priority."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute("SELECT url, priority, enabled FROM hifi_instances WHERE enabled = 1 ORDER BY priority ASC, id ASC")
cursor.execute("SELECT url, priority, enabled FROM hifi_instances WHERE enabled = 1 ORDER BY priority ASC, id ASC") return [dict(row) for row in cursor.fetchall()]
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting HiFi instances: {e}")
return []
def get_all_hifi_instances(self) -> List[Dict[str, Any]]: def get_all_hifi_instances(self) -> List[Dict[str, Any]]:
"""Get all HiFi instances (including disabled) ordered by priority.""" """Get all HiFi instances (including disabled) ordered by priority."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute("SELECT url, priority, enabled FROM hifi_instances ORDER BY priority ASC, id ASC")
cursor.execute("SELECT url, priority, enabled FROM hifi_instances ORDER BY priority ASC, id ASC") return [dict(row) for row in cursor.fetchall()]
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting all HiFi instances: {e}")
return []
def add_hifi_instance(self, url: str, priority: int = 0) -> bool: def add_hifi_instance(self, url: str, priority: int = 0) -> bool:
"""Add a new HiFi instance.""" """Add a new HiFi instance."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute(
cursor.execute( "INSERT OR IGNORE INTO hifi_instances (url, priority, enabled) VALUES (?, ?, 1)",
"INSERT OR IGNORE INTO hifi_instances (url, priority, enabled) VALUES (?, ?, 1)", (url, priority)
(url, priority) )
) conn.commit()
conn.commit() return cursor.rowcount > 0
return cursor.rowcount > 0
except Exception as e:
logger.error(f"Error adding HiFi instance: {e}")
return False
def remove_hifi_instance(self, url: str) -> bool: def remove_hifi_instance(self, url: str) -> bool:
"""Remove a HiFi instance.""" """Remove a HiFi instance."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute("DELETE FROM hifi_instances WHERE url = ?", (url,))
cursor.execute("DELETE FROM hifi_instances WHERE url = ?", (url,)) conn.commit()
conn.commit() return cursor.rowcount > 0
return cursor.rowcount > 0
except Exception as e:
logger.error(f"Error removing HiFi instance: {e}")
return False
def toggle_hifi_instance(self, url: str, enabled: bool) -> bool: def toggle_hifi_instance(self, url: str, enabled: bool) -> bool:
"""Enable or disable a HiFi instance.""" """Enable or disable a HiFi instance."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute("UPDATE hifi_instances SET enabled = ? WHERE url = ?", (1 if enabled else 0, url))
cursor.execute("UPDATE hifi_instances SET enabled = ? WHERE url = ?", (1 if enabled else 0, url)) conn.commit()
conn.commit() return cursor.rowcount > 0
return cursor.rowcount > 0
except Exception as e:
logger.error(f"Error toggling HiFi instance: {e}")
return False
def reorder_hifi_instances(self, urls: List[str]) -> bool: def reorder_hifi_instances(self, urls: List[str]) -> bool:
"""Update priorities based on the given URL order.""" """Update priorities based on the given URL order."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() for i, url in enumerate(urls):
for i, url in enumerate(urls): cursor.execute("UPDATE hifi_instances SET priority = ? WHERE url = ?", (i, url))
cursor.execute("UPDATE hifi_instances SET priority = ? WHERE url = ?", (i, url)) conn.commit()
conn.commit() return True
return True
except Exception as e:
logger.error(f"Error reordering HiFi instances: {e}")
return False
def seed_hifi_instances(self, default_urls: List[str]) -> None: def seed_hifi_instances(self, default_urls: List[str]) -> None:
"""Insert default instances if the table is empty.""" """Insert default instances if the table is empty."""
try: conn = self._get_connection()
conn = self._get_connection() cursor = conn.cursor()
cursor = conn.cursor() cursor.execute("SELECT COUNT(*) as cnt FROM hifi_instances")
cursor.execute("SELECT COUNT(*) as cnt FROM hifi_instances") count = cursor.fetchone()['cnt']
count = cursor.fetchone()['cnt'] if count == 0:
if count == 0: for i, url in enumerate(default_urls):
for i, url in enumerate(default_urls): cursor.execute(
cursor.execute( "INSERT OR IGNORE INTO hifi_instances (url, priority, enabled) VALUES (?, ?, 1)",
"INSERT OR IGNORE INTO hifi_instances (url, priority, enabled) VALUES (?, ?, 1)", (url, i)
(url, i) )
) conn.commit()
conn.commit() logger.info(f"Seeded {len(default_urls)} default HiFi instances")
logger.info(f"Seeded {len(default_urls)} default HiFi instances")
except Exception as e:
logger.error(f"Error seeding HiFi instances: {e}")
# Thread-safe singleton pattern for database access # Thread-safe singleton pattern for database access
_database_instances: Dict[int, MusicDatabase] = {} # Thread ID -> Database instance _database_instances: Dict[int, MusicDatabase] = {} # Thread ID -> Database instance