2018-08-23 21:02:51 +02:00
|
|
|
// Package database provides the functionality to open, close and use a database
|
2017-05-17 16:17:02 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
2018-08-23 21:02:51 +02:00
|
|
|
// load gorm defaults dialects
|
2017-10-27 19:40:42 +02:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
2017-05-17 16:17:02 +02:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
|
2017-10-27 19:40:42 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-05-17 16:17:02 +02:00
|
|
|
)
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Write Database connection for writing purposes
|
2017-05-17 16:17:02 +02:00
|
|
|
var Write *gorm.DB
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Read Database connection for reading purposes
|
2017-05-17 16:17:02 +02:00
|
|
|
var Read *gorm.DB
|
|
|
|
|
|
|
|
// Configuration files
|
|
|
|
var (
|
|
|
|
config *Config
|
|
|
|
runtime []interface{}
|
|
|
|
)
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Config of the database connection
|
2017-05-17 16:17:02 +02:00
|
|
|
type Config struct {
|
|
|
|
// type of the database, currently supports sqlite and postgres
|
|
|
|
Type string
|
|
|
|
// connection configuration
|
|
|
|
Connection string
|
|
|
|
// create another connection for reading only
|
|
|
|
ReadConnection string
|
|
|
|
// enable logging of the generated sql string
|
|
|
|
Logging bool
|
|
|
|
}
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Open database and set the given configuration
|
2017-05-17 16:17:02 +02:00
|
|
|
func Open(c Config) (err error) {
|
2017-10-27 19:40:42 +02:00
|
|
|
writeLog := log.WithField("db", "write")
|
2017-05-17 16:17:02 +02:00
|
|
|
config = &c
|
|
|
|
Write, err = gorm.Open(config.Type, config.Connection)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Write.SingularTable(true)
|
|
|
|
Write.LogMode(c.Logging)
|
|
|
|
Write.SetLogger(writeLog)
|
|
|
|
Write.Callback().Create().Remove("gorm:update_time_stamp")
|
|
|
|
Write.Callback().Update().Remove("gorm:update_time_stamp")
|
|
|
|
if len(config.ReadConnection) > 0 {
|
2017-10-27 19:40:42 +02:00
|
|
|
readLog := log.WithField("db", "read")
|
2017-05-17 16:17:02 +02:00
|
|
|
Read, err = gorm.Open(config.Type, config.ReadConnection)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Read.SingularTable(true)
|
|
|
|
Read.LogMode(c.Logging)
|
|
|
|
Read.SetLogger(readLog)
|
|
|
|
Read.Callback().Create().Remove("gorm:update_time_stamp")
|
|
|
|
Read.Callback().Update().Remove("gorm:update_time_stamp")
|
|
|
|
} else {
|
|
|
|
Read = Write
|
|
|
|
}
|
|
|
|
Write.AutoMigrate(runtime...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Close connnection to database safely
|
2017-05-17 16:17:02 +02:00
|
|
|
func Close() {
|
|
|
|
Write.Close()
|
|
|
|
if len(config.ReadConnection) > 0 {
|
|
|
|
Read.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// AddModel to the runtime
|
2017-05-17 16:17:02 +02:00
|
|
|
func AddModel(m interface{}) {
|
|
|
|
runtime = append(runtime, m)
|
|
|
|
}
|