Passport/pkg/internal/services/cleaner.go

28 lines
875 B
Go
Raw Permalink Normal View History

2024-04-05 17:07:36 +00:00
package services
import (
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
2024-04-05 17:07:36 +00:00
"github.com/rs/zerolog/log"
2024-08-24 07:17:26 +00:00
"time"
2024-04-05 17:07:36 +00:00
)
func DoAutoDatabaseCleanup() {
2024-07-12 03:25:41 +00:00
log.Debug().Msg("Now cleaning up entire database...")
2024-04-05 17:07:36 +00:00
var count int64
for _, model := range database.AutoMaintainRange {
2024-07-12 03:25:41 +00:00
tx := database.C.Unscoped().Delete(model, "deleted_at IS NOT NULL")
2024-04-05 17:07:36 +00:00
if tx.Error != nil {
2024-07-12 03:25:41 +00:00
log.Error().Err(tx.Error).Msg("An error occurred when running cleaning up entire database...")
2024-04-05 17:07:36 +00:00
}
count += tx.RowsAffected
}
2024-08-24 07:17:26 +00:00
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{})
2024-08-24 07:17:26 +00:00
2024-04-05 17:07:36 +00:00
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
}