mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-21 11:38:45 +00:00
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
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
@@ -34,8 +35,9 @@ type PostgresProfileStore struct {
|
|||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPostgresProfileStore connects to Postgres using the given DSN and returns a repository ready to use.
|
// NewPostgresProfileStore connects to Postgres using the given DSN and runs migrations.
|
||||||
// Note: Database schema should be created by running migrations via `make db-migrate`
|
// Note: Database schema is managed by centralized migration system using Goose.
|
||||||
|
// Migrations are automatically run on startup.
|
||||||
func NewPostgresProfileStore(dsn string) (*PostgresProfileStore, error) {
|
func NewPostgresProfileStore(dsn string) (*PostgresProfileStore, error) {
|
||||||
if dsn == "" {
|
if dsn == "" {
|
||||||
return nil, errors.New("postgres DSN is empty")
|
return nil, errors.New("postgres DSN is empty")
|
||||||
@@ -46,11 +48,25 @@ func NewPostgresProfileStore(dsn string) (*PostgresProfileStore, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("open postgres: %w", err)
|
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
|
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.
|
// Create inserts a profile row.
|
||||||
func (s *PostgresProfileStore) Create(ctx context.Context, p *Profile) error {
|
func (s *PostgresProfileStore) Create(ctx context.Context, p *Profile) error {
|
||||||
row := toRow(p)
|
row := toRow(p)
|
||||||
|
|||||||
Reference in New Issue
Block a user