Fix debug mode enabled by default in db

This commit is contained in:
Juan Font Alonso 2021-07-11 13:13:36 +02:00
parent a16a763283
commit 29b80e3ca1

15
db.go
View file

@ -6,6 +6,7 @@ import (
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
) )
const dbVersion = "1" const dbVersion = "1"
@ -50,23 +51,31 @@ func (h *Headscale) initDB() error {
func (h *Headscale) openDB() (*gorm.DB, error) { func (h *Headscale) openDB() (*gorm.DB, error) {
var db *gorm.DB var db *gorm.DB
var err error var err error
var log logger.Interface
if h.dbDebug {
log = logger.Default
} else {
log = logger.Default.LogMode(logger.Silent)
}
switch h.dbType { switch h.dbType {
case "sqlite3": case "sqlite3":
db, err = gorm.Open(sqlite.Open(h.dbString), &gorm.Config{ db, err = gorm.Open(sqlite.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
}) })
case "postgres": case "postgres":
db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{ db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
}) })
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
if h.dbDebug {
db.Debug()
}
return db, nil return db, nil
} }