feat: Replace GORM AutoMigrate with Goose CLI migrations in api-server database initialization

- Add os/exec import for running external commands
- Replace AutoMigrate function with runMigrations function using goose postgres command
- Call runMigrations in NewDatabase before initializing IMSI allocation
- Remove PoolStats function and GORM-based schema migration code
- Reorganize database methods with section comments (Subscriber CRUD, Payment methods, Transactions, Additional CRUD, IMSI allocation)
-
This commit is contained in:
nutcas3
2026-04-29 03:31:00 +03:00
parent e6450f15ad
commit 71353fc237
+53 -95
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"os/exec"
"time" "time"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
@@ -31,36 +32,28 @@ func NewDatabase(cfg *config.DatabaseConfig) (*Database, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s", dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
cfg.Host, cfg.Username, cfg.Password, cfg.Database, cfg.Port, cfg.SSLMode) cfg.Host, cfg.Username, cfg.Password, cfg.Database, cfg.Port, cfg.SSLMode)
// Configure GORM logger
gormLogger := logger.Default.LogMode(logger.Info) gormLogger := logger.Default.LogMode(logger.Info)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: gormLogger})
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: gormLogger,
})
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err) return nil, fmt.Errorf("failed to connect to database: %w", err)
} }
// Get underlying SQL DB to configure connection pool
sqlDB, err := db.DB() sqlDB, err := db.DB()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err) return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err)
} }
// Configure connection pool for optimal performance sqlDB.SetMaxIdleConns(25)
sqlDB.SetMaxIdleConns(25) // Minimum idle connections sqlDB.SetMaxOpenConns(100)
sqlDB.SetMaxOpenConns(100) // Maximum open connections sqlDB.SetConnMaxLifetime(time.Hour)
sqlDB.SetConnMaxLifetime(time.Hour) // Max connection lifetime sqlDB.SetConnMaxIdleTime(30 * time.Minute)
sqlDB.SetConnMaxIdleTime(30 * time.Minute) // Max idle time before closing
database := &Database{DB: db} database := &Database{DB: db}
// Auto-migrate schemas if err := runMigrations(dsn); err != nil {
if err := database.AutoMigrate(); err != nil { return nil, fmt.Errorf("failed to run migrations: %w", err)
return nil, fmt.Errorf("failed to auto-migrate: %w", err)
} }
// Initialize IMSI allocation
if err := database.InitializeIMSIAllocation(); err != nil { if err := database.InitializeIMSIAllocation(); err != nil {
return nil, fmt.Errorf("failed to initialize IMSI allocation: %w", err) return nil, fmt.Errorf("failed to initialize IMSI allocation: %w", err)
} }
@@ -68,20 +61,14 @@ func NewDatabase(cfg *config.DatabaseConfig) (*Database, error) {
return database, nil return database, nil
} }
func (d *Database) AutoMigrate() error { func runMigrations(dsn string) error {
return d.DB.AutoMigrate( cmd := exec.Command("goose", "postgres", dsn, "up", "migrations")
&models.Subscriber{}, output, err := cmd.CombinedOutput()
&models.ServicePlan{}, if err != nil {
&models.Session{}, return fmt.Errorf("goose migration failed: %w, output: %s", err, string(output))
&models.UsageRecord{}, }
&IMSIAllocation{}, log.Printf("Database migrations completed successfully")
&models.Plugin{}, return nil
&models.Automation{},
&models.AutomationRun{},
&models.ConfigEntry{},
&models.DeploymentRecord{},
&models.ChaosExperimentRecord{},
)
} }
func (d *Database) InitializeIMSIAllocation() error { func (d *Database) InitializeIMSIAllocation() error {
@@ -89,17 +76,10 @@ func (d *Database) InitializeIMSIAllocation() error {
result := d.DB.First(&allocation) result := d.DB.First(&allocation)
if result.Error == gorm.ErrRecordNotFound { if result.Error == gorm.ErrRecordNotFound {
// Create initial allocation record allocation = IMSIAllocation{LastIMSI: 0, MinIMSI: 1, MaxIMSI: 999999999}
allocation = IMSIAllocation{
LastIMSI: 0,
MinIMSI: 1,
MaxIMSI: 999999999, // 9-digit subscriber numbers
}
if err := d.DB.Create(&allocation).Error; err != nil { if err := d.DB.Create(&allocation).Error; err != nil {
return fmt.Errorf("failed to create IMSI allocation: %w", err) return fmt.Errorf("failed to create IMSI allocation: %w", err)
} }
log.Printf("Created IMSI allocation record") log.Printf("Created IMSI allocation record")
} else if result.Error != nil { } else if result.Error != nil {
return fmt.Errorf("failed to query IMSI allocation: %w", result.Error) return fmt.Errorf("failed to query IMSI allocation: %w", result.Error)
@@ -116,27 +96,6 @@ func (d *Database) Close() error {
return sqlDB.Close() return sqlDB.Close()
} }
// PoolStats returns current connection pool statistics
func (d *Database) PoolStats() (map[string]any, error) {
sqlDB, err := d.DB.DB()
if err != nil {
return nil, err
}
stats := sqlDB.Stats()
return map[string]any{
"max_open_connections": stats.MaxOpenConnections,
"open_connections": stats.OpenConnections,
"in_use": stats.InUse,
"idle": stats.Idle,
"wait_count": stats.WaitCount,
"wait_duration": stats.WaitDuration.String(),
"max_idle_closed": stats.MaxIdleClosed,
"max_idle_time_closed": stats.MaxIdleTimeClosed,
"max_lifetime_closed": stats.MaxLifetimeClosed,
}, nil
}
// Ping verifies database connectivity
func (d *Database) Ping(ctx context.Context) error { func (d *Database) Ping(ctx context.Context) error {
sqlDB, err := d.DB.DB() sqlDB, err := d.DB.DB()
if err != nil { if err != nil {
@@ -145,6 +104,7 @@ func (d *Database) Ping(ctx context.Context) error {
return sqlDB.PingContext(ctx) return sqlDB.PingContext(ctx)
} }
// Subscriber CRUD
func (d *Database) CreateSubscriber(ctx context.Context, subscriber *models.Subscriber) error { func (d *Database) CreateSubscriber(ctx context.Context, subscriber *models.Subscriber) error {
return d.DB.WithContext(ctx).Create(subscriber).Error return d.DB.WithContext(ctx).Create(subscriber).Error
} }
@@ -176,8 +136,6 @@ func (d *Database) ListSubscribers(ctx context.Context, req *ListSubscribersRequ
var total int64 var total int64
query := d.DB.WithContext(ctx).Model(&models.Subscriber{}) query := d.DB.WithContext(ctx).Model(&models.Subscriber{})
// Apply filters
if req.Status != "" { if req.Status != "" {
query = query.Where("status = ?", req.Status) query = query.Where("status = ?", req.Status)
} }
@@ -189,31 +147,15 @@ func (d *Database) ListSubscribers(ctx context.Context, req *ListSubscribersRequ
"%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%") "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%")
} }
// Count total records
if err := query.Count(&total).Error; err != nil { if err := query.Count(&total).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
// Apply pagination and ordering
offset := (req.Page - 1) * req.PageSize offset := (req.Page - 1) * req.PageSize
err := query.Preload("Plan").Offset(offset).Limit(req.PageSize).Order("created_at DESC").Find(&subscribers).Error err := query.Preload("Plan").Offset(offset).Limit(req.PageSize).Order("created_at DESC").Find(&subscribers).Error
return subscribers, total, err return subscribers, total, err
} }
func (d *Database) GetIMSIAllocation(ctx context.Context) (*IMSIAllocation, error) {
var allocation IMSIAllocation
err := d.DB.WithContext(ctx).First(&allocation).Error
if err != nil {
return nil, err
}
return &allocation, nil
}
func (d *Database) UpdateIMSIAllocation(ctx context.Context, allocation *IMSIAllocation) error {
return d.DB.WithContext(ctx).Save(allocation).Error
}
func (d *Database) GetActiveSessionsByIMSI(ctx context.Context, imsi models.IMSI) ([]models.Session, error) { func (d *Database) GetActiveSessionsByIMSI(ctx context.Context, imsi models.IMSI) ([]models.Session, error) {
var sessions []models.Session var sessions []models.Session
err := d.DB.WithContext(ctx).Where("subscriber_id = ? AND status = ?", imsi, "active").Find(&sessions).Error err := d.DB.WithContext(ctx).Where("subscriber_id = ? AND status = ?", imsi, "active").Find(&sessions).Error
@@ -224,15 +166,7 @@ func (d *Database) UpdateSession(ctx context.Context, session *models.Session) e
return d.DB.WithContext(ctx).Save(session).Error return d.DB.WithContext(ctx).Save(session).Error
} }
func (d *Database) CreateTransaction(ctx context.Context, transaction *models.Transaction) error { // Payment methods
return d.DB.WithContext(ctx).Create(transaction).Error
}
func (d *Database) UpdateSubscriberBalance(ctx context.Context, subscriberID uint, amount float64) error {
return d.DB.WithContext(ctx).Model(&models.Subscriber{}).Where("id = ?", subscriberID).
UpdateColumn("balance", gorm.Expr("balance + ?", amount)).Error
}
func (d *Database) CreatePaymentMethod(ctx context.Context, pm *models.PaymentMethod) error { func (d *Database) CreatePaymentMethod(ctx context.Context, pm *models.PaymentMethod) error {
return d.DB.WithContext(ctx).Create(pm).Error return d.DB.WithContext(ctx).Create(pm).Error
} }
@@ -246,16 +180,17 @@ func (d *Database) GetPaymentMethod(ctx context.Context, id string) (*models.Pay
return &pm, nil return &pm, nil
} }
func (d *Database) DeletePaymentMethod(ctx context.Context, id string) error {
return d.DB.WithContext(ctx).Where("id = ?", id).Delete(&models.PaymentMethod{}).Error
}
func (d *Database) ListPaymentMethods(ctx context.Context, subscriberID uint) ([]models.PaymentMethod, error) { func (d *Database) ListPaymentMethods(ctx context.Context, subscriberID uint) ([]models.PaymentMethod, error) {
var methods []models.PaymentMethod var methods []models.PaymentMethod
err := d.DB.WithContext(ctx).Where("subscriber_id = ?", subscriberID).Find(&methods).Error err := d.DB.WithContext(ctx).Where("subscriber_id = ?", subscriberID).Find(&methods).Error
return methods, err return methods, err
} }
// Transactions
func (d *Database) CreateTransaction(ctx context.Context, transaction *models.Transaction) error {
return d.DB.WithContext(ctx).Create(transaction).Error
}
func (d *Database) GetTransaction(ctx context.Context, transactionID string) (*models.Transaction, error) { func (d *Database) GetTransaction(ctx context.Context, transactionID string) (*models.Transaction, error) {
var tx models.Transaction var tx models.Transaction
err := d.DB.WithContext(ctx).Where("transaction_id = ?", transactionID).First(&tx).Error err := d.DB.WithContext(ctx).Where("transaction_id = ?", transactionID).First(&tx).Error
@@ -293,6 +228,16 @@ func (d *Database) ListTransactions(ctx context.Context, subscriberID uint) ([]m
return transactions, err return transactions, err
} }
// Additional CRUD
func (d *Database) UpdateSubscriberBalance(ctx context.Context, subscriberID uint, amount float64) error {
return d.DB.WithContext(ctx).Model(&models.Subscriber{}).Where("id = ?", subscriberID).
UpdateColumn("balance", gorm.Expr("balance + ?", amount)).Error
}
func (d *Database) DeletePaymentMethod(ctx context.Context, id string) error {
return d.DB.WithContext(ctx).Where("id = ?", id).Delete(&models.PaymentMethod{}).Error
}
func (d *Database) CreateAlert(ctx context.Context, alert *models.Alert) error { func (d *Database) CreateAlert(ctx context.Context, alert *models.Alert) error {
return d.DB.WithContext(ctx).Create(alert).Error return d.DB.WithContext(ctx).Create(alert).Error
} }
@@ -301,11 +246,24 @@ func (d *Database) CreateNotification(ctx context.Context, notification *models.
return d.DB.WithContext(ctx).Create(notification).Error return d.DB.WithContext(ctx).Create(notification).Error
} }
// Request types for database operations // IMSI allocation
func (d *Database) GetIMSIAllocation(ctx context.Context) (*IMSIAllocation, error) {
var allocation IMSIAllocation
err := d.DB.WithContext(ctx).First(&allocation).Error
if err != nil {
return nil, err
}
return &allocation, nil
}
func (d *Database) UpdateIMSIAllocation(ctx context.Context, allocation *IMSIAllocation) error {
return d.DB.WithContext(ctx).Save(allocation).Error
}
type ListSubscribersRequest struct { type ListSubscribersRequest struct {
Page int `json:"page"` Page int `json:"page"`
PageSize int `json:"page_size"` PageSize int `json:"page_size"`
Status models.SubscriberStatus `json:"status"` Status string `json:"status"`
OrganizationID string `json:"organization_id"` OrganizationID string `json:"organization_id"`
Search string `json:"search"` Search string `json:"search"`
} }