support sync database structure when starting web server

This commit is contained in:
MaysWind
2020-12-03 23:22:01 +08:00
parent 50f56ccf3d
commit 1f6922c9e5
4 changed files with 76 additions and 6 deletions
+60 -4
View File
@@ -29,11 +29,67 @@ func updateDatabaseStructure(c *cli.Context) error {
log.BootInfof("[database.updateDatabaseStructure] starting maintaining") log.BootInfof("[database.updateDatabaseStructure] starting maintaining")
_ = datastore.Container.UserStore.SyncStructs(new(models.User), new(models.TwoFactor), new(models.TwoFactorRecoveryCode)) err = updateAllDatabaseTablesStructure()
_ = datastore.Container.TokenStore.SyncStructs(new(models.TokenRecord))
_ = datastore.Container.UserDataStore.SyncStructs(new(models.Account), new(models.TransactionCategory))
log.BootInfof("[database.updateDatabaseStructure] maintained successfully") if err != nil {
log.BootErrorf("[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error())
return err
}
log.BootInfof("[database.updateDatabaseStructure] all tables maintained successfully")
return nil
}
func updateAllDatabaseTablesStructure() error {
var err error
err = datastore.Container.UserStore.SyncStructs(new(models.User))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] user table maintained successfully")
}
err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactor))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] two factor table maintained successfully")
}
err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactorRecoveryCode))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] two factor recovery code table maintained successfully")
}
err = datastore.Container.TokenStore.SyncStructs(new(models.TokenRecord))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] token record table maintained successfully")
}
err = datastore.Container.UserDataStore.SyncStructs(new(models.Account))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] account table maintained successfully")
}
err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionCategory))
if err != nil {
return err
} else {
log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction category table maintained successfully")
}
return nil return nil
} }
+9
View File
@@ -44,6 +44,15 @@ func startWebServer(c *cli.Context) error {
log.BootInfof("[server.startWebServer] static root path is %s", config.StaticRootPath) log.BootInfof("[server.startWebServer] static root path is %s", config.StaticRootPath)
if config.AutoUpdateDatabase {
err = updateAllDatabaseTablesStructure()
if err != nil {
log.BootErrorf("[server.startWebServer] update database table structure failed, because %s", err.Error())
return err
}
}
err = requestid.InitializeRequestIdGenerator(config) err = requestid.InitializeRequestIdGenerator(config)
if err != nil { if err != nil {
+3
View File
@@ -65,6 +65,9 @@ conn_max_lifetime = 14400
# Set to true to log each sql statement and execution times # Set to true to log each sql statement and execution times
log_query = false log_query = false
# Set to true to automatically update database structure when starting web server
auto_update_database = true
[log] [log]
# Either "console", "file", default is "console" # Either "console", "file", default is "console"
# Use space to separate multiple modes, e.g. "console file" # Use space to separate multiple modes, e.g. "console file"
+4 -2
View File
@@ -116,8 +116,9 @@ type Config struct {
EnableRequestLog bool EnableRequestLog bool
// Database // Database
DatabaseConfig *DatabaseConfig DatabaseConfig *DatabaseConfig
EnableQueryLog bool EnableQueryLog bool
AutoUpdateDatabase bool
// Log // Log
LogModes []string LogModes []string
@@ -300,6 +301,7 @@ func loadDatabaseConfiguration(config *Config, configFile *ini.File, sectionName
config.DatabaseConfig = dbConfig config.DatabaseConfig = dbConfig
config.EnableQueryLog = getConfigItemBoolValue(configFile, sectionName, "log_query", false) config.EnableQueryLog = getConfigItemBoolValue(configFile, sectionName, "log_query", false)
config.AutoUpdateDatabase = getConfigItemBoolValue(configFile, sectionName, "auto_update_database", true)
return nil return nil
} }