You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v96.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "os"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/setting"
  9. "xorm.io/xorm"
  10. )
  11. func deleteOrphanedAttachments(x *xorm.Engine) error {
  12. type Attachment struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. UUID string `xorm:"uuid UNIQUE"`
  15. IssueID int64 `xorm:"INDEX"`
  16. ReleaseID int64 `xorm:"INDEX"`
  17. CommentID int64
  18. }
  19. sess := x.NewSession()
  20. defer sess.Close()
  21. var limit = setting.Database.IterateBufferSize
  22. if limit <= 0 {
  23. limit = 50
  24. }
  25. for {
  26. attachements := make([]Attachment, 0, limit)
  27. if err := sess.Where("`issue_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").
  28. Cols("id, uuid").Limit(limit).
  29. Asc("id").
  30. Find(&attachements); err != nil {
  31. return err
  32. }
  33. if len(attachements) == 0 {
  34. return nil
  35. }
  36. var ids = make([]int64, 0, limit)
  37. for _, attachment := range attachements {
  38. ids = append(ids, attachment.ID)
  39. }
  40. if _, err := sess.In("id", ids).Delete(new(Attachment)); err != nil {
  41. return err
  42. }
  43. for _, attachment := range attachements {
  44. if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
  45. return err
  46. }
  47. }
  48. if len(attachements) < limit {
  49. return nil
  50. }
  51. }
  52. }