2020-01-11 14:58:46 -05:00
|
|
|
package persistence
|
|
|
|
|
|
2020-01-15 11:11:33 -05:00
|
|
|
import (
|
2020-01-27 09:41:33 -05:00
|
|
|
"context"
|
2021-07-31 20:15:20 -04:00
|
|
|
"database/sql"
|
2020-01-15 11:11:33 -05:00
|
|
|
"reflect"
|
|
|
|
|
|
2022-07-30 12:43:48 -04:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2020-02-01 13:46:03 -05:00
|
|
|
"github.com/navidrome/navidrome/db"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-01-15 11:11:33 -05:00
|
|
|
)
|
|
|
|
|
|
2020-02-01 12:25:05 -05:00
|
|
|
type SQLStore struct {
|
2022-07-30 12:43:48 -04:00
|
|
|
orm orm.QueryExecutor
|
2021-07-31 20:15:20 -04:00
|
|
|
db *sql.DB
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-31 20:15:20 -04:00
|
|
|
func New(db *sql.DB) model.DataStore {
|
|
|
|
|
return &SQLStore{db: db}
|
2020-01-28 08:22:17 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
|
|
|
|
|
return NewAlbumRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Artist(ctx context.Context) model.ArtistRepository {
|
|
|
|
|
return NewArtistRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) MediaFile(ctx context.Context) model.MediaFileRepository {
|
|
|
|
|
return NewMediaFileRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) MediaFolder(ctx context.Context) model.MediaFolderRepository {
|
|
|
|
|
return NewMediaFolderRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Genre(ctx context.Context) model.GenreRepository {
|
|
|
|
|
return NewGenreRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 13:07:39 -04:00
|
|
|
func (s *SQLStore) PlayQueue(ctx context.Context) model.PlayQueueRepository {
|
|
|
|
|
return NewPlayQueueRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Playlist(ctx context.Context) model.PlaylistRepository {
|
|
|
|
|
return NewPlaylistRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Property(ctx context.Context) model.PropertyRepository {
|
|
|
|
|
return NewPropertyRepository(ctx, s.getOrmer())
|
2020-01-15 11:11:33 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-15 20:11:37 +00:00
|
|
|
func (s *SQLStore) Radio(ctx context.Context) model.RadioRepository {
|
|
|
|
|
return NewRadioRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 16:47:32 -04:00
|
|
|
func (s *SQLStore) UserProps(ctx context.Context) model.UserPropsRepository {
|
|
|
|
|
return NewUserPropsRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 21:20:35 +05:30
|
|
|
func (s *SQLStore) Share(ctx context.Context) model.ShareRepository {
|
|
|
|
|
return NewShareRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) User(ctx context.Context) model.UserRepository {
|
|
|
|
|
return NewUserRepository(ctx, s.getOrmer())
|
2020-01-19 15:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 20:01:09 -05:00
|
|
|
func (s *SQLStore) Transcoding(ctx context.Context) model.TranscodingRepository {
|
|
|
|
|
return NewTranscodingRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SQLStore) Player(ctx context.Context) model.PlayerRepository {
|
|
|
|
|
return NewPlayerRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 00:01:05 -04:00
|
|
|
func (s *SQLStore) ScrobbleBuffer(ctx context.Context) model.ScrobbleBufferRepository {
|
|
|
|
|
return NewScrobbleBufferRepository(ctx, s.getOrmer())
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRepository {
|
2020-01-28 08:22:17 -05:00
|
|
|
switch m.(type) {
|
|
|
|
|
case model.User:
|
2020-02-28 14:16:15 -05:00
|
|
|
return s.User(ctx).(model.ResourceRepository)
|
2020-02-29 20:01:09 -05:00
|
|
|
case model.Transcoding:
|
|
|
|
|
return s.Transcoding(ctx).(model.ResourceRepository)
|
|
|
|
|
case model.Player:
|
|
|
|
|
return s.Player(ctx).(model.ResourceRepository)
|
2020-01-28 08:22:17 -05:00
|
|
|
case model.Artist:
|
2020-02-28 14:16:15 -05:00
|
|
|
return s.Artist(ctx).(model.ResourceRepository)
|
2020-01-28 08:22:17 -05:00
|
|
|
case model.Album:
|
2020-02-28 14:16:15 -05:00
|
|
|
return s.Album(ctx).(model.ResourceRepository)
|
2020-01-28 08:22:17 -05:00
|
|
|
case model.MediaFile:
|
2020-02-28 14:16:15 -05:00
|
|
|
return s.MediaFile(ctx).(model.ResourceRepository)
|
2021-07-16 19:41:49 -04:00
|
|
|
case model.Genre:
|
|
|
|
|
return s.Genre(ctx).(model.ResourceRepository)
|
2020-05-03 20:05:03 -04:00
|
|
|
case model.Playlist:
|
|
|
|
|
return s.Playlist(ctx).(model.ResourceRepository)
|
2023-01-15 20:11:37 +00:00
|
|
|
case model.Radio:
|
|
|
|
|
return s.Radio(ctx).(model.ResourceRepository)
|
2021-06-08 16:30:19 -04:00
|
|
|
case model.Share:
|
|
|
|
|
return s.Share(ctx).(model.ResourceRepository)
|
2020-01-28 08:22:17 -05:00
|
|
|
}
|
2020-03-28 18:50:18 -04:00
|
|
|
log.Error("Resource not implemented", "model", reflect.TypeOf(m).Name())
|
2020-01-28 08:22:17 -05:00
|
|
|
return nil
|
2020-01-19 20:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:16:15 -05:00
|
|
|
func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
|
2021-07-31 20:15:20 -04:00
|
|
|
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
|
2020-02-28 14:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-07-30 12:43:48 -04:00
|
|
|
return o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
|
|
|
|
|
newDb := &SQLStore{orm: txOrm}
|
|
|
|
|
return block(newDb)
|
|
|
|
|
})
|
2020-01-15 11:11:33 -05:00
|
|
|
}
|
2020-01-12 17:32:06 -05:00
|
|
|
|
2020-10-02 16:18:45 -04:00
|
|
|
func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
|
|
|
|
|
err := s.MediaFile(ctx).(*mediaFileRepository).deleteNotInPath(rootFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(ctx, "Error removing dangling tracks", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-11-05 20:24:41 -04:00
|
|
|
err = s.MediaFile(ctx).(*mediaFileRepository).removeNonAlbumArtistIds()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(ctx, "Error removing non-album artist_ids", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-10-02 16:18:45 -04:00
|
|
|
err = s.Album(ctx).(*albumRepository).purgeEmpty()
|
2020-01-31 20:17:36 -05:00
|
|
|
if err != nil {
|
2020-06-26 10:23:05 -04:00
|
|
|
log.Error(ctx, "Error removing empty albums", err)
|
2020-01-31 20:17:36 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2020-05-18 20:32:01 -04:00
|
|
|
err = s.Artist(ctx).(*artistRepository).purgeEmpty()
|
2020-01-31 20:17:36 -05:00
|
|
|
if err != nil {
|
2020-06-26 10:23:05 -04:00
|
|
|
log.Error(ctx, "Error removing empty artists", err)
|
2020-01-31 20:17:36 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2020-02-28 14:16:15 -05:00
|
|
|
err = s.MediaFile(ctx).(*mediaFileRepository).cleanAnnotations()
|
2020-02-01 12:15:12 -05:00
|
|
|
if err != nil {
|
2020-06-26 10:23:05 -04:00
|
|
|
log.Error(ctx, "Error removing orphan mediafile annotations", err)
|
2020-02-01 12:15:12 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2020-02-28 14:16:15 -05:00
|
|
|
err = s.Album(ctx).(*albumRepository).cleanAnnotations()
|
2020-02-01 12:15:12 -05:00
|
|
|
if err != nil {
|
2020-06-26 10:23:05 -04:00
|
|
|
log.Error(ctx, "Error removing orphan album annotations", err)
|
2020-02-01 12:15:12 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2020-05-18 20:32:01 -04:00
|
|
|
err = s.Artist(ctx).(*artistRepository).cleanAnnotations()
|
|
|
|
|
if err != nil {
|
2020-06-26 10:23:05 -04:00
|
|
|
log.Error(ctx, "Error removing orphan artist annotations", err)
|
2020-05-18 20:32:01 -04:00
|
|
|
return err
|
|
|
|
|
}
|
2020-08-01 12:17:06 -04:00
|
|
|
err = s.MediaFile(ctx).(*mediaFileRepository).cleanBookmarks()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(ctx, "Error removing orphan bookmarks", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-06-26 10:23:05 -04:00
|
|
|
err = s.Playlist(ctx).(*playlistRepository).removeOrphans()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(ctx, "Error tidying up playlists", err)
|
|
|
|
|
}
|
2021-07-16 18:32:22 -04:00
|
|
|
err = s.Genre(ctx).(*genreRepository).purgeEmpty()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(ctx, "Error removing unused genres", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-06-26 10:23:05 -04:00
|
|
|
return err
|
2020-01-31 20:17:36 -05:00
|
|
|
}
|
|
|
|
|
|
2022-07-30 12:43:48 -04:00
|
|
|
func (s *SQLStore) getOrmer() orm.QueryExecutor {
|
2020-02-28 14:16:15 -05:00
|
|
|
if s.orm == nil {
|
2021-07-31 20:15:20 -04:00
|
|
|
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
|
2020-02-28 14:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
|
log.Error("Error obtaining new orm instance", err)
|
|
|
|
|
}
|
|
|
|
|
return o
|
2020-01-12 17:32:06 -05:00
|
|
|
}
|
2020-02-28 14:16:15 -05:00
|
|
|
return s.orm
|
2020-01-12 17:32:06 -05:00
|
|
|
}
|