diff --git a/config/config.go b/config/config.go index ce727a6..b9ecb12 100644 --- a/config/config.go +++ b/config/config.go @@ -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") } diff --git a/config/model.go b/config/model.go index 7e758f5..70dde26 100644 --- a/config/model.go +++ b/config/model.go @@ -24,3 +24,4 @@ type Config struct { var pathToConfigure string var Default Config +var InitDB bool diff --git a/database/database.go b/database/database.go index 799fc00..9e001c6 100644 --- a/database/database.go +++ b/database/database.go @@ -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{}) + } }