Inject DB into DataStore, instead of hardcode the dependency
This commit is contained in:
@@ -4,12 +4,12 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
_ "github.com/navidrome/navidrome/db/migration"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/utils/singleton"
|
||||
"github.com/pressly/goose"
|
||||
)
|
||||
|
||||
@@ -18,26 +18,21 @@ var (
|
||||
Path string
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
func Db() *sql.DB {
|
||||
once.Do(func() {
|
||||
var err error
|
||||
instance := singleton.Get(&sql.DB{}, func() interface{} {
|
||||
Path = conf.Server.DbPath
|
||||
if Path == ":memory:" {
|
||||
Path = "file::memory:?cache=shared&_foreign_keys=on"
|
||||
conf.Server.DbPath = Path
|
||||
}
|
||||
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
|
||||
db, err = sql.Open(Driver, Path)
|
||||
instance, err := sql.Open(Driver, Path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return instance
|
||||
})
|
||||
return db
|
||||
return instance.(*sql.DB)
|
||||
}
|
||||
|
||||
func EnsureLatestVersion() {
|
||||
|
||||
Reference in New Issue
Block a user