Improve performance of save_playlist

This commit is contained in:
Keith Edmunds 2022-12-22 17:41:46 +00:00
parent 784d036bb7
commit be4f19757c
2 changed files with 24 additions and 3 deletions

View File

@ -593,6 +593,27 @@ class PlaylistRows(Base):
.values(row_number=PlaylistRows.row_number + move_by)
)
@staticmethod
def indexed_by_row(session: Session, playlist_id: int) -> dict:
"""
Return a dictionary of playlist_rows indexed by row number for
the passed playlist_id.
"""
plrs = session.execute(
select(PlaylistRows)
.where(
PlaylistRows.playlist_id == playlist_id,
)
.order_by(PlaylistRows.row_number)
).scalars().all()
result = {}
for plr in plrs:
result[plr.row_number] = plr
return result
class Settings(Base):
"""Manage settings"""

View File

@ -827,11 +827,11 @@ class PlaylistTab(QTableWidget):
the row number is correct (in case tracks have been reordered).
"""
plr_dict = PlaylistRows.indexed_by_row(session, self.playlist_id)
for row in range(self.rowCount()):
plr = session.get(PlaylistRows, self._get_playlistrow_id(row))
# Set the row number and playlist id (even if correct)
plr.row_number = row
plr.playlist_id = self.playlist_id
plr_dict[row].row_number = row
plr_dict[row].playlist_id = self.playlist_id
# Any rows in the database with a row_number higher that the
# current value of 'row' should not be there. Commit session