mirror of
https://github.com/Dvorinka/Trackeep.git
synced 2026-06-04 12:32:58 +00:00
feat: migrate to DragonflyDB and clean up environment configuration
- Replace Redis with DragonflyDB for better performance and memory efficiency - Remove redundant environment variables (POSTGRES_*, ENCRYPTION_KEY, OAUTH_SERVICE_URL) - Consolidate database configuration to use single DB_* variables - Use JWT_SECRET for both JWT tokens and encryption - Remove PORT variable redundancy, use BACKEND_PORT consistently - Clean up docker-compose configurations for dev/prod consistency - Add DragonflyDB configuration with optimized memory usage - Remove redis.conf as it's no longer needed - Update health checks to use Redis-compatible CLI for DragonflyDB - Add missing VITE_API_URL to production frontend - Fix GitHub Actions to use correct go.sum path - Clean up development directories and unused files
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
|
||||
// RunMigrations runs all database migrations using Goose
|
||||
func RunMigrations() error {
|
||||
// Get database connection string
|
||||
dbType := os.Getenv("DB_TYPE")
|
||||
if dbType == "" {
|
||||
dbType = "postgres"
|
||||
}
|
||||
|
||||
if dbType != "postgres" {
|
||||
return fmt.Errorf("goose migrations currently only support PostgreSQL, got: %s", dbType)
|
||||
}
|
||||
|
||||
// Build connection string
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
|
||||
os.Getenv("DB_HOST"),
|
||||
os.Getenv("DB_USER"),
|
||||
os.Getenv("DB_PASSWORD"),
|
||||
os.Getenv("DB_NAME"),
|
||||
os.Getenv("DB_PORT"),
|
||||
os.Getenv("DB_SSL_MODE"),
|
||||
)
|
||||
|
||||
// Open database connection
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open database for migrations: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Test connection
|
||||
if err := db.Ping(); err != nil {
|
||||
return fmt.Errorf("failed to ping database for migrations: %w", err)
|
||||
}
|
||||
|
||||
// Set goose dialect
|
||||
if err := goose.SetDialect("postgres"); err != nil {
|
||||
return fmt.Errorf("failed to set goose dialect: %w", err)
|
||||
}
|
||||
|
||||
// Run migrations
|
||||
log.Println("Running database migrations...")
|
||||
if err := goose.Up(db, "migrations"); err != nil {
|
||||
return fmt.Errorf("failed to run migrations: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Database migrations completed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMigrationStatus returns the current migration status
|
||||
func GetMigrationStatus() error {
|
||||
// Get database connection string
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
|
||||
os.Getenv("DB_HOST"),
|
||||
os.Getenv("DB_USER"),
|
||||
os.Getenv("DB_PASSWORD"),
|
||||
os.Getenv("DB_NAME"),
|
||||
os.Getenv("DB_PORT"),
|
||||
os.Getenv("DB_SSL_MODE"),
|
||||
)
|
||||
|
||||
// Open database connection
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open database for migration status: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Set goose dialect
|
||||
if err := goose.SetDialect("postgres"); err != nil {
|
||||
return fmt.Errorf("failed to set goose dialect: %w", err)
|
||||
}
|
||||
|
||||
// Get migration status
|
||||
log.Println("Checking migration status...")
|
||||
if err := goose.Status(db, "migrations"); err != nil {
|
||||
return fmt.Errorf("failed to get migration status: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateMigration creates a new migration file
|
||||
func CreateMigration(name, migrationType string) error {
|
||||
var err error
|
||||
|
||||
switch migrationType {
|
||||
case "up":
|
||||
err = goose.Create(nil, "migrations", name, "up")
|
||||
case "down":
|
||||
err = goose.Create(nil, "migrations", name, "down")
|
||||
default:
|
||||
return fmt.Errorf("invalid migration type: %s (must be 'up' or 'down')", migrationType)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create migration: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Migration file created: %s", name)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user