mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 21:17:37 +03:00
refactor: better integration between db
and persistence
packages
Will address support for different DBs in the future (+1 squashed commit) Squashed commits: [a014757] refactor: better integration between `db` and `persistence` packages
This commit is contained in:
parent
76ca8afc84
commit
7e65bb8f20
4 changed files with 30 additions and 27 deletions
27
db/db.go
27
db/db.go
|
@ -3,6 +3,7 @@ package db
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/conf"
|
"github.com/deluan/navidrome/conf"
|
||||||
_ "github.com/deluan/navidrome/db/migrations"
|
_ "github.com/deluan/navidrome/db/migrations"
|
||||||
|
@ -11,19 +12,35 @@ import (
|
||||||
"github.com/pressly/goose"
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
const driver = "sqlite3"
|
var (
|
||||||
|
once sync.Once
|
||||||
|
Driver = "sqlite3"
|
||||||
|
Path string
|
||||||
|
)
|
||||||
|
|
||||||
func EnsureDB() {
|
func Init() {
|
||||||
db, err := sql.Open(driver, conf.Server.DbPath)
|
once.Do(func() {
|
||||||
|
Path = conf.Server.DbPath
|
||||||
|
if Path == ":memory:" {
|
||||||
|
Path = "file::memory:?cache=shared"
|
||||||
|
conf.Server.DbPath = Path
|
||||||
|
}
|
||||||
|
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func EnsureLatestVersion() {
|
||||||
|
Init()
|
||||||
|
db, err := sql.Open(Driver, Path)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to open DB", err)
|
log.Error("Failed to open DB", err)
|
||||||
os.Exit(1)
|
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)
|
||||||
}
|
}
|
||||||
err = goose.Run("up", db, "./")
|
err = goose.Run("up", db, "./")
|
||||||
|
|
2
main.go
2
main.go
|
@ -11,7 +11,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.Load()
|
conf.Load()
|
||||||
db.EnsureDB()
|
db.EnsureLatestVersion()
|
||||||
|
|
||||||
a := CreateServer(conf.Server.MusicFolder)
|
a := CreateServer(conf.Server.MusicFolder)
|
||||||
a.MountRouter("/rest", CreateSubsonicAPIRouter())
|
a.MountRouter("/rest", CreateSubsonicAPIRouter())
|
||||||
|
|
|
@ -3,18 +3,16 @@ package persistence
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
"github.com/deluan/navidrome/conf"
|
"github.com/deluan/navidrome/db"
|
||||||
"github.com/deluan/navidrome/log"
|
"github.com/deluan/navidrome/log"
|
||||||
"github.com/deluan/navidrome/model"
|
"github.com/deluan/navidrome/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
once sync.Once
|
once sync.Once
|
||||||
driver = "sqlite3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SQLStore struct {
|
type SQLStore struct {
|
||||||
|
@ -23,13 +21,7 @@ type SQLStore struct {
|
||||||
|
|
||||||
func New() model.DataStore {
|
func New() model.DataStore {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
dbPath := conf.Server.DbPath
|
err := orm.RegisterDataBase("default", db.Driver, db.Path)
|
||||||
if dbPath == ":memory:" {
|
|
||||||
dbPath = "file::memory:?cache=shared"
|
|
||||||
}
|
|
||||||
log.Debug("Opening DataBase", "dbPath", dbPath, "driver", driver)
|
|
||||||
|
|
||||||
err := initORM(dbPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -147,10 +139,3 @@ func (db *SQLStore) getOrmer() orm.Ormer {
|
||||||
}
|
}
|
||||||
return db.orm
|
return db.orm
|
||||||
}
|
}
|
||||||
|
|
||||||
func initORM(dbPath string) error {
|
|
||||||
if strings.Contains(dbPath, "postgres") {
|
|
||||||
driver = "postgres"
|
|
||||||
}
|
|
||||||
return orm.RegisterDataBase("default", driver, dbPath)
|
|
||||||
}
|
|
||||||
|
|
|
@ -21,10 +21,11 @@ func TestPersistence(t *testing.T) {
|
||||||
tests.Init(t, true)
|
tests.Init(t, true)
|
||||||
|
|
||||||
//os.Remove("./test-123.db")
|
//os.Remove("./test-123.db")
|
||||||
//conf.Server.DbPath = "./test-123.db"
|
//conf.Server.Path = "./test-123.db"
|
||||||
conf.Server.DbPath = "file::memory:?cache=shared"
|
conf.Server.DbPath = ":memory:"
|
||||||
|
db.Init()
|
||||||
New()
|
New()
|
||||||
db.EnsureDB()
|
db.EnsureLatestVersion()
|
||||||
log.SetLevel(log.LevelCritical)
|
log.SetLevel(log.LevelCritical)
|
||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
RunSpecs(t, "Persistence Suite")
|
RunSpecs(t, "Persistence Suite")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue