make sure that at least one admin user is added to DB in release mode

This commit is contained in:
Sonja Happ 2020-03-19 09:32:39 +01:00
parent d7de421365
commit 29e0e0cc9e
2 changed files with 21 additions and 4 deletions

View file

@ -309,10 +309,18 @@ var WidgetE = Widget{
func DBAddAdminUser(db *gorm.DB) error {
db.AutoMigrate(&User{})
//create a copy of global test data
user0 := User0
// add admin user to DB
err := db.Create(&user0).Error
// Check if admin user exists in DB
var users []User
err := db.Where("Role = ?", "Admin").Find(&users).Error
if len(users) == 0 {
fmt.Println("No admin user found in DB, adding default admin user.")
//create a copy of global test data
user0 := User0
// add admin user to DB
err = db.Create(&user0).Error
}
return err
}

View file

@ -133,6 +133,7 @@ func main() {
return
}
if mode == "test" {
// test mode: drop all tables and add test data to DB
database.DropTables(db)
log.Println("Database tables dropped, adding test data to DB")
err = database.DBAddTestData(db, basePath, r)
@ -142,6 +143,14 @@ func main() {
panic(err)
}
log.Println("Database initialized with test data")
} else {
// release mode: make sure that at least one admin user exists in DB
err = database.DBAddAdminUser(db)
if err != nil {
fmt.Println(err.Error())
fmt.Println("error: adding admin user failed, aborting")
panic(err)
}
}
amqpurl, _ := configuration.GolbalConfig.String("amqp.url")