Skip to content

Commit

Permalink
fix: ClickHouse
Browse files Browse the repository at this point in the history
feat: debug.Stack() for DontPanic()
  • Loading branch information
pieceowater committed Oct 10, 2024
1 parent ad26452 commit 26c819d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 10 additions & 4 deletions internal/infra/db/ch/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,17 @@ func (d *ClickHouseDB) InitDB() {
log.Printf("connected to database")

if d.autoMigrate {
err = d.db.AutoMigrate(d.models...)
if err != nil {
log.Fatalf("failed to auto-migrate: %v", err)
for _, model := range d.models {
if !d.db.Migrator().HasTable(model) {
err = d.db.AutoMigrate(model)
if err != nil {
log.Fatalf("failed to auto-migrate model %v: %v", model, err)
}
log.Printf("auto-migrated model: %v", model)
} else {
log.Printf("Table already exists for model: %v. Skipping migration.", model)
}
}
log.Printf("auto-migrate complete")
} else {
log.Println("Manual migration mode enabled. Skipping auto-migration.")
}
Expand Down
7 changes: 5 additions & 2 deletions internal/tools/panics/handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package panics

import "github.com/rs/zerolog/log"
import (
"github.com/rs/zerolog/log"
"runtime/debug"
)

// DontPanic is a utility function that handles and logs panics.
// It recovers from a panic, if one occurs, and logs the panic message.
// This function is useful for ensuring that panics do not crash the application.
func DontPanic() {
if r := recover(); r != nil {
log.Printf("Recovered from panic: %v\n", r)
log.Printf("Recovered from panic: %v\n%s", r, debug.Stack())
}
}

0 comments on commit 26c819d

Please sign in to comment.