优化结构

This commit is contained in:
Yvan
2026-08-28 15:37:40 +08:00
parent a4ed3a02df
commit 9b1daaf3f6
17 changed files with 100 additions and 64 deletions
+4 -4
View File
@@ -116,18 +116,18 @@ func (r *mediaRepo) ListChunks(ctx context.Context, uploadID uint) ([]*system.Up
return out, nil
}
func (r *mediaRepo) DeleteChunks(ctx context.Context, uploadID uint) error {
return r.data.DB().WithContext(ctx).Where("upload_id = ?", uploadID).Delete(&uploadChunkPO{}).Error
return r.data.DB().WithContext(ctx).Unscoped().Where("upload_id = ?", uploadID).Delete(&uploadChunkPO{}).Error
}
func (r *mediaRepo) StaleUploadSessionIDs(ctx context.Context, before time.Time) ([]uint, error) {
var ids []uint
err := r.data.DB().WithContext(ctx).Model(&uploadSessionPO{}).Where("status IN ? AND updated_at < ?", []string{"uploading", "failed", "merging"}, before).Pluck("id", &ids).Error
err := r.data.DB().WithContext(ctx).Model(&uploadSessionPO{}).Where("(status IN ? AND updated_at < ?) OR (status = ? AND updated_at < ?)", []string{"uploading", "failed"}, before, "merging", before.Add(-time.Hour)).Pluck("id", &ids).Error
return ids, err
}
func (r *mediaRepo) DeleteUploadData(ctx context.Context, uploadID uint) error {
return r.data.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("upload_id = ?", uploadID).Delete(&uploadChunkPO{}).Error; err != nil {
if err := tx.Unscoped().Where("upload_id = ?", uploadID).Delete(&uploadChunkPO{}).Error; err != nil {
return err
}
return tx.Delete(&uploadSessionPO{}, uploadID).Error
return tx.Unscoped().Delete(&uploadSessionPO{}, uploadID).Error
})
}