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.

42 lines
995 B

  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. "xorm.io/builder"
  9. "xorm.io/xorm"
  10. )
  11. func removeAttachmentMissedRepo(x *xorm.Engine) error {
  12. type Attachment struct {
  13. UUID string `xorm:"uuid"`
  14. }
  15. var start int
  16. attachments := make([]*Attachment, 0, 50)
  17. for {
  18. err := x.Select("uuid").Where(builder.NotIn("release_id", builder.Select("id").From("`release`"))).
  19. And("release_id > 0").
  20. OrderBy("id").Limit(50, start).Find(&attachments)
  21. if err != nil {
  22. return err
  23. }
  24. for i := 0; i < len(attachments); i++ {
  25. os.RemoveAll(models.AttachmentLocalPath(attachments[i].UUID))
  26. }
  27. if len(attachments) < 50 {
  28. break
  29. }
  30. start += 50
  31. attachments = attachments[:0]
  32. }
  33. _, err := x.Exec("DELETE FROM attachment WHERE release_id > 0 AND release_id NOT IN (SELECT id FROM `release`)")
  34. return err
  35. }