diff --git a/Dockerfile-local b/Dockerfile-local index 570c93b..6dc59c2 100644 --- a/Dockerfile-local +++ b/Dockerfile-local @@ -9,6 +9,8 @@ RUN go install github.com/air-verse/air@v1.52.2 RUN go install github.com/go-delve/delve/cmd/dlv@v1.22.1 COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o ./build/migrationtool ./database/migrations/tool/. + RUN #go test -v ./... #RUN addgroup -g 1000 appgroup diff --git a/database/migrations/tool/migrate.go b/database/migrations/tool/migrate.go index b500672..1c77d86 100644 --- a/database/migrations/tool/migrate.go +++ b/database/migrations/tool/migrate.go @@ -2,6 +2,7 @@ package main import ( _ "TSS-microservices/database" + "context" _ "github.com/go-sql-driver/mysql" "github.com/pressly/goose/v3" "log" @@ -9,17 +10,20 @@ import ( ) 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) } }