Messaging/pkg/internal/services/cleaner.go

26 lines
685 B
Go
Raw Permalink Normal View History

2024-04-06 06:05:51 +00:00
package services
import (
2024-05-26 15:01:20 +00:00
"time"
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
2024-04-06 06:05:51 +00:00
"github.com/rs/zerolog/log"
)
func DoAutoDatabaseCleanup() {
deadline := time.Now().Add(60 * time.Minute)
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
// Deal soft-deletion
var count int64
2024-09-19 14:23:32 +00:00
for _, model := range database.AutoMaintainRange {
2024-04-06 06:05:51 +00:00
tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline)
if tx.Error != nil {
2024-05-26 15:01:20 +00:00
log.Error().Err(tx.Error).Msg("An error occurred when running database cleanup...")
2024-04-06 06:05:51 +00:00
}
count += tx.RowsAffected
}
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
}