mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-18 16:18:52 +01:00
Replace the 131 SQLite migrations with a single goose migration that creates the whole schema in PostgreSQL (the user table becomes user_account, json columns become jsonb, seed rows for the default library and transcodings). Port the repositories' SQL just far enough for Navidrome to start, scan, log in and browse artists and albums: json_agg/jsonb_* for the SQLite JSON functions, lower() for COLLATE NOCASE, ON CONFLICT DO NOTHING, coalesce on annotations, stricter GROUP BY, explicit casts under the simple protocol, sequential queries in library.RefreshStats. db/ is now Postgres-only, with backup/restore stubbed and DevExternalScanner forced off. Embed the PGlite module like go-taglib: pglite.wasi.gz is go:embed'ed, decompressed in memory and compiled by wazero with an on-disk compilation cache, so the module itself is never written to disk. The cluster is created by initdb at runtime on the first start; initdb runs in a throwaway wasm instance and the backend in a fresh one, otherwise the next start cannot find a valid checkpoint. Since initdb creates only template1, a short-lived backend then runs CREATE DATABASE navidrome and the real backend starts on it. The build recipe and patches for the -O2, no-wizer module live in db/pglite/build.
149 lines
4 KiB
Go
149 lines
4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/deluan/rest"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
type playerRepository struct {
|
|
sqlRepository
|
|
}
|
|
|
|
func NewPlayerRepository(ctx context.Context, db dbx.Builder) model.PlayerRepository {
|
|
r := &playerRepository{}
|
|
r.ctx = ctx
|
|
r.db = db
|
|
r.registerModel(&model.Player{}, map[string]filterFunc{
|
|
"name": containsFilter("player.name"),
|
|
})
|
|
r.setSortMappings(map[string]string{
|
|
"user_name": "username", //TODO rename all user_name and userName to username
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *playerRepository) Put(p *model.Player) error {
|
|
_, err := r.put(p.ID, p)
|
|
return err
|
|
}
|
|
|
|
func (r *playerRepository) selectPlayer(options ...model.QueryOptions) SelectBuilder {
|
|
return r.newSelect(options...).
|
|
Columns("player.*").
|
|
Join("user_account ON player.user_id = user_account.id").
|
|
Columns("user_account.user_name username")
|
|
}
|
|
|
|
func (r *playerRepository) Get(id string) (*model.Player, error) {
|
|
sel := r.selectPlayer().Where(Eq{"player.id": id})
|
|
var res model.Player
|
|
err := r.queryOne(sel, &res)
|
|
return &res, err
|
|
}
|
|
|
|
func (r *playerRepository) FindMatch(userId, client, userAgent string) (*model.Player, error) {
|
|
sel := r.selectPlayer().Where(And{
|
|
Eq{"client": client},
|
|
Eq{"user_agent": userAgent},
|
|
Eq{"user_id": userId},
|
|
})
|
|
var res model.Player
|
|
err := r.queryOne(sel, &res)
|
|
return &res, err
|
|
}
|
|
|
|
func (r *playerRepository) newRestSelect(options ...model.QueryOptions) SelectBuilder {
|
|
s := r.selectPlayer(options...)
|
|
return s.Where(r.addRestriction())
|
|
}
|
|
|
|
func (r *playerRepository) CountByClient(options ...model.QueryOptions) (map[string]int64, error) {
|
|
sel := r.newSelect(options...).
|
|
Columns(
|
|
"case when client = 'NavidromeUI' then name else client end as player",
|
|
"count(*) as count",
|
|
).GroupBy("client")
|
|
var res []struct {
|
|
Player string
|
|
Count int64
|
|
}
|
|
err := r.queryAll(sel, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
counts := make(map[string]int64, len(res))
|
|
for _, c := range res {
|
|
counts[c.Player] = c.Count
|
|
}
|
|
return counts, nil
|
|
}
|
|
|
|
func (r *playerRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
|
return r.count(r.newRestSelect(), options...)
|
|
}
|
|
|
|
func (r *playerRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
|
return r.CountAll(r.parseRestOptions(r.ctx, options...))
|
|
}
|
|
|
|
func (r *playerRepository) Read(id string) (any, error) {
|
|
sel := r.newRestSelect().Where(Eq{"player.id": id})
|
|
var res model.Player
|
|
err := r.queryOne(sel, &res)
|
|
return &res, err
|
|
}
|
|
|
|
func (r *playerRepository) ReadAll(options ...rest.QueryOptions) (any, error) {
|
|
sel := r.newRestSelect(r.parseRestOptions(r.ctx, options...))
|
|
res := model.Players{}
|
|
err := r.queryAll(sel, &res)
|
|
return res, err
|
|
}
|
|
|
|
func (r *playerRepository) EntityName() string {
|
|
return "player"
|
|
}
|
|
|
|
func (r *playerRepository) NewInstance() any {
|
|
return &model.Player{}
|
|
}
|
|
|
|
// isPermitted authorizes creating a new record, based on the owner declared in the request body.
|
|
// This is only safe for inserts: there is no stored row yet, and a non-admin may only create a
|
|
// player they own. Updates must not use this (the body owner is attacker-controlled); they go
|
|
// through updateOwned, which authorizes against the persisted user_id in the WHERE clause.
|
|
func (r *playerRepository) isPermitted(p *model.Player) bool {
|
|
u := loggedUser(r.ctx)
|
|
return u.IsAdmin || p.UserId == u.ID
|
|
}
|
|
|
|
func (r *playerRepository) Save(entity any) (string, error) {
|
|
t := entity.(*model.Player)
|
|
if !r.isPermitted(t) {
|
|
return "", rest.ErrPermissionDenied
|
|
}
|
|
id, err := r.put(t.ID, t)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return "", rest.ErrNotFound
|
|
}
|
|
return id, err
|
|
}
|
|
|
|
func (r *playerRepository) Update(id string, entity any, cols ...string) error {
|
|
t := entity.(*model.Player)
|
|
t.ID = id
|
|
return r.updateOwned(id, t, cols...)
|
|
}
|
|
|
|
func (r *playerRepository) Delete(id string) error {
|
|
return r.deleteOwned(id)
|
|
}
|
|
|
|
var _ model.PlayerRepository = (*playerRepository)(nil)
|
|
var _ rest.Repository = (*playerRepository)(nil)
|
|
var _ rest.Persistable = (*playerRepository)(nil)
|