Replace beego/orm with dbx (#2693)

* Start migration to dbx package

* Fix annotations and bookmarks bindings

* Fix tests

* Fix more tests

* Remove remaining references to beego/orm

* Add PostScanner/PostMapper interfaces

* Fix importing SmartPlaylists

* Renaming

* More renaming

* Fix artist DB mapping

* Fix playlist updates

* Remove bookmarks at the end of the test

* Remove remaining `orm` struct tags

* Fix user timestamps DB access

* Fix smart playlist evaluated_at DB access

* Fix search3
This commit is contained in:
Deluan Quintão 2023-12-09 13:52:17 -05:00 committed by GitHub
parent 7074455e0e
commit 0ca0d5da22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
60 changed files with 461 additions and 376 deletions

View file

@ -5,79 +5,78 @@ import (
"database/sql"
"reflect"
"github.com/beego/beego/v2/client/orm"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/pocketbase/dbx"
)
type SQLStore struct {
orm orm.QueryExecutor
db *sql.DB
db dbx.Builder
}
func New(db *sql.DB) model.DataStore {
return &SQLStore{db: db}
func New(conn *sql.DB) model.DataStore {
return &SQLStore{db: dbx.NewFromDB(conn, db.Driver)}
}
func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
return NewAlbumRepository(ctx, s.getOrmer())
return NewAlbumRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Artist(ctx context.Context) model.ArtistRepository {
return NewArtistRepository(ctx, s.getOrmer())
return NewArtistRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) MediaFile(ctx context.Context) model.MediaFileRepository {
return NewMediaFileRepository(ctx, s.getOrmer())
return NewMediaFileRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) MediaFolder(ctx context.Context) model.MediaFolderRepository {
return NewMediaFolderRepository(ctx, s.getOrmer())
return NewMediaFolderRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Genre(ctx context.Context) model.GenreRepository {
return NewGenreRepository(ctx, s.getOrmer())
return NewGenreRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) PlayQueue(ctx context.Context) model.PlayQueueRepository {
return NewPlayQueueRepository(ctx, s.getOrmer())
return NewPlayQueueRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Playlist(ctx context.Context) model.PlaylistRepository {
return NewPlaylistRepository(ctx, s.getOrmer())
return NewPlaylistRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Property(ctx context.Context) model.PropertyRepository {
return NewPropertyRepository(ctx, s.getOrmer())
return NewPropertyRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Radio(ctx context.Context) model.RadioRepository {
return NewRadioRepository(ctx, s.getOrmer())
return NewRadioRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) UserProps(ctx context.Context) model.UserPropsRepository {
return NewUserPropsRepository(ctx, s.getOrmer())
return NewUserPropsRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Share(ctx context.Context) model.ShareRepository {
return NewShareRepository(ctx, s.getOrmer())
return NewShareRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) User(ctx context.Context) model.UserRepository {
return NewUserRepository(ctx, s.getOrmer())
return NewUserRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Transcoding(ctx context.Context) model.TranscodingRepository {
return NewTranscodingRepository(ctx, s.getOrmer())
return NewTranscodingRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Player(ctx context.Context) model.PlayerRepository {
return NewPlayerRepository(ctx, s.getOrmer())
return NewPlayerRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) ScrobbleBuffer(ctx context.Context) model.ScrobbleBufferRepository {
return NewScrobbleBufferRepository(ctx, s.getOrmer())
return NewScrobbleBufferRepository(ctx, s.getDBXBuilder())
}
func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRepository {
@ -108,12 +107,12 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe
}
func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
if err != nil {
return err
conn, ok := s.db.(*dbx.DB)
if !ok {
conn = dbx.NewFromDB(db.Db(), db.Driver)
}
return o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
newDb := &SQLStore{orm: txOrm}
return conn.Transactional(func(tx *dbx.Tx) error {
newDb := &SQLStore{db: tx}
return block(newDb)
})
}
@ -171,13 +170,9 @@ func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
return err
}
func (s *SQLStore) getOrmer() orm.QueryExecutor {
if s.orm == nil {
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
if err != nil {
log.Error("Error obtaining new orm instance", err)
}
return o
func (s *SQLStore) getDBXBuilder() dbx.Builder {
if s.db == nil {
return dbx.NewFromDB(db.Db(), db.Driver)
}
return s.orm
return s.db
}