Skip to content

Commit

Permalink
perf: add migrating migrations before running app.
Browse files Browse the repository at this point in the history
perf: add migrating migrations before running app.
  • Loading branch information
just-pthai-it committed Aug 26, 2024
1 parent ff263a1 commit c01bb2f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion app/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package main

import (
"UTCTSS-microservices/common"
"UTCTSS-microservices/core/middlewares"
"UTCTSS-microservices/core/routes"
_ "UTCTSS-microservices/docs"
"fmt"
"github.com/gin-gonic/gin"
"log"
"os/exec"
"time"
)

func main() {
router := gin.Default()
migrateDatabase()

router := gin.Default()
router.Use(middlewares.ConsoleLoggerMiddleware)
router.Use(middlewares.FileLoggerMiddleware)
router.Use(middlewares.GinLoggerCustomFormat())
Expand All @@ -24,3 +29,28 @@ func main() {
return
}
}

func migrateDatabase() {
databaseMigrateMaxAttempts := 10
for {
migrateCmd := exec.Command("./build/migrationtool", "./database/migrations/", "up")
output, err := migrateCmd.CombinedOutput()

if err != nil {
fmt.Println(common.Red + "Migrate failed: " + err.Error() + common.Reset)
} else {
fmt.Println(common.Blue + "Migrate successful!" + common.Reset)
fmt.Println(string(output))
break
}

databaseMigrateMaxAttempts--
if databaseMigrateMaxAttempts == 0 {
if err != nil {
log.Fatal(common.Red + "Exit because of database migrating failed!" + common.Reset)
}
break
}
time.Sleep(time.Second)
}
}

0 comments on commit c01bb2f

Please sign in to comment.