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.

48 lines
1.1 KiB

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