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.
435 lines
15 KiB
Go
435 lines
15 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/id"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("PlayQueueRepository", func() {
|
|
var repo model.PlayQueueRepository
|
|
var ctx context.Context
|
|
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
ctx = log.NewContext(context.TODO())
|
|
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: true})
|
|
repo = NewPlayQueueRepository(GetDBXBuilder())
|
|
})
|
|
|
|
Describe("Store", func() {
|
|
It("stores a complete playqueue", func() {
|
|
expected := aPlayQueue("userid", 1, 123, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, expected)).To(Succeed())
|
|
|
|
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
AssertPlayQueue(expected, actual)
|
|
Expect(countPlayQueues(repo, "userid")).To(Equal(1))
|
|
})
|
|
|
|
It("replaces existing playqueue when storing without column names", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Storing replacement playqueue")
|
|
replacement := aPlayQueue("userid", 1, 200, songDayInALife, songAntenna)
|
|
Expect(repo.Store(ctx, replacement)).To(Succeed())
|
|
|
|
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
AssertPlayQueue(replacement, actual)
|
|
Expect(countPlayQueues(repo, "userid")).To(Equal(1))
|
|
})
|
|
|
|
It("clears playqueue when storing empty items", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Storing empty playqueue")
|
|
empty := aPlayQueue("userid", 0, 0)
|
|
Expect(repo.Store(ctx, empty)).To(Succeed())
|
|
|
|
By("Verifying playqueue is cleared")
|
|
_, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("updates only current field when specified", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Getting the existing playqueue to obtain its ID")
|
|
existing, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("Updating only current field")
|
|
update := &model.PlayQueue{
|
|
ID: existing.ID, // Use existing ID for partial update
|
|
UserID: "userid",
|
|
Current: 1,
|
|
ChangedBy: "test-update",
|
|
}
|
|
Expect(repo.Store(ctx, update, "current")).To(Succeed())
|
|
|
|
By("Verifying only current was updated")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Current).To(Equal(1))
|
|
Expect(actual.Position).To(Equal(int64(100))) // Should remain unchanged
|
|
Expect(actual.Items).To(HaveLen(2)) // Should remain unchanged
|
|
})
|
|
|
|
It("updates only position field when specified", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 1, 100, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Getting the existing playqueue to obtain its ID")
|
|
existing, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("Updating only position field")
|
|
update := &model.PlayQueue{
|
|
ID: existing.ID, // Use existing ID for partial update
|
|
UserID: "userid",
|
|
Position: 500,
|
|
ChangedBy: "test-update",
|
|
}
|
|
Expect(repo.Store(ctx, update, "position")).To(Succeed())
|
|
|
|
By("Verifying only position was updated")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Position).To(Equal(int64(500)))
|
|
Expect(actual.Current).To(Equal(1)) // Should remain unchanged
|
|
Expect(actual.Items).To(HaveLen(2)) // Should remain unchanged
|
|
})
|
|
|
|
It("updates multiple specified fields", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Getting the existing playqueue to obtain its ID")
|
|
existing, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("Updating current and position fields")
|
|
update := &model.PlayQueue{
|
|
ID: existing.ID, // Use existing ID for partial update
|
|
UserID: "userid",
|
|
Current: 1,
|
|
Position: 300,
|
|
ChangedBy: "test-update",
|
|
}
|
|
Expect(repo.Store(ctx, update, "current", "position")).To(Succeed())
|
|
|
|
By("Verifying both fields were updated")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Current).To(Equal(1))
|
|
Expect(actual.Position).To(Equal(int64(300)))
|
|
Expect(actual.Items).To(HaveLen(1)) // Should remain unchanged
|
|
})
|
|
|
|
It("preserves existing data when updating with empty items list and column names", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
|
|
By("Getting the existing playqueue to obtain its ID")
|
|
existing, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("Updating only position with empty items")
|
|
update := &model.PlayQueue{
|
|
ID: existing.ID, // Use existing ID for partial update
|
|
UserID: "userid",
|
|
Position: 200,
|
|
ChangedBy: "test-update",
|
|
Items: []model.MediaFile{}, // Empty items
|
|
}
|
|
Expect(repo.Store(ctx, update, "position")).To(Succeed())
|
|
|
|
By("Verifying items are preserved")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Position).To(Equal(int64(200)))
|
|
Expect(actual.Items).To(HaveLen(2)) // Should remain unchanged
|
|
})
|
|
|
|
It("ensures only one record per user by reusing existing record ID", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
initialCount := countPlayQueues(repo, "userid")
|
|
Expect(initialCount).To(Equal(1))
|
|
|
|
By("Storing another playqueue with different ID but same user")
|
|
different := aPlayQueue("userid", 1, 200, songDayInALife)
|
|
different.ID = "different-id" // Force a different ID
|
|
Expect(repo.Store(ctx, different)).To(Succeed())
|
|
|
|
By("Verifying only one record exists for the user")
|
|
finalCount := countPlayQueues(repo, "userid")
|
|
Expect(finalCount).To(Equal(1))
|
|
|
|
By("Verifying the record was updated, not duplicated")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Current).To(Equal(1)) // Should be updated value
|
|
Expect(actual.Position).To(Equal(int64(200))) // Should be updated value
|
|
Expect(actual.Items).To(HaveLen(1)) // Should be new items
|
|
Expect(actual.Items[0].ID).To(Equal(songDayInALife.ID))
|
|
})
|
|
|
|
It("ensures only one record per user even with partial updates", func() {
|
|
By("Storing initial playqueue")
|
|
initial := aPlayQueue("userid", 0, 100, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, initial)).To(Succeed())
|
|
initialCount := countPlayQueues(repo, "userid")
|
|
Expect(initialCount).To(Equal(1))
|
|
|
|
By("Storing partial update with different ID but same user")
|
|
partialUpdate := &model.PlayQueue{
|
|
ID: "completely-different-id", // Use a completely different ID
|
|
UserID: "userid",
|
|
Current: 1,
|
|
ChangedBy: "test-partial",
|
|
}
|
|
Expect(repo.Store(ctx, partialUpdate, "current")).To(Succeed())
|
|
|
|
By("Verifying only one record still exists for the user")
|
|
finalCount := countPlayQueues(repo, "userid")
|
|
Expect(finalCount).To(Equal(1))
|
|
|
|
By("Verifying the existing record was updated with new current value")
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.Current).To(Equal(1)) // Should be updated value
|
|
Expect(actual.Position).To(Equal(int64(100))) // Should remain unchanged
|
|
Expect(actual.Items).To(HaveLen(2)) // Should remain unchanged
|
|
})
|
|
})
|
|
|
|
Describe("Retrieve", func() {
|
|
It("returns notfound error if there's no playqueue for the user", func() {
|
|
_, err := repo.Retrieve(ctx, "user999")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("retrieves the playqueue with only track IDs (no full MediaFile data)", func() {
|
|
By("Storing a playqueue for the user")
|
|
|
|
expected := aPlayQueue("userid", 1, 123, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, expected)).To(Succeed())
|
|
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Basic playqueue properties should match
|
|
Expect(actual.ID).To(Equal(expected.ID))
|
|
Expect(actual.UserID).To(Equal(expected.UserID))
|
|
Expect(actual.Current).To(Equal(expected.Current))
|
|
Expect(actual.Position).To(Equal(expected.Position))
|
|
Expect(actual.ChangedBy).To(Equal(expected.ChangedBy))
|
|
Expect(actual.Items).To(HaveLen(len(expected.Items)))
|
|
|
|
// Items should only contain IDs, not full MediaFile data
|
|
for i, item := range actual.Items {
|
|
Expect(item.ID).To(Equal(expected.Items[i].ID))
|
|
// These fields should be empty since we're not loading full MediaFiles
|
|
Expect(item.Title).To(BeEmpty())
|
|
Expect(item.Path).To(BeEmpty())
|
|
Expect(item.Album).To(BeEmpty())
|
|
Expect(item.Artist).To(BeEmpty())
|
|
}
|
|
})
|
|
|
|
It("returns items with IDs even when some tracks don't exist in the DB", func() {
|
|
// Add a new song to the DB
|
|
newSong := songRadioactivity
|
|
newSong.ID = "temp-track"
|
|
newSong.Path = "/new-path"
|
|
mfRepo := NewMediaFileRepository(GetDBXBuilder())
|
|
|
|
Expect(mfRepo.Put(ctx, &newSong)).To(Succeed())
|
|
|
|
// Create a playqueue with the new song
|
|
pq := aPlayQueue("userid", 0, 0, newSong, songAntenna)
|
|
Expect(repo.Store(ctx, pq)).To(Succeed())
|
|
|
|
// Delete the new song from the database
|
|
Expect(mfRepo.Delete(ctx, "temp-track")).To(Succeed())
|
|
|
|
// Retrieve the playqueue with Retrieve method
|
|
actual, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// The playqueue should still contain both track IDs (including the deleted one)
|
|
Expect(actual.Items).To(HaveLen(2))
|
|
Expect(actual.Items[0].ID).To(Equal("temp-track"))
|
|
Expect(actual.Items[1].ID).To(Equal(songAntenna.ID))
|
|
|
|
// Items should only contain IDs, no other data
|
|
for _, item := range actual.Items {
|
|
Expect(item.Title).To(BeEmpty())
|
|
Expect(item.Path).To(BeEmpty())
|
|
Expect(item.Album).To(BeEmpty())
|
|
Expect(item.Artist).To(BeEmpty())
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("RetrieveWithMediaFiles", func() {
|
|
It("returns notfound error if there's no playqueue for the user", func() {
|
|
_, err := repo.RetrieveWithMediaFiles(ctx, "user999")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("retrieves the playqueue with full MediaFile data", func() {
|
|
By("Storing a playqueue for the user")
|
|
|
|
expected := aPlayQueue("userid", 1, 123, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, expected)).To(Succeed())
|
|
|
|
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
AssertPlayQueue(expected, actual)
|
|
})
|
|
|
|
It("does not return tracks if they don't exist in the DB", func() {
|
|
// Add a new song to the DB
|
|
newSong := songRadioactivity
|
|
newSong.ID = "temp-track"
|
|
newSong.Path = "/new-path"
|
|
mfRepo := NewMediaFileRepository(GetDBXBuilder())
|
|
|
|
Expect(mfRepo.Put(ctx, &newSong)).To(Succeed())
|
|
|
|
// Create a playqueue with the new song
|
|
pq := aPlayQueue("userid", 0, 0, newSong, songAntenna)
|
|
Expect(repo.Store(ctx, pq)).To(Succeed())
|
|
|
|
// Retrieve the playqueue
|
|
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// The playqueue should contain both tracks
|
|
AssertPlayQueue(pq, actual)
|
|
|
|
// Delete the new song
|
|
Expect(mfRepo.Delete(ctx, "temp-track")).To(Succeed())
|
|
|
|
// Retrieve the playqueue
|
|
actual, err = repo.RetrieveWithMediaFiles(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// The playqueue should not contain the deleted track
|
|
Expect(actual.Items).To(HaveLen(1))
|
|
Expect(actual.Items[0].ID).To(Equal(songAntenna.ID))
|
|
})
|
|
})
|
|
|
|
Describe("Clear", func() {
|
|
It("clears an existing playqueue", func() {
|
|
By("Storing a playqueue")
|
|
expected := aPlayQueue("userid", 1, 123, songComeTogether, songDayInALife)
|
|
Expect(repo.Store(ctx, expected)).To(Succeed())
|
|
|
|
By("Verifying playqueue exists")
|
|
_, err := repo.Retrieve(ctx, "userid")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("Clearing the playqueue")
|
|
Expect(repo.Clear(ctx, "userid")).To(Succeed())
|
|
|
|
By("Verifying playqueue is cleared")
|
|
_, err = repo.Retrieve(ctx, "userid")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("does not error when clearing non-existent playqueue", func() {
|
|
// Clear should not error even if no playqueue exists
|
|
Expect(repo.Clear(ctx, "nonexistent-user")).To(Succeed())
|
|
})
|
|
|
|
It("only clears the specified user's playqueue", func() {
|
|
By("Creating users in the database to avoid foreign key constraints")
|
|
userRepo := NewUserRepository(GetDBXBuilder())
|
|
user1 := &model.User{ID: "user1", UserName: "user1", Name: "User 1", Email: "user1@test.com"}
|
|
user2 := &model.User{ID: "user2", UserName: "user2", Name: "User 2", Email: "user2@test.com"}
|
|
Expect(userRepo.Put(ctx, user1)).To(Succeed())
|
|
Expect(userRepo.Put(ctx, user2)).To(Succeed())
|
|
|
|
By("Storing playqueues for two users")
|
|
user1Queue := aPlayQueue("user1", 0, 100, songComeTogether)
|
|
user2Queue := aPlayQueue("user2", 1, 200, songDayInALife)
|
|
Expect(repo.Store(ctx, user1Queue)).To(Succeed())
|
|
Expect(repo.Store(ctx, user2Queue)).To(Succeed())
|
|
|
|
By("Clearing only user1's playqueue")
|
|
Expect(repo.Clear(ctx, "user1")).To(Succeed())
|
|
|
|
By("Verifying user1's playqueue is cleared")
|
|
_, err := repo.Retrieve(ctx, "user1")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
|
|
By("Verifying user2's playqueue still exists")
|
|
actual, err := repo.Retrieve(ctx, "user2")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(actual.UserID).To(Equal("user2"))
|
|
Expect(actual.Current).To(Equal(1))
|
|
Expect(actual.Position).To(Equal(int64(200)))
|
|
})
|
|
})
|
|
})
|
|
|
|
func countPlayQueues(repo model.PlayQueueRepository, userId string) int {
|
|
r := repo.(*playQueueRepository)
|
|
c, err := r.count(GinkgoT().Context(), squirrel.Select().Where(squirrel.Eq{"user_id": userId}))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return int(c)
|
|
}
|
|
|
|
func AssertPlayQueue(expected, actual *model.PlayQueue) {
|
|
Expect(actual.ID).To(Equal(expected.ID))
|
|
Expect(actual.UserID).To(Equal(expected.UserID))
|
|
Expect(actual.Current).To(Equal(expected.Current))
|
|
Expect(actual.Position).To(Equal(expected.Position))
|
|
Expect(actual.ChangedBy).To(Equal(expected.ChangedBy))
|
|
Expect(actual.Items).To(HaveLen(len(expected.Items)))
|
|
for i, item := range actual.Items {
|
|
Expect(item.Title).To(Equal(expected.Items[i].Title))
|
|
}
|
|
}
|
|
|
|
func aPlayQueue(userId string, current int, position int64, items ...model.MediaFile) *model.PlayQueue {
|
|
createdAt := time.Now()
|
|
updatedAt := createdAt.Add(time.Minute)
|
|
return &model.PlayQueue{
|
|
ID: id.NewRandom(),
|
|
UserID: userId,
|
|
Current: current,
|
|
Position: position,
|
|
ChangedBy: "test",
|
|
Items: items,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: updatedAt,
|
|
}
|
|
}
|