refactor(cmd): run goose migrations via separate subcommand
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -22,6 +23,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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()
|
cfg, err := config.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("конфигурация: %v", err)
|
log.Fatalf("конфигурация: %v", err)
|
||||||
@@ -43,10 +51,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
if err := postgres.RunMigrations(ctx, pool); err != nil {
|
|
||||||
log.Fatalf("миграции: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store := postgres.New(pool)
|
store := postgres.New(pool)
|
||||||
|
|
||||||
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
||||||
@@ -99,6 +103,30 @@ func main() {
|
|||||||
log.Println("Сервер остановлен")
|
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()) {
|
func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) {
|
||||||
switch strings.ToLower(cfg.SessionStore) {
|
switch strings.ToLower(cfg.SessionStore) {
|
||||||
case "", "postgres":
|
case "", "postgres":
|
||||||
|
|||||||
Reference in New Issue
Block a user