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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "path/filepath"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/util"
  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. 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. ids := make([]int64, 0, limit)
  37. for _, attachment := range attachements {
  38. ids = append(ids, attachment.ID)
  39. }
  40. if len(ids) > 0 {
  41. if _, err := sess.In("id", ids).Delete(new(Attachment)); err != nil {
  42. return err
  43. }
  44. }
  45. for _, attachment := range attachements {
  46. uuid := attachment.UUID
  47. if err := util.RemoveAll(filepath.Join(setting.Attachment.Path, uuid[0:1], uuid[1:2], uuid)); err != nil {
  48. return err
  49. }
  50. }
  51. if len(attachements) < limit {
  52. return nil
  53. }
  54. }
  55. }