mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 18:37:09 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
391 lines
14 KiB
Go
391 lines
14 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
var _ = Describe("FolderRepository", func() {
|
|
var repo model.FolderRepository
|
|
var ctx context.Context
|
|
var conn *dbx.DB
|
|
var testLib, otherLib model.Library
|
|
|
|
BeforeEach(func() {
|
|
ctx = request.WithUser(log.NewContext(context.TODO()), model.User{ID: "userid"})
|
|
conn = GetDBXBuilder()
|
|
repo = newFolderRepository(conn)
|
|
|
|
// Use existing library ID 1 from test fixtures
|
|
libRepo := NewLibraryRepository(conn)
|
|
lib, err := libRepo.Get(ctx, 1)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
testLib = *lib
|
|
|
|
// Create a second library with its own folder to verify isolation
|
|
otherLib = model.Library{Name: "Other Library", Path: "/other/path"}
|
|
Expect(libRepo.Put(ctx, &otherLib)).To(Succeed())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
// Clean up only test folders created by our tests (paths starting with "Test")
|
|
// This prevents interference with fixture data needed by other tests
|
|
_, _ = conn.NewQuery("DELETE FROM folder WHERE library_id = 1 AND path LIKE 'Test%'").Execute()
|
|
_, _ = conn.NewQuery(fmt.Sprintf("DELETE FROM library WHERE id = %d", otherLib.ID)).Execute()
|
|
})
|
|
|
|
Describe("folderSubtreeFilter", func() {
|
|
var parent, child, grandchild, other *model.Folder
|
|
|
|
matching := func(paths ...string) []string {
|
|
GinkgoHelper()
|
|
folders, err := repo.GetAll(ctx, model.QueryOptions{Filters: folderSubtreeFilter(testLib, paths)})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
return slice.Map(folders, func(f model.Folder) string { return f.ID })
|
|
}
|
|
|
|
BeforeEach(func() {
|
|
parent = model.NewFolder(testLib, "TestSubtree")
|
|
child = model.NewFolder(testLib, "TestSubtree/Child")
|
|
grandchild = model.NewFolder(testLib, "TestSubtree/Child/Grandchild")
|
|
other = model.NewFolder(testLib, "TestSubtreeOther")
|
|
for _, f := range []*model.Folder{parent, child, grandchild, other} {
|
|
Expect(repo.Put(ctx, f)).To(Succeed())
|
|
}
|
|
DeferCleanup(func() {
|
|
_, _ = conn.NewQuery("DELETE FROM folder WHERE name LIKE 'TestSubtree%' OR path LIKE 'TestSubtree%'").Execute()
|
|
})
|
|
})
|
|
|
|
It("matches a folder and all its descendants", func() {
|
|
Expect(matching("TestSubtree")).To(ConsistOf(parent.ID, child.ID, grandchild.ID))
|
|
})
|
|
|
|
It("matches the descendants of a nested slash-form path", func() {
|
|
Expect(matching("TestSubtree/Child")).To(ConsistOf(child.ID, grandchild.ID))
|
|
})
|
|
|
|
It("matches the whole library for the root path", func() {
|
|
Expect(matching(".")).To(ContainElements(parent.ID, child.ID, grandchild.ID, other.ID))
|
|
})
|
|
})
|
|
|
|
Describe("GetFolderUpdateInfo", func() {
|
|
Context("with no target paths", func() {
|
|
It("returns all folders in the library", func() {
|
|
// Create test folders with unique names to avoid conflicts
|
|
folder1 := model.NewFolder(testLib, "TestGetLastUpdates/Folder1")
|
|
folder2 := model.NewFolder(testLib, "TestGetLastUpdates/Folder2")
|
|
|
|
err := repo.Put(ctx, folder1)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
err = repo.Put(ctx, folder2)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
otherFolder := model.NewFolder(otherLib, "TestOtherLib/Folder")
|
|
err = repo.Put(ctx, otherFolder)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Query all folders (no target paths) - should only return folders from testLib
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// Should include folders from testLib
|
|
Expect(results).To(HaveKey(folder1.ID))
|
|
Expect(results).To(HaveKey(folder2.ID))
|
|
// Should NOT include folders from other library
|
|
Expect(results).ToNot(HaveKey(otherFolder.ID))
|
|
})
|
|
})
|
|
|
|
Context("with specific target paths", func() {
|
|
It("returns folder info for existing folders", func() {
|
|
// Create test folders with unique names
|
|
folder1 := model.NewFolder(testLib, "TestSpecific/Rock")
|
|
folder2 := model.NewFolder(testLib, "TestSpecific/Jazz")
|
|
folder3 := model.NewFolder(testLib, "TestSpecific/Classical")
|
|
|
|
err := repo.Put(ctx, folder1)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
err = repo.Put(ctx, folder2)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
err = repo.Put(ctx, folder3)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Query specific paths
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestSpecific/Rock", "TestSpecific/Classical")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(2))
|
|
|
|
// Verify folder IDs are in results
|
|
Expect(results).To(HaveKey(folder1.ID))
|
|
Expect(results).To(HaveKey(folder3.ID))
|
|
Expect(results).ToNot(HaveKey(folder2.ID))
|
|
|
|
// Verify update info is populated
|
|
Expect(results[folder1.ID].UpdatedAt).ToNot(BeZero())
|
|
Expect(results[folder1.ID].Hash).To(Equal(folder1.Hash))
|
|
})
|
|
|
|
It("includes all child folders when querying parent", func() {
|
|
// Create a parent folder with multiple children
|
|
parent := model.NewFolder(testLib, "TestParent/Music")
|
|
child1 := model.NewFolder(testLib, "TestParent/Music/Rock/Queen")
|
|
child2 := model.NewFolder(testLib, "TestParent/Music/Jazz")
|
|
otherParent := model.NewFolder(testLib, "TestParent2/Music/Jazz")
|
|
|
|
Expect(repo.Put(ctx, parent)).To(Succeed())
|
|
Expect(repo.Put(ctx, child1)).To(Succeed())
|
|
Expect(repo.Put(ctx, child2)).To(Succeed())
|
|
|
|
// Query the parent folder - should return parent and all children
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestParent/Music")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(3))
|
|
Expect(results).To(HaveKey(parent.ID))
|
|
Expect(results).To(HaveKey(child1.ID))
|
|
Expect(results).To(HaveKey(child2.ID))
|
|
Expect(results).ToNot(HaveKey(otherParent.ID))
|
|
})
|
|
|
|
It("excludes children from other libraries", func() {
|
|
// Create parent in testLib
|
|
parent := model.NewFolder(testLib, "TestIsolation/Parent")
|
|
child := model.NewFolder(testLib, "TestIsolation/Parent/Child")
|
|
|
|
Expect(repo.Put(ctx, parent)).To(Succeed())
|
|
Expect(repo.Put(ctx, child)).To(Succeed())
|
|
|
|
// Create similar path in other library
|
|
otherParent := model.NewFolder(otherLib, "TestIsolation/Parent")
|
|
otherChild := model.NewFolder(otherLib, "TestIsolation/Parent/Child")
|
|
|
|
Expect(repo.Put(ctx, otherParent)).To(Succeed())
|
|
Expect(repo.Put(ctx, otherChild)).To(Succeed())
|
|
|
|
// Query should only return folders from testLib
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestIsolation/Parent")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(2))
|
|
Expect(results).To(HaveKey(parent.ID))
|
|
Expect(results).To(HaveKey(child.ID))
|
|
Expect(results).ToNot(HaveKey(otherParent.ID))
|
|
Expect(results).ToNot(HaveKey(otherChild.ID))
|
|
})
|
|
|
|
It("excludes missing children when querying parent", func() {
|
|
// Create parent and children, mark one as missing
|
|
parent := model.NewFolder(testLib, "TestMissingChild/Parent")
|
|
child1 := model.NewFolder(testLib, "TestMissingChild/Parent/Child1")
|
|
child2 := model.NewFolder(testLib, "TestMissingChild/Parent/Child2")
|
|
child2.Missing = true
|
|
|
|
Expect(repo.Put(ctx, parent)).To(Succeed())
|
|
Expect(repo.Put(ctx, child1)).To(Succeed())
|
|
Expect(repo.Put(ctx, child2)).To(Succeed())
|
|
|
|
// Query parent - should only return parent and non-missing child
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestMissingChild/Parent")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(2))
|
|
Expect(results).To(HaveKey(parent.ID))
|
|
Expect(results).To(HaveKey(child1.ID))
|
|
Expect(results).ToNot(HaveKey(child2.ID))
|
|
})
|
|
|
|
It("handles mix of existing and non-existing target paths", func() {
|
|
// Create folders for one path but not the other
|
|
existingParent := model.NewFolder(testLib, "TestMixed/Exists")
|
|
existingChild := model.NewFolder(testLib, "TestMixed/Exists/Child")
|
|
|
|
Expect(repo.Put(ctx, existingParent)).To(Succeed())
|
|
Expect(repo.Put(ctx, existingChild)).To(Succeed())
|
|
|
|
// Query both existing and non-existing paths
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestMixed/Exists", "TestMixed/DoesNotExist")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(2))
|
|
Expect(results).To(HaveKey(existingParent.ID))
|
|
Expect(results).To(HaveKey(existingChild.ID))
|
|
})
|
|
|
|
It("handles empty folder path as root", func() {
|
|
// Test querying for root folder without creating it (fixtures should have one)
|
|
rootFolderID := model.FolderID(testLib, ".")
|
|
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// Should return the root folder if it exists
|
|
if len(results) > 0 {
|
|
Expect(results).To(HaveKey(rootFolderID))
|
|
}
|
|
})
|
|
|
|
It("returns empty map for non-existent folders", func() {
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "NonExistent/Path")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(BeEmpty())
|
|
})
|
|
|
|
It("skips missing folders", func() {
|
|
// Create a folder and mark it as missing
|
|
folder := model.NewFolder(testLib, "TestMissing/Folder")
|
|
folder.Missing = true
|
|
err := repo.Put(ctx, folder)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
results, err := repo.GetFolderUpdateInfo(ctx, testLib, "TestMissing/Folder")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(BeEmpty())
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("HasAudioOutsideFolders", func() {
|
|
var albumRoot, disc1, disc2 *model.Folder
|
|
|
|
// TestHasAudio/Album/
|
|
// ├── CD1/ (audio, belongs to the album)
|
|
// └── CD2/ (audio, belongs to the album)
|
|
BeforeEach(func() {
|
|
albumRoot = model.NewFolder(testLib, "TestHasAudio/Album")
|
|
disc1 = model.NewFolder(testLib, "TestHasAudio/Album/CD1")
|
|
disc1.NumAudioFiles = 5
|
|
disc2 = model.NewFolder(testLib, "TestHasAudio/Album/CD2")
|
|
disc2.NumAudioFiles = 5
|
|
for _, f := range []*model.Folder{albumRoot, disc1, disc2} {
|
|
Expect(repo.Put(ctx, f)).To(Succeed())
|
|
}
|
|
})
|
|
|
|
It("returns false when all audio under the parent belongs to the given folders", func() {
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *albumRoot, []string{disc1.ID, disc2.ID})).To(BeFalse())
|
|
})
|
|
|
|
It("returns true when another folder under the parent has audio", func() {
|
|
bonus := model.NewFolder(testLib, "TestHasAudio/Album/Bonus")
|
|
bonus.NumAudioFiles = 1
|
|
Expect(repo.Put(ctx, bonus)).To(Succeed())
|
|
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *albumRoot, []string{disc1.ID, disc2.ID})).To(BeTrue())
|
|
})
|
|
|
|
It("returns true when the parent itself contains audio files", func() {
|
|
albumRoot.NumAudioFiles = 2
|
|
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *albumRoot, []string{disc1.ID, disc2.ID})).To(BeTrue())
|
|
})
|
|
|
|
It("ignores audio outside the parent's subtree", func() {
|
|
other := model.NewFolder(testLib, "TestHasAudio/Other Album")
|
|
other.NumAudioFiles = 10
|
|
Expect(repo.Put(ctx, other)).To(Succeed())
|
|
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *albumRoot, []string{disc1.ID, disc2.ID})).To(BeFalse())
|
|
})
|
|
|
|
It("ignores missing folders", func() {
|
|
gone := model.NewFolder(testLib, "TestHasAudio/Album/Gone")
|
|
gone.NumAudioFiles = 3
|
|
gone.Missing = true
|
|
Expect(repo.Put(ctx, gone)).To(Succeed())
|
|
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *albumRoot, []string{disc1.ID, disc2.ID})).To(BeFalse())
|
|
})
|
|
|
|
It("does not treat LIKE wildcards in the parent path as patterns", func() {
|
|
// "TestHas_udio" would LIKE-match "TestHasAudio" if "_" were not escaped
|
|
wildcardRoot := model.NewFolder(testLib, "TestHas_udio/Album")
|
|
Expect(repo.Put(ctx, wildcardRoot)).To(Succeed())
|
|
|
|
Expect(repo.HasAudioOutsideFolders(ctx, *wildcardRoot, []string{"none"})).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("dbFolder.String", func() {
|
|
It("does not dereference a nil Folder", func() {
|
|
Expect(fmt.Sprint(dbFolder{})).To(Equal("<nil>"))
|
|
Expect(fmt.Sprint(&dbFolder{})).To(Equal("<nil>"))
|
|
})
|
|
})
|
|
|
|
Describe("wrapFolderCursor", func() {
|
|
It("does not panic when the cursor yields a dbFolder with nil Folder", func() {
|
|
// Simulate what queryWithStableResults does on the rows.Err() path:
|
|
// it yields a zero-value dbFolder (where Folder is nil) with an error.
|
|
dbErr := fmt.Errorf("database is locked")
|
|
cursor := func(yield func(dbFolder, error) bool) {
|
|
var empty dbFolder // Folder pointer is nil
|
|
yield(empty, dbErr)
|
|
}
|
|
|
|
// wrapFolderCursor should handle the nil Folder without panicking
|
|
wrappedCursor := wrapFolderCursor(cursor)
|
|
var gotErr error
|
|
Expect(func() {
|
|
for _, err := range wrappedCursor {
|
|
gotErr = err
|
|
}
|
|
}).ToNot(Panic())
|
|
Expect(gotErr).To(HaveOccurred())
|
|
Expect(gotErr.Error()).To(ContainSubstring("unexpected nil model.Folder"))
|
|
Expect(errors.Is(gotErr, dbErr)).To(BeTrue(), "should wrap the original cursor error")
|
|
})
|
|
|
|
It("yields folders from a valid cursor", func() {
|
|
folder := &model.Folder{ID: "f1", Name: "Test"}
|
|
cursor := func(yield func(dbFolder, error) bool) {
|
|
yield(dbFolder{Folder: folder}, nil)
|
|
}
|
|
|
|
wrappedCursor := wrapFolderCursor(cursor)
|
|
var folders []model.Folder
|
|
for f, err := range wrappedCursor {
|
|
Expect(err).ToNot(HaveOccurred())
|
|
folders = append(folders, f)
|
|
}
|
|
Expect(folders).To(HaveLen(1))
|
|
Expect(folders[0].ID).To(Equal("f1"))
|
|
})
|
|
})
|
|
|
|
Describe("GetAllWithPlaylists", func() {
|
|
It("returns all non-missing folders with playlists, ignoring the scan-timestamp gate", func() {
|
|
withPls := model.NewFolder(testLib, "TestAllPls/WithPls")
|
|
withPls.NumPlaylists = 2
|
|
noPls := model.NewFolder(testLib, "TestAllPls/NoPls")
|
|
noPls.NumPlaylists = 0
|
|
missingWithPls := model.NewFolder(testLib, "TestAllPls/Missing")
|
|
missingWithPls.NumPlaylists = 1
|
|
missingWithPls.Missing = true
|
|
|
|
Expect(repo.Put(ctx, withPls)).To(Succeed())
|
|
Expect(repo.Put(ctx, noPls)).To(Succeed())
|
|
Expect(repo.Put(ctx, missingWithPls)).To(Succeed())
|
|
|
|
// Force the folder's updated_at to the past so GetTouchedWithPlaylists
|
|
// (which gates on updated_at > last_scan_at) would NOT return it.
|
|
_, err := conn.NewQuery("UPDATE folder SET updated_at = {:t} WHERE id = {:id}").
|
|
Bind(dbx.Params{"t": "2000-01-01 00:00:00", "id": withPls.ID}).Execute()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var ids []string
|
|
cursor, err := repo.GetAllWithPlaylists(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
for f, err := range cursor {
|
|
Expect(err).ToNot(HaveOccurred())
|
|
ids = append(ids, f.ID)
|
|
}
|
|
|
|
Expect(ids).To(ConsistOf(withPls.ID)) // only the non-missing folder with playlists
|
|
})
|
|
})
|
|
})
|