Skip to content

Commit

Permalink
fix db query for album tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Aug 29, 2024
1 parent 41cd503 commit f9d2a2a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions music_assistant/server/controllers/media/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ async def get_library_album_tracks(
item_id: str | int,
) -> list[Track]:
"""Return in-database album tracks for the given database album."""
subquery = f"SELECT track_id FROM {DB_TABLE_ALBUM_TRACKS} WHERE album_id = {item_id}"
query = f"WHERE tracks.item_id in ({subquery})"
return await self.mass.music.tracks._get_library_items_by_query(extra_query_parts=[query])
return await self.mass.music.tracks._get_library_items_by_query(
extra_query_parts=[f"WHERE album_tracks.album_id = {item_id}"],
)

async def _add_library_item(self, item: Album) -> int:
"""Add a new record to the database."""
Expand Down
1 change: 1 addition & 0 deletions music_assistant/server/controllers/media/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ async def _get_library_items_by_query(
if query_parts:
sql_query += " WHERE " + " AND ".join(query_parts)
# build final query
sql_query += f" GROUP BY {self.db_table}.item_id"
if order_by:
if sort_key := SORT_KEYS.get(order_by):
sql_query += f" ORDER BY {sort_key}"
Expand Down
6 changes: 4 additions & 2 deletions music_assistant/server/controllers/media/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ def __init__(self, *args, **kwargs) -> None:
'disc_number', album_tracks.disc_number,
'track_number', album_tracks.track_number,
'images', json_extract(albums.metadata, '$.images')
) FROM albums JOIN album_tracks on album_tracks.track_id = tracks.item_id WHERE albums.item_id = album_tracks.album_id) AS track_album
FROM tracks""" # noqa: E501
) FROM albums WHERE albums.item_id = album_tracks.album_id) AS track_album
FROM tracks
LEFT JOIN album_tracks on album_tracks.track_id = tracks.item_id
""" # noqa: E501
# register (extra) api handlers
api_base = self.api_base
self.mass.register_api_command(f"music/{api_base}/track_versions", self.versions)
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def setup(self) -> None:
"""Perform async initialization."""
self._db = await aiosqlite.connect(self.db_path)
self._db.row_factory = aiosqlite.Row
await self.execute("PRAGMA analysis_limit=400;")
await self.execute("PRAGMA analysis_limit=10000;")
await self.execute("PRAGMA optimize;")
await self.commit()

Expand Down

0 comments on commit f9d2a2a

Please sign in to comment.