From e56e17ef9868b2fd7f5c90a0555f73ec7f3f12c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 6 Oct 2026 20:19:02 +0300 Subject: [PATCH] refactor(cmd): run goose migrations via separate subcommand --- backend/cmd/server/main.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 04057cbf..b72ab2f0 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -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":