This commit is contained in:
nose
2025-11-27 18:35:06 -08:00
parent 9eff65d1af
commit ed417c8200
6 changed files with 143 additions and 93 deletions

View File

@@ -163,13 +163,25 @@ class LocalLibraryDB:
# Use check_same_thread=False to allow multi-threaded access
# This is safe because we're not sharing connections across threads;
# each thread will get its own cursor
self.connection = sqlite3.connect(str(self.db_path), check_same_thread=False)
# Set a generous timeout to avoid "database is locked" errors during heavy concurrency
self.connection = sqlite3.connect(str(self.db_path), check_same_thread=False, timeout=60.0)
self.connection.row_factory = sqlite3.Row
# Enable Write-Ahead Logging (WAL) for better concurrency
self.connection.execute("PRAGMA journal_mode=WAL")
# Enable foreign keys
self.connection.execute("PRAGMA foreign_keys = ON")
self._create_tables()
logger.info(f"Database initialized at {self.db_path}")
except Exception as e:
logger.error(f"Failed to initialize database: {e}", exc_info=True)
if self.connection:
try:
self.connection.close()
except Exception:
pass
self.connection = None
raise
def _create_tables(self) -> None:
@@ -1492,6 +1504,19 @@ class LocalLibrarySearchOptimizer:
except Exception as e:
logger.error(f"Failed to get playlist ID {playlist_id}: {e}")
return None
def delete_playlist(self, playlist_id: int) -> bool:
"""Delete a playlist by ID."""
if not self.db:
return False
try:
cursor = self.db.connection.cursor()
cursor.execute("DELETE FROM playlists WHERE id = ?", (playlist_id,))
self.db.connection.commit()
return cursor.rowcount > 0
except Exception as e:
logger.error(f"Failed to delete playlist ID {playlist_id}: {e}")
return False
if not self.db:
return []
return self.db.search_by_tag(tag, limit)