Skip to content

Commit

Permalink
Update migrations migrate tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
just-pthai-it committed Aug 2, 2024
1 parent 84c02bb commit 492abbe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Dockerfile-local
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ RUN go install github.com/air-verse/[email protected]
RUN go install github.com/go-delve/delve/cmd/[email protected]
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o ./build/migrationtool ./database/migrations/tool/.

RUN #go test -v ./...

#RUN addgroup -g 1000 appgroup
Expand Down
10 changes: 7 additions & 3 deletions database/migrations/tool/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package main

import (
_ "TSS-microservices/database"
"context"
_ "github.com/go-sql-driver/mysql"
"github.com/pressly/goose/v3"
"log"
"os"
)

func main() {
MigrateDatabase("../" + os.Getenv("SERVICE_NAME"))
args := os.Args
command := args[2]
migrationsDirRelativePath := args[1]
MigrateDatabase(command, migrationsDirRelativePath+os.Getenv("SERVICE_NAME"))
}

func MigrateDatabase(migrationsFilepath string) {
func MigrateDatabase(command string, migrationsDirRelativePath string) {
dataSourceString := "host=" + os.Getenv("DB_HOST") + " user=" + os.Getenv("DB_USERNAME") + " password=" + os.Getenv("DB_PASSWORD") + " dbname=" + os.Getenv("DB_NAME") + " port=" + os.Getenv("DB_PORT") + " sslmode=disable"
db, err := goose.OpenDBWithDriver(os.Getenv("DB_TYPE"), dataSourceString)
if err != nil {
log.Fatalf("failed to open DB: %v", err)
}

if err := goose.Up(db, migrationsFilepath); err != nil {
if err := goose.RunContext(context.Background(), command, db, migrationsDirRelativePath); err != nil {
log.Fatalf("failed to run migrations: %v", err)
}
}

0 comments on commit 492abbe

Please sign in to comment.