feat(niki): add migrate flag to main for running migrations

This commit is contained in:
Erfan Mohammadi 2024-04-28 14:09:21 +03:30
parent 659cba01d3
commit 02c16595b1
1 changed files with 15 additions and 3 deletions

16
main.go
View File

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