mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
The cleanup worker now hard-deletes package rows whose deleted_at is older than cache.deleted_retention. The default is 30 days. A value of 0 keeps the rows. The purge runs in bounded batches, with at most 100 batches per cleanup pass, and removes child rows with the package. History tables have no foreign key to packages, and stats_snapshots holds its own counts, so the purge leaves both untouched. The cleanup log line now reports purged_deleted and deleted_retention.
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/pkg/errors"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PurgeDeletedPackages hard-deletes up to limit soft-deleted packages deleted
|
|
// before deletedBefore, oldest id first, with the rows that reference them.
|
|
// Download history and stats snapshots hold no reference and stay.
|
|
func (s *GORMStoreV2) PurgeDeletedPackages(ctx context.Context, deletedBefore time.Time, limit int) (int64, error) {
|
|
if limit <= 0 {
|
|
limit = metadata.DefaultBatchSize
|
|
}
|
|
var purged int64
|
|
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
var ids []int64
|
|
if err := tx.Unscoped().Model(&PackageModel{}).
|
|
Where("deleted_at IS NOT NULL AND deleted_at < ?", deletedBefore).
|
|
Order("id").Limit(limit).Pluck("id", &ids).Error; err != nil {
|
|
return err
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
// The foreign keys cascade on PostgreSQL and MySQL; SQLite enforces them only when asked.
|
|
for _, child := range []interface{}{&PackageVulnerabilityModel{}, &ScanResultModel{}, &PackageMetadataModel{}} {
|
|
if err := tx.Unscoped().Where("package_id IN ?", ids).Delete(child).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
res := tx.Unscoped().Where("id IN ?", ids).Delete(&PackageModel{})
|
|
purged = res.RowsAffected
|
|
return res.Error
|
|
})
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to purge deleted packages")
|
|
}
|
|
return purged, nil
|
|
}
|