navidrome/persistence/persistence.go
Deluan Quintão b293b96256
refactor(persistence): stateless repositories with per-call context (#6149)
* 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.
2026-09-25 18:06:10 -04:00

279 lines
9.8 KiB
Go

package persistence
import (
"context"
"database/sql"
"fmt"
"sync"
"time"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils/run"
"github.com/pocketbase/dbx"
)
type SQLStore struct {
db dbx.Builder
library func() model.LibraryRepository
folder func() model.FolderRepository
album func() model.AlbumRepository
artist func() model.ArtistRepository
mediaFile func() model.MediaFileRepository
genre func() model.GenreRepository
tag func() model.TagRepository
playlist func() model.PlaylistRepository
playQueue func() model.PlayQueueRepository
transcoding func() model.TranscodingRepository
player func() model.PlayerRepository
radio func() model.RadioRepository
share func() model.ShareRepository
property func() model.PropertyRepository
user func() model.UserRepository
userProps func() model.UserPropsRepository
scrobbleBuf func() model.ScrobbleBufferRepository
scrobble func() model.ScrobbleRepository
plugin func() model.PluginRepository
artwork func() model.ArtworkRepository
artworkQueue func() model.ArtworkQueueRepository
}
// Repositories are built on first use, so a transaction store only pays for the ones its block touches.
func newSQLStore(db dbx.Builder) *SQLStore {
return &SQLStore{
db: db,
library: sync.OnceValue(func() model.LibraryRepository { return NewLibraryRepository(db) }),
folder: sync.OnceValue(func() model.FolderRepository { return newFolderRepository(db) }),
album: sync.OnceValue(func() model.AlbumRepository { return NewAlbumRepository(db) }),
artist: sync.OnceValue(func() model.ArtistRepository { return NewArtistRepository(db) }),
mediaFile: sync.OnceValue(func() model.MediaFileRepository { return NewMediaFileRepository(db) }),
genre: sync.OnceValue(func() model.GenreRepository { return NewGenreRepository(db) }),
tag: sync.OnceValue(func() model.TagRepository { return NewTagRepository(db) }),
playlist: sync.OnceValue(func() model.PlaylistRepository { return NewPlaylistRepository(db) }),
playQueue: sync.OnceValue(func() model.PlayQueueRepository { return NewPlayQueueRepository(db) }),
transcoding: sync.OnceValue(func() model.TranscodingRepository { return NewTranscodingRepository(db) }),
player: sync.OnceValue(func() model.PlayerRepository { return NewPlayerRepository(db) }),
radio: sync.OnceValue(func() model.RadioRepository { return NewRadioRepository(db) }),
share: sync.OnceValue(func() model.ShareRepository { return NewShareRepository(db) }),
property: sync.OnceValue(func() model.PropertyRepository { return NewPropertyRepository(db) }),
user: sync.OnceValue(func() model.UserRepository { return NewUserRepository(db) }),
userProps: sync.OnceValue(func() model.UserPropsRepository { return NewUserPropsRepository(db) }),
scrobbleBuf: sync.OnceValue(func() model.ScrobbleBufferRepository { return NewScrobbleBufferRepository(db) }),
scrobble: sync.OnceValue(func() model.ScrobbleRepository { return NewScrobbleRepository(db) }),
plugin: sync.OnceValue(func() model.PluginRepository { return NewPluginRepository(db) }),
artwork: sync.OnceValue(func() model.ArtworkRepository { return NewArtworkRepository(db) }),
artworkQueue: sync.OnceValue(func() model.ArtworkQueueRepository { return NewArtworkQueueRepository(db) }),
}
}
func New(conn *sql.DB) model.DataStore {
return newSQLStore(dbx.NewFromDB(conn, db.Driver))
}
func (s *SQLStore) Album() model.AlbumRepository {
return s.album()
}
func (s *SQLStore) Artist() model.ArtistRepository {
return s.artist()
}
func (s *SQLStore) MediaFile() model.MediaFileRepository {
return s.mediaFile()
}
func (s *SQLStore) Library() model.LibraryRepository {
return s.library()
}
func (s *SQLStore) Folder() model.FolderRepository {
return s.folder()
}
func (s *SQLStore) Genre() model.GenreRepository {
return s.genre()
}
func (s *SQLStore) Tag() model.TagRepository {
return s.tag()
}
func (s *SQLStore) PlayQueue() model.PlayQueueRepository {
return s.playQueue()
}
func (s *SQLStore) Playlist() model.PlaylistRepository {
return s.playlist()
}
func (s *SQLStore) Property() model.PropertyRepository {
return s.property()
}
func (s *SQLStore) Radio() model.RadioRepository {
return s.radio()
}
func (s *SQLStore) UserProps() model.UserPropsRepository {
return s.userProps()
}
func (s *SQLStore) Share() model.ShareRepository {
return s.share()
}
func (s *SQLStore) User() model.UserRepository {
return s.user()
}
func (s *SQLStore) Transcoding() model.TranscodingRepository {
return s.transcoding()
}
func (s *SQLStore) Player() model.PlayerRepository {
return s.player()
}
func (s *SQLStore) ScrobbleBuffer() model.ScrobbleBufferRepository {
return s.scrobbleBuf()
}
func (s *SQLStore) Scrobble() model.ScrobbleRepository {
return s.scrobble()
}
func (s *SQLStore) Plugin() model.PluginRepository {
return s.plugin()
}
func (s *SQLStore) Artwork() model.ArtworkRepository {
return s.artwork()
}
func (s *SQLStore) ArtworkQueue() model.ArtworkQueueRepository {
return s.artworkQueue()
}
func scopeLabel(scope []string) string {
if len(scope) > 0 {
return scope[0]
}
return ""
}
func (s *SQLStore) WithTx(block func(tx model.DataStore) error, scope ...string) error {
msg := scopeLabel(scope)
start := time.Now()
conn, inTx := s.db.(*dbx.DB)
if !inTx {
log.Trace("Nested Transaction started", "scope", msg)
conn = dbx.NewFromDB(db.Db(), db.Driver)
} else {
log.Trace("Transaction started", "scope", msg)
}
return conn.Transactional(func(tx *dbx.Tx) error {
newDb := newSQLStore(tx)
err := block(newDb)
if !inTx {
log.Trace("Nested Transaction finished", "scope", msg, "elapsed", time.Since(start), err)
} else {
log.Trace("Transaction finished", "scope", msg, "elapsed", time.Since(start), err)
}
return err
})
}
func (s *SQLStore) WithTxImmediate(block func(tx model.DataStore) error, scope ...string) error {
ctx := context.Background()
return s.WithTx(func(tx model.DataStore) error {
// Workaround to force the transaction to be upgraded to immediate mode to avoid deadlocks
// See https://berthub.eu/articles/posts/a-brief-post-on-sqlite3-database-locked-despite-timeout/
_ = tx.Property().Put(ctx, "tmp_lock_flag", "")
defer func() {
_ = tx.Property().Delete(ctx, "tmp_lock_flag")
}()
return block(tx)
}, scope...)
}
// txRetryDelay spaces out reruns of a busy transaction. Each attempt has already waited out the
// busy timeout, so WithTxRetry gives up only after a sustained lock.
var txRetryDelay = 5 * time.Second
const txMaxRetries = 3
func (s *SQLStore) WithTxRetry(ctx context.Context, block func(ctx context.Context, tx model.DataStore) error, scope ...string) error {
// Inside a transaction, join it: the outer one holds the lock and owns commit and rollback
if _, ok := s.db.(*dbx.DB); !ok {
return block(ctx, s)
}
for attempt := 0; ; attempt++ {
attemptCtx := ctx
if attempt < txMaxRetries {
attemptCtx = withBusyRetry(ctx)
}
err := s.WithTx(func(tx model.DataStore) error { return block(attemptCtx, tx) }, scope...)
if attempt == txMaxRetries || !db.IsBusy(err) {
return err
}
log.Warn(ctx, "Database busy, retrying transaction", "scope", scopeLabel(scope), "attempt", attempt+1, err)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Duration(attempt+1) * txRetryDelay):
}
}
}
type busyRetryKey struct{}
// withBusyRetry marks a transaction attempt that WithTxRetry will rerun, so a busy statement in it
// is logged as a warning rather than an error.
func withBusyRetry(ctx context.Context) context.Context {
return context.WithValue(ctx, busyRetryKey{}, true)
}
func hasBusyRetry(ctx context.Context) bool {
if ctx == nil {
return false
}
retry, _ := ctx.Value(busyRetryKey{}).(bool)
return retry
}
func (s *SQLStore) GC(ctx context.Context, libraryIDs ...int) error {
trace := func(ctx context.Context, msg string, f func() error) func() error {
return func() error {
start := time.Now()
err := f()
log.Debug(ctx, "GC: "+msg, "elapsed", time.Since(start), err)
return err
}
}
// If libraryIDs are provided, scope operations to those libraries where possible
scoped := len(libraryIDs) > 0
if scoped {
log.Debug(ctx, "GC: Running selective garbage collection", "libraryIDs", libraryIDs)
}
err := run.Sequentially(
trace(ctx, "purge empty albums", func() error { return s.album().(*albumRepository).purgeEmpty(ctx, libraryIDs...) }),
trace(ctx, "purge empty artists", func() error { return s.artist().(*artistRepository).purgeEmpty(ctx) }),
trace(ctx, "mark missing artists", func() error { return s.artist().(*artistRepository).markMissing(ctx) }),
trace(ctx, "purge empty folders", func() error { return s.folder().(*folderRepository).purgeEmpty(ctx, libraryIDs...) }),
trace(ctx, "clean album annotations", func() error { return s.album().(*albumRepository).cleanAnnotations(ctx) }),
trace(ctx, "clean artist annotations", func() error { return s.artist().(*artistRepository).cleanAnnotations(ctx) }),
trace(ctx, "clean media file annotations", func() error { return s.mediaFile().(*mediaFileRepository).cleanAnnotations(ctx) }),
trace(ctx, "clean playlist annotations", func() error { return s.playlist().(*playlistRepository).cleanAnnotations(ctx) }),
trace(ctx, "clean media file bookmarks", func() error { return s.mediaFile().(*mediaFileRepository).cleanBookmarks(ctx) }),
trace(ctx, "purge non used tags", func() error { return s.tag().(*tagRepository).purgeUnused(ctx) }),
trace(ctx, "remove orphan playlist tracks", func() error { return s.playlist().(*playlistRepository).removeOrphans(ctx) }),
)
if err != nil {
return fmt.Errorf("tidying up database: %w", err)
}
return nil
}