mirror of
https://github.com/navidrome/navidrome.git
synced 2026-09-13 22:24:14 -04:00
* feat(cli): add missing file list and remap subcommands Signed-off-by: zerovox <933064+zerovox@users.noreply.github.com> * fix: prevent remapping from dropping participants on target track * fix: after remapping, refresh stats synchronously * fix: only move album annotations if moving a track would empty the old album * fix(persistence): keep the new item's annotation when reassigning onto an item the user already annotated ReassignAnnotation was a plain UPDATE; the annotation table is unique on (user_id, item_id, item_type), so when a user had annotated both items the statement aborted and none of the rows moved. In the scanner that surfaced as a warning; in the missing-file remap it rolled back the whole operation. UPDATE OR IGNORE moves what it can and leaves the conflicting rows for GC. * fix(core): keep the target track's history when remapping a missing file onto it The remap discards the target's row, and GC then dropped its play counts, stars, ratings, bookmarks and every playlist entry pointing at it. That is harmless in the scanner, whose target was imported seconds earlier, but the CLI lets the user pick any existing track. Move those references onto the surviving id first; where a user already has a row for both, theirs on the missing file wins. * fix(persistence): stop FindByPaths dropping plain paths that contain a colon Any colon was taken as the libraryID separator, and a non-numeric prefix made the whole path vanish from the lookup. 'missing fix' then rejected the very paths 'missing list' printed, and M3U imports silently skipped such tracks. Only a numeric prefix qualifies a path now. * perf(cli): stream 'missing list' instead of loading every missing file into memory GetAll materialised the whole result set before a single row was written; on a library with 97k missing files that peaked at 1.28 GB of RSS. Iterate the repository cursor and write rows as they arrive. * refactor(core): tidy the missing-file remap Drop the log lines copied from deleteMissing that still said 'after deleting missing files', the debug-on-success branches, and the what-comments; build the affected album list without slice helpers. * fix(cli): move path to the last column of 'missing list' Path is the only variable-width field, so leading with it misaligns every row that follows. Applies to both csv and json. * fix(persistence): also try a numeric colon prefix as a plain path '1999: A Different Life/01.mp3' parsed as library 1999 plus a truncated path and matched nothing. The prefix is ambiguous, so search both ways. Also buffer the json branch of 'missing list', which wrote a syscall per row. * fix(persistence): move scrobbles and buffered scrobbles off a discarded media file Both tables carry ON DELETE CASCADE on media_file_id, so 'missing fix' deleting the target erased its play history and dropped scrobbles still waiting on an external service. scrobble_buffer needs OR IGNORE for its unique (user_id, service, media_file_id, play_time). * 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. * fix(persistence): recompute the cached average rating inside ReassignAnnotation Moving annotation rows always changes the new item's rating population, so the recompute belongs with the move rather than at each call site. Covers the album reassign in the remap and the two scanner sites, and replaces the explicit call ReassignReferences was making. Album was the worse case: rate an album, move its files, and 'missing fix' handed the rating to an album still caching an average of 0. * fix(cli): let libraryID:path win over a file literally named like one FindByPaths searches a numeric-prefixed reference both ways, so a top-level file named '1:foo.mp3' can tie with library 1's 'foo.mp3'. The CLI then rejected the reference as ambiguous while advising the exact syntax the caller had used. Also disambiguates the same path in two libraries, which is what the qualified form is for. --------- Signed-off-by: zerovox <933064+zerovox@users.noreply.github.com> Co-authored-by: Deluan Quintão <deluan@navidrome.org>
82 lines
2.9 KiB
Go
82 lines
2.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("writeMissingList", func() {
|
|
cursor := func(err error, mfs ...model.MediaFile) model.MediaFileCursor {
|
|
return func(yield func(model.MediaFile, error) bool) {
|
|
for _, mf := range mfs {
|
|
if !yield(mf, nil) {
|
|
return
|
|
}
|
|
}
|
|
if err != nil {
|
|
yield(model.MediaFile{}, err)
|
|
}
|
|
}
|
|
}
|
|
song := model.MediaFile{ID: "1", LibraryID: 1, Path: "Bach: Goldberg/01.mp3", Title: "Aria", Album: "Goldberg", Artist: "Bach"}
|
|
|
|
It("writes csv with a header, quoting as needed", func() {
|
|
var out strings.Builder
|
|
Expect(writeMissingList(&out, "csv", cursor(nil, song))).To(Succeed())
|
|
Expect(out.String()).To(Equal("id,library id,title,album,artist,path\n1,1,Aria,Goldberg,Bach,Bach: Goldberg/01.mp3\n"))
|
|
})
|
|
|
|
It("writes a json array", func() {
|
|
var out strings.Builder
|
|
Expect(writeMissingList(&out, "json", cursor(nil, song, song))).To(Succeed())
|
|
Expect(out.String()).To(MatchJSON(`[
|
|
{"id":"1","libraryId":1,"path":"Bach: Goldberg/01.mp3","title":"Aria","album":"Goldberg","artist":"Bach"},
|
|
{"id":"1","libraryId":1,"path":"Bach: Goldberg/01.mp3","title":"Aria","album":"Goldberg","artist":"Bach"}
|
|
]`))
|
|
})
|
|
|
|
It("writes an empty json array when nothing is missing", func() {
|
|
var out strings.Builder
|
|
Expect(writeMissingList(&out, "json", cursor(nil))).To(Succeed())
|
|
Expect(out.String()).To(MatchJSON(`[]`))
|
|
})
|
|
|
|
It("returns the cursor's error", func() {
|
|
var out strings.Builder
|
|
Expect(writeMissingList(&out, "csv", cursor(errors.New("boom"), song))).To(MatchError("boom"))
|
|
})
|
|
})
|
|
|
|
var _ = Describe("preferQualified", func() {
|
|
target := model.MediaFile{ID: "want", LibraryID: 1, Path: "foo.mp3"}
|
|
decoy := model.MediaFile{ID: "decoy", LibraryID: 1, Path: "1:foo.mp3"}
|
|
|
|
It("picks the library-qualified match over a literal path that looks like one", func() {
|
|
Expect(preferQualified("1:foo.mp3", model.MediaFiles{target, decoy})).To(Equal(model.MediaFiles{target}))
|
|
})
|
|
|
|
It("picks the named library when the same path exists in two", func() {
|
|
other := model.MediaFile{ID: "other", LibraryID: 2, Path: "foo.mp3"}
|
|
Expect(preferQualified("1:foo.mp3", model.MediaFiles{target, other})).To(Equal(model.MediaFiles{target}))
|
|
})
|
|
|
|
It("leaves an unqualified reference ambiguous", func() {
|
|
both := model.MediaFiles{target, {ID: "other", LibraryID: 2, Path: "foo.mp3"}}
|
|
Expect(preferQualified("foo.mp3", both)).To(Equal(both))
|
|
})
|
|
|
|
It("leaves it alone when the prefix is not a library id", func() {
|
|
both := model.MediaFiles{decoy, {ID: "other", LibraryID: 2, Path: "1:foo.mp3"}}
|
|
Expect(preferQualified("x:foo.mp3", both)).To(Equal(both))
|
|
})
|
|
|
|
It("leaves it alone when no candidate matches the qualified form", func() {
|
|
both := model.MediaFiles{decoy, {ID: "other", LibraryID: 2, Path: "1:foo.mp3"}}
|
|
Expect(preferQualified("9:nope.mp3", both)).To(Equal(both))
|
|
})
|
|
})
|