- InitDB returns error

- Check more errors in InitDB
- fix for healthz endpoint (error checking was wrong)
- remove dbinit parameter from config due to redundancy with mode parameter
This commit is contained in:
Sonja Happ 2019-11-14 11:44:26 +01:00
parent e90e69a326
commit 6d4c84ec4e
14 changed files with 91 additions and 31 deletions

View file

@ -21,7 +21,6 @@ func InitConfig() error {
dbName = flag.String("dbname", "villasdb", "Name of the database to use (default is villasdb)")
dbUser = flag.String("dbuser", "", "Username of database connection (default is <empty>)")
dbPass = flag.String("dbpass", "", "Password of database connection (default is <empty>)")
dbInit = flag.Bool("dbinit", false, "Initialize database with test data (default is off)")
dbSSLMode = flag.String("dbsslmode", "disable", "SSL mode of DB (default is disable)") // TODO: change default for production
amqpURL = flag.String("amqp", "", "If set, use this url to connect to an AMQP broker (default is disabled)")
configFile = flag.String("configFile", "", "Path to YAML configuration file")
@ -31,19 +30,11 @@ func InitConfig() error {
)
flag.Parse()
var dbInitStr string
if *dbInit {
dbInitStr = "true"
} else {
dbInitStr = "false"
}
static := map[string]string{
"db.host": *dbHost,
"db.name": *dbName,
"db.user": *dbUser,
"db.pass": *dbPass,
"db.init": dbInitStr,
"db.ssl": *dbSSLMode,
"amqp.url": *amqpURL,
"mode": *mode,
@ -57,7 +48,6 @@ func InitConfig() error {
"DB_USER": "db.user",
"DB_PASS": "db.pass",
"DB_SSLMODE": "db.ssl",
"DB_INIT": "db.init",
"AMQP_URL": "amqp.url",
"BASE_HOST": "base.host",
"BASE_PATH": "base.path",

View file

@ -3,6 +3,7 @@ package database
import (
"fmt"
"log"
"strings"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
@ -12,14 +13,34 @@ import (
var DBpool *gorm.DB // database used by backend
// Initialize connection to the database
func InitDB(cfg *config.Config) *gorm.DB {
func InitDB(cfg *config.Config) (*gorm.DB, error) {
name, err := cfg.String("db.name")
if err != nil {
return nil, err
}
host, err := cfg.String("db.host")
if err != nil {
return nil, err
}
user, err := cfg.String("db.user")
pass, err := cfg.String("db.pass")
if err != nil && !strings.Contains(err.Error(), "Required setting 'db.user' not set") {
return nil, err
}
pass := ""
if user != "" {
pass, err = cfg.String("db.pass")
if err != nil {
return nil, err
}
}
sslmode, err := cfg.String("db.ssl")
init, err := cfg.Bool("db.init")
if err != nil {
return nil, err
}
mode, err := cfg.String("mode")
if err != nil {
return nil, err
}
dbinfo := fmt.Sprintf("host=%s sslmode=%s dbname=%s", host, sslmode, name)
if user != "" && pass != "" {
@ -28,24 +49,27 @@ func InitDB(cfg *config.Config) *gorm.DB {
db, err := gorm.Open("postgres", dbinfo)
if err != nil {
log.Fatal(err)
return nil, err
}
DBpool = db
MigrateModels(db)
if mode == "test" || init {
if mode == "test" {
DropTables(db)
log.Println("Database tables dropped")
DBAddTestData(db)
err = DBAddTestData(db)
if err != nil {
return nil, err
}
log.Println("Database initialized with test data")
}
log.Println("Database connection established")
return db
return db, nil
}
// Connection pool to already opened DB

View file

@ -17,7 +17,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = InitDB(configuration.GolbalConfig)
db, err = InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
// Verify that you can connect to the database
err = db.DB().Ping()

View file

@ -59,7 +59,10 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -164,7 +164,10 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -4,9 +4,11 @@ import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strings"
)
func RegisterHealthzEndpoint(r *gin.RouterGroup) {
@ -28,17 +30,21 @@ func getHealth(c *gin.Context) {
// check if DB connection is active
db := database.GetDB()
err := db.DB().Ping()
if err != nil {
if helper.DBError(c, err) {
return
}
// check if connection to AMQP broker is alive if backend was started with AMQP client
url, err := configuration.GolbalConfig.String("amqp.url")
if err != nil {
if err != nil && strings.Contains(err.Error(), "Required setting 'amqp.url' not set") {
c.JSON(http.StatusOK, gin.H{})
return
} else if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success:": false,
"message": err.Error(),
})
return
}
if len(url) != 0 {

View file

@ -21,7 +21,9 @@ func TestHealthz(t *testing.T) {
assert.NoError(t, err)
// connect DB
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
assert.NoError(t, err)
defer db.Close()
router = gin.Default()
@ -37,7 +39,8 @@ func TestHealthz(t *testing.T) {
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// reconnect DB
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
assert.NoError(t, err)
defer db.Close()
// test healthz endpoint for connected DB and unconnected AMQP client

View file

@ -37,7 +37,10 @@ func TestMain(m *testing.M) {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -110,7 +110,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -99,7 +99,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -32,7 +32,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -35,7 +35,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -84,7 +84,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(m)
}
db = database.InitDB(configuration.GolbalConfig)
db, err = database.InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
defer db.Close()
router = gin.Default()

View file

@ -43,7 +43,11 @@ func main() {
log.Printf("Error during initialization of global configuration: %v, aborting.", err.Error())
return
}
db := database.InitDB(configuration.GolbalConfig)
db, err := database.InitDB(configuration.GolbalConfig)
if err != nil {
log.Printf("Error during initialization of database: %v, aborting.", err.Error())
return
}
defer db.Close()
m, err := configuration.GolbalConfig.String("mode")