fix(backend): improve migration logs with friendly messages
- Show ⏭️ 'skipped (already applied)' for migrations that already exist - Add emojis for better log readability (✅ success, ❌ error, 📦 running) - Avoid confusing 'Error' messages when migrations are simply re-applied
This commit is contained in:
parent
60eafdc6e2
commit
dc2142499b
1 changed files with 14 additions and 11 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
|
@ -51,7 +52,7 @@ func InitDB() {
|
|||
log.Fatalf("Error connecting to database: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Successfully connected to the database")
|
||||
log.Println("✅ Successfully connected to the database")
|
||||
}
|
||||
|
||||
func RunMigrations() {
|
||||
|
|
@ -60,7 +61,7 @@ func RunMigrations() {
|
|||
// Try fallback to relative path if running from cmd/api
|
||||
files, err = os.ReadDir("../../migrations")
|
||||
if err != nil {
|
||||
log.Printf("Warning: Could not list migrations directory: %v", err)
|
||||
log.Printf("⚠️ Warning: Could not list migrations directory: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -74,26 +75,28 @@ func RunMigrations() {
|
|||
continue
|
||||
}
|
||||
|
||||
log.Printf("Running migration: %s", file.Name())
|
||||
log.Printf("📦 Running migration: %s", file.Name())
|
||||
content, err := os.ReadFile("migrations/" + file.Name())
|
||||
if err != nil {
|
||||
// Try fallback
|
||||
content, err = os.ReadFile("../../migrations/" + file.Name())
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading migration file %s: %v", file.Name(), err)
|
||||
log.Fatalf("❌ Error reading migration file %s: %v", file.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = DB.Exec(string(content))
|
||||
if err != nil {
|
||||
// Log warning but don't crash on "already exists" errors if possible,
|
||||
// but pure SQL Exec might fail hard.
|
||||
// Given the SQL files use IF NOT EXISTS, we should be fine.
|
||||
// If one fails, it might be syntax.
|
||||
log.Printf("Error running migration %s: %v", file.Name(), err)
|
||||
// Continue or Fail? User wants robustness. Let's Warn and continue for now to avoid single failure blocking all
|
||||
errStr := err.Error()
|
||||
// Check if it's an "already exists" error - these are safe to skip
|
||||
if strings.Contains(errStr, "already exists") {
|
||||
log.Printf("⏭️ Migration %s skipped (already applied)", file.Name())
|
||||
} else {
|
||||
// Real error - log it
|
||||
log.Printf("❌ Error running migration %s: %v", file.Name(), err)
|
||||
}
|
||||
} else {
|
||||
log.Printf("Migration %s executed successfully", file.Name())
|
||||
log.Printf("✅ Migration %s executed successfully", file.Name())
|
||||
}
|
||||
}
|
||||
log.Println("All migrations processed")
|
||||
|
|
|
|||
Loading…
Reference in a new issue