Added initial support for PostgreSQL

This commit is contained in:
Deluan 2020-01-14 19:20:47 -05:00
parent a167669717
commit 9922ba5994
5 changed files with 31 additions and 8 deletions

2
go.mod
View File

@ -20,7 +20,7 @@ require (
github.com/kennygrant/sanitize v0.0.0-20170120101633-6a0bfdde8629
github.com/koding/multiconfig v0.0.0-20170327155832-26b6dfd3a84a
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.1.1 // indirect
github.com/lib/pq v1.1.1
github.com/mattn/go-sqlite3 v2.0.2+incompatible
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5
github.com/onsi/ginkgo v1.11.0

View File

@ -47,7 +47,7 @@ func (r *artistIndexRepository) Put(idx *domain.ArtistIndex) error {
Artist: artist.Artist,
AlbumCount: artist.AlbumCount,
}
_, err := o.Insert(&a)
err := r.insert(o, &a)
if err != nil {
return err
}

View File

@ -35,7 +35,10 @@ func (r *searchableRepository) put(o orm.Ormer, id string, textToIndex string, a
return err
}
if c == 0 {
_, err = o.Insert(a)
err = r.insert(o, a)
if err != nil && err.Error() == "LastInsertId is not supported by this driver" {
err = nil
}
} else {
_, err = o.Update(a)
}
@ -62,7 +65,7 @@ func (r *searchableRepository) addToIndex(o orm.Ormer, table, id, text string) e
sanitizedText := strings.TrimSpace(sanitize.Accents(strings.ToLower(text)))
item = Search{ID: id, Table: table, FullText: sanitizedText}
if err == orm.ErrNoRows {
_, err = o.Insert(&item)
err = r.insert(o, &item)
} else {
_, err = o.Update(&item)
}

View File

@ -1,17 +1,22 @@
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
var (
once sync.Once
driver = "sqlite3"
)
func Db() orm.Ormer {
once.Do(func() {
@ -23,7 +28,7 @@ func Db() orm.Ormer {
if err != nil {
panic(err)
}
log.Debug("Opening SQLite DB from: " + dbPath)
log.Debug("Opening DB from: "+dbPath, "driver", driver)
})
return orm.NewOrm()
}
@ -62,7 +67,10 @@ func initORM(dbPath string) error {
orm.RegisterModel(new(Property))
orm.RegisterModel(new(Playlist))
orm.RegisterModel(new(Search))
err := orm.RegisterDataBase("default", "sqlite3", dbPath)
if strings.Contains(dbPath, "postgres") {
driver = "postgres"
}
err := orm.RegisterDataBase("default", driver, dbPath)
if err != nil {
panic(err)
}

View File

@ -54,13 +54,25 @@ func (r *sqlRepository) GetAllIds() ([]string, error) {
return result, nil
}
// "Hack" to bypass Postgres driver limitation
func (r *sqlRepository) insert(o orm.Ormer, record interface{}) error {
_, err := o.Insert(record)
if err != nil && err.Error() != "LastInsertId is not supported by this driver" {
return err
}
return nil
}
func (r *sqlRepository) put(o orm.Ormer, id string, a interface{}) error {
c, err := r.newQuery(o).Filter("id", id).Count()
if err != nil {
return err
}
if c == 0 {
_, err = o.Insert(a)
err = r.insert(o, a)
if err != nil && err.Error() == "LastInsertId is not supported by this driver" {
err = nil
}
return err
}
_, err = o.Update(a)