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"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
@ -51,7 +52,7 @@ func InitDB() {
|
||||||
log.Fatalf("Error connecting to database: %v", err)
|
log.Fatalf("Error connecting to database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Successfully connected to the database")
|
log.Println("✅ Successfully connected to the database")
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunMigrations() {
|
func RunMigrations() {
|
||||||
|
|
@ -60,7 +61,7 @@ func RunMigrations() {
|
||||||
// Try fallback to relative path if running from cmd/api
|
// Try fallback to relative path if running from cmd/api
|
||||||
files, err = os.ReadDir("../../migrations")
|
files, err = os.ReadDir("../../migrations")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Warning: Could not list migrations directory: %v", err)
|
log.Printf("⚠️ Warning: Could not list migrations directory: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -74,26 +75,28 @@ func RunMigrations() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Running migration: %s", file.Name())
|
log.Printf("📦 Running migration: %s", file.Name())
|
||||||
content, err := os.ReadFile("migrations/" + file.Name())
|
content, err := os.ReadFile("migrations/" + file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Try fallback
|
// Try fallback
|
||||||
content, err = os.ReadFile("../../migrations/" + file.Name())
|
content, err = os.ReadFile("../../migrations/" + file.Name())
|
||||||
if err != nil {
|
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))
|
_, err = DB.Exec(string(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Log warning but don't crash on "already exists" errors if possible,
|
errStr := err.Error()
|
||||||
// but pure SQL Exec might fail hard.
|
// Check if it's an "already exists" error - these are safe to skip
|
||||||
// Given the SQL files use IF NOT EXISTS, we should be fine.
|
if strings.Contains(errStr, "already exists") {
|
||||||
// If one fails, it might be syntax.
|
log.Printf("⏭️ Migration %s skipped (already applied)", file.Name())
|
||||||
log.Printf("Error running migration %s: %v", file.Name(), err)
|
} else {
|
||||||
// Continue or Fail? User wants robustness. Let's Warn and continue for now to avoid single failure blocking all
|
// Real error - log it
|
||||||
|
log.Printf("❌ Error running migration %s: %v", file.Name(), err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Migration %s executed successfully", file.Name())
|
log.Printf("✅ Migration %s executed successfully", file.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("All migrations processed")
|
log.Println("All migrations processed")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue