Merge pull request 'feat(niki): add migrate flag to main for running migrations' (#47) from stage/erfan/73-add-flag-to-main-for-migration into develop

Reviewed-on: ebhomengo/niki#47
This commit is contained in:
Alireza Mokhtari Garakani 2024-04-28 10:51:37 +00:00
commit 93b0f51911
1 changed files with 15 additions and 3 deletions

18
main.go
View File

@ -1,6 +1,8 @@
package main
import (
"flag"
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
"git.gocasts.ir/ebhomengo/niki/config"
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
@ -17,9 +19,17 @@ type Dependencies struct {
initial.Services
}
func parseFlags() bool {
migrateFlag := flag.Bool("migrate", false, "perform database migration")
flag.Parse()
return *migrateFlag
}
func main() {
migrate := parseFlags()
cfg := config.C()
db := initDatabase(cfg)
db := initDatabase(cfg, migrate)
redisAdapter := initRedis(cfg)
dependencies := initDependencies(cfg, redisAdapter, db)
@ -68,8 +78,10 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) {
server.Serve()
}
func initDatabase(cfg config.Config) *mysql.DB {
migrateDatabase(cfg)
func initDatabase(cfg config.Config, migrate bool) *mysql.DB {
if migrate {
migrateDatabase(cfg)
}
return initial.InitMysql(cfg)
}