Convert db backend to use Gorm, with basis for support

for Mysql and Postgres in addition to existing Sqlite
This commit is contained in:
Jamie Curnow
2023-05-26 11:04:43 +10:00
parent b4e5b8b6db
commit 29990110b1
93 changed files with 1215 additions and 3075 deletions

View File

@ -11,6 +11,8 @@ import (
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/entity/setting"
"npm/internal/entity/user"
"npm/internal/errors"
"npm/internal/jobqueue"
"npm/internal/logger"
)
@ -25,7 +27,7 @@ func main() {
database.Migrate(func() {
setting.ApplySettings()
database.CheckSetup()
checkSetup()
// Internal Job Queue
jobqueue.Start()
@ -41,7 +43,8 @@ func main() {
if irq == syscall.SIGINT || irq == syscall.SIGTERM {
logger.Info("Got ", irq, " shutting server down ...")
// Close db
err := database.GetInstance().Close()
sqlDB, _ := database.GetDB().DB()
err := sqlDB.Close()
if err != nil {
logger.Error("DatabaseCloseError", err)
}
@ -52,3 +55,28 @@ func main() {
}
})
}
// checkSetup Quick check by counting the number of users in the database
func checkSetup() {
db := database.GetDB()
var count int64
if db != nil {
db.Model(&user.Model{}).
Where("is_disabled = ?", false).
Where("is_system = ?", false).
Count(&count)
if count == 0 {
logger.Warn("No users found, starting in Setup Mode")
} else {
config.IsSetup = true
logger.Info("Application is setup")
}
if config.ErrorReporting {
logger.Warn("Error reporting is enabled - Application Errors WILL be sent to Sentry, you can disable this in the Settings interface")
}
} else {
logger.Error("DatabaseError", errors.ErrDatabaseUnavailable)
}
}