refactor(cmd): run goose migrations via separate subcommand
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -22,6 +23,13 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
if err := runMigrate(); err != nil {
|
||||
log.Fatalf("миграции: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("конфигурация: %v", err)
|
||||
@@ -43,10 +51,6 @@ func main() {
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := postgres.RunMigrations(ctx, pool); err != nil {
|
||||
log.Fatalf("миграции: %v", err)
|
||||
}
|
||||
|
||||
store := postgres.New(pool)
|
||||
|
||||
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
||||
@@ -99,6 +103,30 @@ func main() {
|
||||
log.Println("Сервер остановлен")
|
||||
}
|
||||
|
||||
func runMigrate() error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("конфигурация: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
pool, err := postgres.Connect(ctx, postgres.Config{
|
||||
URL: cfg.DatabaseURL,
|
||||
ConnectTimeout: 5 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("БД: %w", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := postgres.RunMigrations(ctx, pool); err != nil {
|
||||
return fmt.Errorf("применение миграций: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) {
|
||||
switch strings.ToLower(cfg.SessionStore) {
|
||||
case "", "postgres":
|
||||
|
||||
Reference in New Issue
Block a user