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
+1 -1
View File
@@ -20,7 +20,7 @@ require (
github.com/kennygrant/sanitize v0.0.0-20170120101633-6a0bfdde8629 github.com/kennygrant/sanitize v0.0.0-20170120101633-6a0bfdde8629
github.com/koding/multiconfig v0.0.0-20170327155832-26b6dfd3a84a github.com/koding/multiconfig v0.0.0-20170327155832-26b6dfd3a84a
github.com/kr/pretty v0.1.0 // indirect 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/mattn/go-sqlite3 v2.0.2+incompatible
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5
github.com/onsi/ginkgo v1.11.0 github.com/onsi/ginkgo v1.11.0
+1 -1
View File
@@ -47,7 +47,7 @@ func (r *artistIndexRepository) Put(idx *domain.ArtistIndex) error {
Artist: artist.Artist, Artist: artist.Artist,
AlbumCount: artist.AlbumCount, AlbumCount: artist.AlbumCount,
} }
_, err := o.Insert(&a) err := r.insert(o, &a)
if err != nil { if err != nil {
return err return err
} }
+5 -2
View File
@@ -35,7 +35,10 @@ func (r *searchableRepository) put(o orm.Ormer, id string, textToIndex string, a
return err return err
} }
if c == 0 { 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 { } else {
_, err = o.Update(a) _, 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))) sanitizedText := strings.TrimSpace(sanitize.Accents(strings.ToLower(text)))
item = Search{ID: id, Table: table, FullText: sanitizedText} item = Search{ID: id, Table: table, FullText: sanitizedText}
if err == orm.ErrNoRows { if err == orm.ErrNoRows {
_, err = o.Insert(&item) err = r.insert(o, &item)
} else { } else {
_, err = o.Update(&item) _, err = o.Update(&item)
} }
+11 -3
View File
@@ -1,17 +1,22 @@
package persistence package persistence
import ( import (
"strings"
"sync" "sync"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/conf" "github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log" "github.com/cloudsonic/sonic-server/log"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
const batchSize = 100 const batchSize = 100
var once sync.Once var (
once sync.Once
driver = "sqlite3"
)
func Db() orm.Ormer { func Db() orm.Ormer {
once.Do(func() { once.Do(func() {
@@ -23,7 +28,7 @@ func Db() orm.Ormer {
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Debug("Opening SQLite DB from: " + dbPath) log.Debug("Opening DB from: "+dbPath, "driver", driver)
}) })
return orm.NewOrm() return orm.NewOrm()
} }
@@ -62,7 +67,10 @@ func initORM(dbPath string) error {
orm.RegisterModel(new(Property)) orm.RegisterModel(new(Property))
orm.RegisterModel(new(Playlist)) orm.RegisterModel(new(Playlist))
orm.RegisterModel(new(Search)) 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 { if err != nil {
panic(err) panic(err)
} }
+13 -1
View File
@@ -54,13 +54,25 @@ func (r *sqlRepository) GetAllIds() ([]string, error) {
return result, nil 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 { func (r *sqlRepository) put(o orm.Ormer, id string, a interface{}) error {
c, err := r.newQuery(o).Filter("id", id).Count() c, err := r.newQuery(o).Filter("id", id).Count()
if err != nil { if err != nil {
return err return err
} }
if c == 0 { 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 return err
} }
_, err = o.Update(a) _, err = o.Update(a)