🧪 refactor: add optional scenario-level cleanup logging with BDD_ENABLE_CLEANUP_LOGS
- Add isCleanupLoggingEnabled() helper to check BDD_ENABLE_CLEANUP_LOGS env var - Wrap all cleanup logs in suite.go and server.go with env var check - Add CLEANUP: prefix to all cleanup-related logs for easy filtering - Logs at Info level when enabled (Trace level when disabled) Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -20,6 +20,11 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// isCleanupLoggingEnabled returns true if BDD_ENABLE_CLEANUP_LOGS environment variable is set to "true"
|
||||
func isCleanupLoggingEnabled() bool {
|
||||
return os.Getenv("BDD_ENABLE_CLEANUP_LOGS") == "true"
|
||||
}
|
||||
|
||||
// getDatabaseHost returns the database host from environment variable or defaults to localhost
|
||||
func getDatabaseHost() string {
|
||||
host := os.Getenv("DLC_DATABASE_HOST")
|
||||
@@ -295,12 +300,16 @@ func (s *Server) initDBConnection() error {
|
||||
// This prevents JWT secret pollution between tests
|
||||
func (s *Server) ResetJWTSecrets() error {
|
||||
if s.authService == nil {
|
||||
log.Debug().Msg("No auth service available, skipping JWT secrets reset")
|
||||
if isCleanupLoggingEnabled() {
|
||||
log.Info().Msg("CLEANUP: No auth service available, skipping JWT secrets reset")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
s.authService.ResetJWTSecrets()
|
||||
log.Trace().Msg("JWT secrets reset to initial state")
|
||||
if isCleanupLoggingEnabled() {
|
||||
log.Info().Msg("CLEANUP: JWT secrets reset to initial state")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -309,10 +318,17 @@ func (s *Server) ResetJWTSecrets() error {
|
||||
// Uses SET CONSTRAINTS ALL DEFERRED to temporarily disable foreign key checks
|
||||
func (s *Server) CleanupDatabase() error {
|
||||
if s.db == nil {
|
||||
log.Debug().Msg("No database connection, skipping cleanup")
|
||||
if isCleanupLoggingEnabled() {
|
||||
log.Info().Msg("CLEANUP: No database connection, skipping cleanup")
|
||||
}
|
||||
return nil // No database connection, skip cleanup
|
||||
}
|
||||
|
||||
// Log database state before cleanup
|
||||
if isCleanupLoggingEnabled() {
|
||||
log.Info().Msg("CLEANUP: Starting database cleanup")
|
||||
}
|
||||
|
||||
// Start a transaction for atomic cleanup
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
@@ -408,7 +424,9 @@ func (s *Server) CleanupDatabase() error {
|
||||
return fmt.Errorf("failed to commit cleanup transaction: %w", err)
|
||||
}
|
||||
|
||||
log.Debug().Msg("Database cleanup completed successfully")
|
||||
if isCleanupLoggingEnabled() {
|
||||
log.Info().Msg("CLEANUP: Database cleanup completed successfully")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user