From bc33ace6d6e060fd04842e2cb5ffe30e9f4d9176 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 15 Jan 2020 11:11:33 -0500 Subject: [PATCH] Merged helper and initialization functions in one file --- persistence/persistence.go | 78 +++++++++++++++++++++++++++++++++++++- persistence/sql.go | 78 -------------------------------------- 2 files changed, 77 insertions(+), 79 deletions(-) delete mode 100644 persistence/sql.go diff --git a/persistence/persistence.go b/persistence/persistence.go index 1e2653f2b..6e7ce7598 100644 --- a/persistence/persistence.go +++ b/persistence/persistence.go @@ -1,6 +1,61 @@ package persistence -import "reflect" +import ( + "reflect" + "strings" + "sync" + + "github.com/astaxie/beego/orm" + "github.com/cloudsonic/sonic-server/conf" + "github.com/cloudsonic/sonic-server/log" + _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" +) + +const batchSize = 100 + +var ( + once sync.Once + driver = "sqlite3" +) + +func Db() orm.Ormer { + once.Do(func() { + dbPath := conf.Sonic.DbPath + if dbPath == ":memory:" { + dbPath = "file::memory:?cache=shared" + } + err := initORM(dbPath) + if err != nil { + panic(err) + } + log.Debug("Opening DB from: "+dbPath, "driver", driver) + }) + 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 collectField(collection interface{}, getValue func(item interface{}) string) []string { s := reflect.ValueOf(collection) @@ -12,3 +67,24 @@ func collectField(collection interface{}, getValue func(item interface{}) string return result } + +func initORM(dbPath string) error { + verbose := conf.Sonic.LogLevel == "trace" + orm.Debug = verbose + orm.RegisterModel(new(Artist)) + orm.RegisterModel(new(Album)) + orm.RegisterModel(new(MediaFile)) + orm.RegisterModel(new(ArtistInfo)) + orm.RegisterModel(new(Checksum)) + orm.RegisterModel(new(Property)) + orm.RegisterModel(new(Playlist)) + orm.RegisterModel(new(Search)) + if strings.Contains(dbPath, "postgres") { + driver = "postgres" + } + err := orm.RegisterDataBase("default", driver, dbPath) + if err != nil { + panic(err) + } + return orm.RunSyncdb("default", false, verbose) +} diff --git a/persistence/sql.go b/persistence/sql.go deleted file mode 100644 index 6c29e8785..000000000 --- a/persistence/sql.go +++ /dev/null @@ -1,78 +0,0 @@ -package persistence - -import ( - "strings" - "sync" - - "github.com/astaxie/beego/orm" - "github.com/cloudsonic/sonic-server/conf" - "github.com/cloudsonic/sonic-server/log" - _ "github.com/lib/pq" - _ "github.com/mattn/go-sqlite3" -) - -const batchSize = 100 - -var ( - once sync.Once - driver = "sqlite3" -) - -func Db() orm.Ormer { - once.Do(func() { - dbPath := conf.Sonic.DbPath - if dbPath == ":memory:" { - dbPath = "file::memory:?cache=shared" - } - err := initORM(dbPath) - if err != nil { - panic(err) - } - log.Debug("Opening DB from: "+dbPath, "driver", driver) - }) - 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 { - verbose := conf.Sonic.LogLevel == "trace" - orm.Debug = verbose - orm.RegisterModel(new(Artist)) - orm.RegisterModel(new(Album)) - orm.RegisterModel(new(MediaFile)) - orm.RegisterModel(new(ArtistInfo)) - orm.RegisterModel(new(Checksum)) - orm.RegisterModel(new(Property)) - orm.RegisterModel(new(Playlist)) - orm.RegisterModel(new(Search)) - if strings.Contains(dbPath, "postgres") { - driver = "postgres" - } - err := orm.RegisterDataBase("default", driver, dbPath) - if err != nil { - panic(err) - } - return orm.RunSyncdb("default", false, verbose) -}