refactor: use only one DB instance for the whole application

This commit is contained in:
Deluan 2020-02-28 14:16:15 -05:00
parent 38faffa907
commit faa2a978c0
4 changed files with 61 additions and 56 deletions

View file

@ -13,32 +13,36 @@ import (
) )
var ( var (
once sync.Once
Driver = "sqlite3" Driver = "sqlite3"
Path string Path string
) )
func Init() { var (
once sync.Once
db *sql.DB
)
func Db() *sql.DB {
once.Do(func() { once.Do(func() {
var err error
Path = conf.Server.DbPath Path = conf.Server.DbPath
if Path == ":memory:" { if Path == ":memory:" {
Path = "file::memory:?cache=shared" Path = "file::memory:?cache=shared"
conf.Server.DbPath = Path conf.Server.DbPath = Path
} }
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver) log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
db, err = sql.Open(Driver, Path)
if err != nil {
panic(err)
}
}) })
return db
} }
func EnsureLatestVersion() { func EnsureLatestVersion() {
Init() db := Db()
db, err := sql.Open(Driver, Path)
defer db.Close()
if err != nil {
log.Error("Failed to open DB", err)
os.Exit(1)
}
err = goose.SetDialect(Driver) err := goose.SetDialect(Driver)
if err != nil { if err != nil {
log.Error("Invalid DB driver", "driver", Driver, err) log.Error("Invalid DB driver", "driver", Driver, err)
os.Exit(1) os.Exit(1)

View file

@ -20,65 +20,62 @@ type SQLStore struct {
} }
func New() model.DataStore { func New() model.DataStore {
once.Do(func() {
err := orm.RegisterDataBase("default", db.Driver, db.Path)
if err != nil {
panic(err)
}
})
return &SQLStore{} return &SQLStore{}
} }
func (db *SQLStore) Album(ctx context.Context) model.AlbumRepository { func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
return NewAlbumRepository(ctx, db.getOrmer()) return NewAlbumRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) Artist(ctx context.Context) model.ArtistRepository { func (s *SQLStore) Artist(ctx context.Context) model.ArtistRepository {
return NewArtistRepository(ctx, db.getOrmer()) return NewArtistRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) MediaFile(ctx context.Context) model.MediaFileRepository { func (s *SQLStore) MediaFile(ctx context.Context) model.MediaFileRepository {
return NewMediaFileRepository(ctx, db.getOrmer()) return NewMediaFileRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) MediaFolder(ctx context.Context) model.MediaFolderRepository { func (s *SQLStore) MediaFolder(ctx context.Context) model.MediaFolderRepository {
return NewMediaFolderRepository(ctx, db.getOrmer()) return NewMediaFolderRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) Genre(ctx context.Context) model.GenreRepository { func (s *SQLStore) Genre(ctx context.Context) model.GenreRepository {
return NewGenreRepository(ctx, db.getOrmer()) return NewGenreRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) Playlist(ctx context.Context) model.PlaylistRepository { func (s *SQLStore) Playlist(ctx context.Context) model.PlaylistRepository {
return NewPlaylistRepository(ctx, db.getOrmer()) return NewPlaylistRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) Property(ctx context.Context) model.PropertyRepository { func (s *SQLStore) Property(ctx context.Context) model.PropertyRepository {
return NewPropertyRepository(ctx, db.getOrmer()) return NewPropertyRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) User(ctx context.Context) model.UserRepository { func (s *SQLStore) User(ctx context.Context) model.UserRepository {
return NewUserRepository(ctx, db.getOrmer()) return NewUserRepository(ctx, s.getOrmer())
} }
func (db *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRepository { func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRepository {
switch m.(type) { switch m.(type) {
case model.User: case model.User:
return db.User(ctx).(model.ResourceRepository) return s.User(ctx).(model.ResourceRepository)
case model.Artist: case model.Artist:
return db.Artist(ctx).(model.ResourceRepository) return s.Artist(ctx).(model.ResourceRepository)
case model.Album: case model.Album:
return db.Album(ctx).(model.ResourceRepository) return s.Album(ctx).(model.ResourceRepository)
case model.MediaFile: case model.MediaFile:
return db.MediaFile(ctx).(model.ResourceRepository) return s.MediaFile(ctx).(model.ResourceRepository)
} }
log.Error("Resource no implemented", "model", reflect.TypeOf(m).Name()) log.Error("Resource no implemented", "model", reflect.TypeOf(m).Name())
return nil return nil
} }
func (db *SQLStore) WithTx(block func(tx model.DataStore) error) error { func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
o := orm.NewOrm() o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
err := o.Begin() if err != nil {
return err
}
err = o.Begin()
if err != nil { if err != nil {
return err return err
} }
@ -101,41 +98,45 @@ func (db *SQLStore) WithTx(block func(tx model.DataStore) error) error {
return nil return nil
} }
func (db *SQLStore) GC(ctx context.Context) error { func (s *SQLStore) GC(ctx context.Context) error {
err := db.Album(ctx).PurgeEmpty() err := s.Album(ctx).PurgeEmpty()
if err != nil { if err != nil {
return err return err
} }
err = db.Artist(ctx).PurgeEmpty() err = s.Artist(ctx).PurgeEmpty()
if err != nil { if err != nil {
return err return err
} }
err = db.MediaFile(ctx).(*mediaFileRepository).cleanSearchIndex() err = s.MediaFile(ctx).(*mediaFileRepository).cleanSearchIndex()
if err != nil { if err != nil {
return err return err
} }
err = db.Album(ctx).(*albumRepository).cleanSearchIndex() err = s.Album(ctx).(*albumRepository).cleanSearchIndex()
if err != nil { if err != nil {
return err return err
} }
err = db.Artist(ctx).(*artistRepository).cleanSearchIndex() err = s.Artist(ctx).(*artistRepository).cleanSearchIndex()
if err != nil { if err != nil {
return err return err
} }
err = db.MediaFile(ctx).(*mediaFileRepository).cleanAnnotations() err = s.MediaFile(ctx).(*mediaFileRepository).cleanAnnotations()
if err != nil { if err != nil {
return err return err
} }
err = db.Album(ctx).(*albumRepository).cleanAnnotations() err = s.Album(ctx).(*albumRepository).cleanAnnotations()
if err != nil { if err != nil {
return err return err
} }
return db.Artist(ctx).(*artistRepository).cleanAnnotations() return s.Artist(ctx).(*artistRepository).cleanAnnotations()
} }
func (db *SQLStore) getOrmer() orm.Ormer { func (s *SQLStore) getOrmer() orm.Ormer {
if db.orm == nil { if s.orm == nil {
return orm.NewOrm() o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
if err != nil {
log.Error("Error obtaining new orm instance", err)
}
return o
} }
return db.orm return s.orm
} }

View file

@ -21,8 +21,8 @@ func TestPersistence(t *testing.T) {
//os.Remove("./test-123.db") //os.Remove("./test-123.db")
//conf.Server.Path = "./test-123.db" //conf.Server.Path = "./test-123.db"
conf.Server.DbPath = ":memory:" conf.Server.DbPath = "file::memory:?cache=shared"
db.Init() orm.RegisterDataBase("default", db.Driver, conf.Server.DbPath)
New() New()
db.EnsureLatestVersion() db.EnsureLatestVersion()
log.SetLevel(log.LevelCritical) log.SetLevel(log.LevelCritical)

View file

@ -1,6 +1,6 @@
DevDisableAuthentication = false DevDisableAuthentication = false
User = "deluan" User = "deluan"
Password = "wordpass" Password = "wordpass"
DbPath = ":memory:" DbPath = "file::memory:?cache=shared"
MusicFolder = "./tests/itunes-library.xml" MusicFolder = "./tests/itunes-library.xml"
DownsampleCommand = "ffmpeg -i %s -b:a %bk mp3 -" DownsampleCommand = "ffmpeg -i %s -b:a %bk mp3 -"