Passport/pkg/internal/services/cleaner.go

28 lines
876 B
Go

package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/rs/zerolog/log"
"time"
)
func DoAutoDatabaseCleanup() {
log.Debug().Msg("Now cleaning up entire database...")
var count int64
for _, model := range database.AutoMaintainRange {
tx := database.C.Unscoped().Delete(model, "deleted_at IS NOT NULL")
if tx.Error != nil {
log.Error().Err(tx.Error).Msg("An error occurred when running cleaning up entire database...")
}
count += tx.RowsAffected
}
deadline := time.Now().Add(-30 * 24 * time.Hour)
seenDeadline := time.Now().Add(-7 * 24 * time.Hour)
database.C.Unscoped().Where("created_at <= ? OR read_at <= ?", deadline, seenDeadline).Delete(&models.Notification{})
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
}