From 76d76614434d232aa29c82b18860aaa97ad388dc Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Wed, 29 Apr 2026 02:57:41 +0300 Subject: [PATCH] feat: Add automatic Goose migration execution on startup in carrier-connector PostgresProfileStore - Add os/exec import for running external commands - Implement runMigrations function to execute goose postgres migrations - Call runMigrations in NewPostgresProfileStore before returning store instance - Update function comment to reflect automatic migration execution on startup - Add error handling and output capture for migration failures --- .../internal/repository/postgres.go | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/apps/carrier-connector/internal/repository/postgres.go b/apps/carrier-connector/internal/repository/postgres.go index 51acfa3..ebe92c3 100644 --- a/apps/carrier-connector/internal/repository/postgres.go +++ b/apps/carrier-connector/internal/repository/postgres.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os/exec" "time" "gorm.io/driver/postgres" @@ -34,8 +35,9 @@ type PostgresProfileStore struct { db *gorm.DB } -// NewPostgresProfileStore connects to Postgres using the given DSN and returns a repository ready to use. -// Note: Database schema should be created by running migrations via `make db-migrate` +// NewPostgresProfileStore connects to Postgres using the given DSN and runs migrations. +// Note: Database schema is managed by centralized migration system using Goose. +// Migrations are automatically run on startup. func NewPostgresProfileStore(dsn string) (*PostgresProfileStore, error) { if dsn == "" { return nil, errors.New("postgres DSN is empty") @@ -46,11 +48,25 @@ func NewPostgresProfileStore(dsn string) (*PostgresProfileStore, error) { if err != nil { return nil, fmt.Errorf("open postgres: %w", err) } - // Note: AutoMigrate removed - schema managed by migration system - // Run `make db-migrate` to create/update database schema + + // Run Goose migrations on startup + if err := runMigrations(dsn); err != nil { + return nil, fmt.Errorf("migration failed: %w", err) + } + return &PostgresProfileStore{db: db}, nil } +// runMigrations executes Goose migrations using the goose binary +func runMigrations(dsn string) error { + cmd := exec.Command("goose", "postgres", dsn, "up", "migrations") + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("goose migration failed: %w, output: %s", err, string(output)) + } + return nil +} + // Create inserts a profile row. func (s *PostgresProfileStore) Create(ctx context.Context, p *Profile) error { row := toRow(p)