数据库AutoMigrate现在是可选的,添加了启动参数

This commit is contained in:
2025-05-21 09:03:20 +08:00
parent 66980958f5
commit 2a408f9cfd
3 changed files with 10 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ func Load() {
}
func readconfig() {
viper.SetConfigFile(pathToConfigure)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file: %v\n", err)
os.Exit(1)
@@ -36,8 +37,9 @@ func readconfig() {
func parseArgs() {
pflag.String("config", "./config.yaml", "the path to config file.")
pflag.Bool("init-db", false, "whether to initialize the database,useful when migrating to a new one.")
viper.BindPFlags(pflag.CommandLine)
pflag.Parse()
pathToConfigure = viper.GetString("config")
viper.SetConfigFile(pathToConfigure)
InitDB = viper.GetBool("init-db")
}

View File

@@ -24,3 +24,4 @@ type Config struct {
var pathToConfigure string
var Default Config
var InitDB bool

View File

@@ -33,9 +33,10 @@ func connectSQLite() {
fmt.Printf("error in connecting to SQLite:")
fmt.Println(err)
os.Exit(1)
}
Main.AutoMigrate(&model.Member{}, &model.Tweak{})
if config.InitDB == true {
Main.AutoMigrate(&model.Member{}, &model.Tweak{})
}
}
func connectPGSQL() {
@@ -43,5 +44,7 @@ func connectPGSQL() {
if err != nil {
panic(err)
}
Main.AutoMigrate(&model.Member{}, &model.Tweak{})
if config.InitDB == true {
Main.AutoMigrate(&model.Member{}, &model.Tweak{})
}
}