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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. err := sess.BufferSize(setting.Database.IterateBufferSize).
  22. Where("`issue_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").Cols("uuid").
  23. Iterate(new(Attachment),
  24. func(idx int, bean interface{}) error {
  25. attachment := bean.(*Attachment)
  26. if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
  27. return err
  28. }
  29. _, err := sess.ID(attachment.ID).NoAutoCondition().Delete(attachment)
  30. return err
  31. })
  32. if err != nil {
  33. return err
  34. }
  35. return sess.Commit()
  36. }