mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-09 19:07:12 +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.
184 lines
6 KiB
Go
184 lines
6 KiB
Go
package persistence
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"slices"
|
|
"time"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
const (
|
|
itemArtworkTable = "item_artwork"
|
|
artworkBatchSize = 200
|
|
)
|
|
|
|
type artworkRepository struct {
|
|
sqlRepository
|
|
items sqlRepository
|
|
}
|
|
|
|
func NewArtworkRepository(db dbx.Builder) model.ArtworkRepository {
|
|
r := &artworkRepository{}
|
|
r.db = db
|
|
r.tableName = "artwork"
|
|
r.items.db = db
|
|
r.items.tableName = itemArtworkTable
|
|
return r
|
|
}
|
|
|
|
func (r *artworkRepository) GetImage(ctx context.Context, hash string) (*model.Artwork, error) {
|
|
sel := Select("*").From(r.tableName).Where(Eq{"hash": hash})
|
|
var res model.Artwork
|
|
if err := r.queryOne(ctx, sel, &res); err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *artworkRepository) PutImage(ctx context.Context, a *model.Artwork) error {
|
|
// created_at is the last-acquisition-write time the prune grace window keys on.
|
|
a.CreatedAt = time.Now()
|
|
values, err := toSQLArgs(*a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// created_at=excluded.created_at: reacquiring an orphan must reset the prune grace window.
|
|
ins := Insert(r.tableName).SetMap(values).Suffix(`ON CONFLICT (hash) DO UPDATE SET mime=excluded.mime, width=excluded.width,
|
|
height=excluded.height, size_bytes=excluded.size_bytes, blur_hash=excluded.blur_hash,
|
|
thumb_hash=excluded.thumb_hash, dominant_color=excluded.dominant_color,
|
|
created_at=excluded.created_at`)
|
|
_, err = r.executeSQL(ctx, ins)
|
|
return err
|
|
}
|
|
|
|
func (r *artworkRepository) GetMimeByHash(ctx context.Context) (map[string]string, error) {
|
|
sel := Select("hash", "mime").From(r.tableName)
|
|
var rows []struct {
|
|
Hash string
|
|
Mime string
|
|
}
|
|
if err := r.queryAll(ctx, sel, &rows); err != nil {
|
|
return nil, err
|
|
}
|
|
res := make(map[string]string, len(rows))
|
|
for _, row := range rows {
|
|
res[row.Hash] = row.Mime
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (r *artworkRepository) PurgeOrphans(ctx context.Context, createdBefore time.Time) (int64, error) {
|
|
del := Delete(r.tableName).Where(And{
|
|
Lt{"created_at": createdBefore},
|
|
Expr("hash NOT IN (SELECT hash FROM " + itemArtworkTable + " WHERE hash <> '')"),
|
|
})
|
|
return r.executeSQL(ctx, del)
|
|
}
|
|
|
|
// artworkOwnerTables maps an artwork kind to the table that owns the entity.
|
|
var artworkOwnerTables = map[model.Kind]string{
|
|
model.KindAlbumArtwork: "album",
|
|
model.KindArtistArtwork: "artist",
|
|
model.KindPlaylistArtwork: "playlist",
|
|
model.KindRadioArtwork: "radio",
|
|
model.KindMediaFileArtwork: "media_file",
|
|
}
|
|
|
|
// purgeDangling deletes rows in r's table whose owning entity is gone, one statement per kind.
|
|
func purgeDangling(ctx context.Context, r sqlRepository) (int64, error) {
|
|
var total int64
|
|
for kind, entityTable := range artworkOwnerTables {
|
|
del := Delete(r.tableName).Where(And{
|
|
Eq{"item_kind": kind.Prefix()},
|
|
Expr("item_id NOT IN (SELECT id FROM " + entityTable + ")"),
|
|
})
|
|
c, err := r.executeSQL(ctx, del)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
total += c
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
func (r *artworkRepository) PurgeDanglingItems(ctx context.Context) (int64, error) {
|
|
return purgeDangling(ctx, r.items)
|
|
}
|
|
|
|
func (r *artworkRepository) GetItemArtwork(ctx context.Context, kind model.Kind, id, imageType string) (*model.ItemArtwork, error) {
|
|
sel := Select("*").From(itemArtworkTable).
|
|
Where(Eq{"item_kind": kind.Prefix(), "item_id": id, "image_type": imageType})
|
|
var res model.ItemArtwork
|
|
if err := r.items.queryOne(ctx, sel, &res); err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *artworkRepository) PutItemArtwork(ctx context.Context, ia *model.ItemArtwork) error {
|
|
ia.ImageType = cmp.Or(ia.ImageType, model.ImageTypePrimary)
|
|
ia.UpdatedAt = time.Now()
|
|
// PutItemArtwork records the outcome of an attempt, so an unset attempted_at is now.
|
|
if ia.AttemptedAt.IsZero() {
|
|
ia.AttemptedAt = ia.UpdatedAt
|
|
}
|
|
values, err := toSQLArgs(*ia)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ins := Insert(itemArtworkTable).SetMap(values).Suffix(`ON CONFLICT (item_kind, item_id, image_type) DO UPDATE SET
|
|
hash=excluded.hash, source=excluded.source, source_path=excluded.source_path, ref_mtime=excluded.ref_mtime,
|
|
trace=excluded.trace, last_failure=excluded.last_failure,
|
|
attempted_at=excluded.attempted_at, updated_at=excluded.updated_at`)
|
|
_, err = r.items.executeSQL(ctx, ins)
|
|
return err
|
|
}
|
|
|
|
// PutLastFailure records why an item exhausted its retry budget. It only updates an existing row:
|
|
// inserting one would write an empty hash, which the rest of the system reads as a settled absent.
|
|
func (r *artworkRepository) PutLastFailure(ctx context.Context, kind model.Kind, id, imageType, trace string) error {
|
|
upd := Update(itemArtworkTable).Set("last_failure", trace).
|
|
Where(Eq{"item_kind": kind.Prefix(), "item_id": id, "image_type": imageType})
|
|
_, err := r.items.executeSQL(ctx, upd)
|
|
return err
|
|
}
|
|
|
|
func (r *artworkRepository) DeleteForItems(ctx context.Context, kind model.Kind, ids []string) error {
|
|
for chunk := range slices.Chunk(ids, artworkBatchSize) {
|
|
if err := r.items.delete(ctx, Eq{"item_kind": kind.Prefix(), "item_id": chunk}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *artworkRepository) GetInfoForItems(ctx context.Context, kind model.Kind, ids []string) (map[string]model.ItemArtworkInfo, error) {
|
|
res := map[string]model.ItemArtworkInfo{}
|
|
for chunk := range slices.Chunk(ids, artworkBatchSize) {
|
|
sel := Select("ia.item_id", "ia.hash", "COALESCE(a.blur_hash, '') as blur_hash",
|
|
"COALESCE(a.thumb_hash, '') as thumb_hash",
|
|
"COALESCE(a.dominant_color, '') as dominant_color",
|
|
"COALESCE(a.width, 0) as width", "COALESCE(a.height, 0) as height").
|
|
From(itemArtworkTable + " ia").
|
|
LeftJoin("artwork a ON a.hash = ia.hash").
|
|
Where(And{
|
|
Eq{"ia.item_kind": kind.Prefix()},
|
|
Eq{"ia.image_type": model.ImageTypePrimary},
|
|
Eq{"ia.item_id": chunk},
|
|
})
|
|
var rows []model.ItemArtworkInfo
|
|
if err := r.items.queryAll(ctx, sel, &rows); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
res[row.ItemID] = row
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
var _ model.ArtworkRepository = (*artworkRepository)(nil)
|