fix(persistence): recompute the cached average rating after merging annotations

Merging the discarded row's annotations grows the rating population of the
surviving track, so media_file.average_rating no longer matched what the
annotation rows say. Only reachable since the remap started merging those
rows instead of deleting them.
This commit is contained in:
Deluan 2026-09-12 10:50:44 -04:00
parent f5996711f4
commit 1d592af243
2 changed files with 16 additions and 0 deletions

View File

@ -404,6 +404,10 @@ func (r *mediaFileRepository) ReassignReferences(prevID, newID string) error {
if err := r.ReassignAnnotation(prevID, newID); err != nil {
return fmt.Errorf("reassigning annotations: %w", err)
}
// The merged rows change the rating population, so the cached average no longer matches
if err := r.updateAvgRating(newID); err != nil {
return fmt.Errorf("recomputing average rating: %w", err)
}
if err := r.reassignBookmark(prevID, newID); err != nil {
return fmt.Errorf("reassigning bookmarks: %w", err)
}

View File

@ -989,6 +989,18 @@ var _ = Describe("MediaRepository", func() {
Expect(entry.MediaFile.ID).To(Equal(next.ID))
})
It("recomputes the average rating after merging another user's annotation", func() {
other := NewMediaFileRepository(request.WithUser(log.NewContext(context.TODO()), model.User{ID: "2222"}), GetDBXBuilder())
Expect(mr.SetRating(5, next.ID)).To(Succeed())
Expect(other.SetRating(3, prev.ID)).To(Succeed())
Expect(mr.ReassignReferences(prev.ID, next.ID)).To(Succeed())
got, err := mr.Get(next.ID)
Expect(err).ToNot(HaveOccurred())
Expect(got.AverageRating).To(Equal(4.0))
})
It("keeps the new id's own annotation and bookmark when both exist", func() {
Expect(mr.SetRating(5, prev.ID)).To(Succeed())
Expect(mr.SetRating(1, next.ID)).To(Succeed())