From 02c16595b16b7f33d473d69e9d07412112828ae6 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Sun, 28 Apr 2024 14:09:21 +0330 Subject: [PATCH] feat(niki): add migrate flag to main for running migrations --- main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index ed102da..6989ed1 100644 --- a/main.go +++ b/main.go @@ -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) }