mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
63 lines
1 KiB
Go
63 lines
1 KiB
Go
package db_sql
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"sync"
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
"github.com/cloudsonic/sonic-server/conf"
|
|
"github.com/cloudsonic/sonic-server/log"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var once sync.Once
|
|
|
|
func Db() orm.Ormer {
|
|
once.Do(func() {
|
|
err := os.MkdirAll(conf.Sonic.DbPath, 0700)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
dbPath := path.Join(conf.Sonic.DbPath, "sqlite.db")
|
|
err = initORM(dbPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Debug("Opening SQLite DB from: " + dbPath)
|
|
})
|
|
return orm.NewOrm()
|
|
}
|
|
|
|
func WithTx(block func(orm.Ormer) error) error {
|
|
o := orm.NewOrm()
|
|
err := o.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = block(o)
|
|
if err != nil {
|
|
err2 := o.Rollback()
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
return err
|
|
}
|
|
|
|
err2 := o.Commit()
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func initORM(dbPath string) error {
|
|
orm.Debug = true
|
|
orm.RegisterModel(new(Artist))
|
|
err := orm.RegisterDataBase("default", "sqlite3", dbPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return orm.RunSyncdb("default", false, true)
|
|
}
|