@@ -26,23 +26,38 @@ func deleteOrphanedAttachments(x *xorm.Engine) error {
26
26
sess := x .NewSession ()
27
27
defer sess .Close ()
28
28
29
- err := sess .BufferSize (setting .Database .IterateBufferSize ).
30
- Where ("`issue_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))" ).Cols ("uuid" ).
31
- Iterate (new (Attachment ),
32
- func (idx int , bean interface {}) error {
33
- attachment := bean .(* Attachment )
34
-
35
- if err := os .RemoveAll (models .AttachmentLocalPath (attachment .UUID )); err != nil {
36
- return err
37
- }
29
+ var limit = setting .Database .IterateBufferSize
30
+ if limit <= 0 {
31
+ limit = 50
32
+ }
38
33
39
- _ , err := sess .ID (attachment .ID ).NoAutoCondition ().Delete (attachment )
34
+ for {
35
+ attachements := make ([]Attachment , 0 , limit )
36
+ if err := sess .Where ("`issue_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))" ).
37
+ Cols ("id, uuid" ).Limit (limit ).
38
+ Asc ("id" ).
39
+ Find (& attachements ); err != nil {
40
+ return err
41
+ }
42
+ if len (attachements ) == 0 {
43
+ return nil
44
+ }
45
+
46
+ var ids = make ([]int64 , 0 , limit )
47
+ for _ , attachment := range attachements {
48
+ ids = append (ids , attachment .ID )
49
+ }
50
+ if _ , err := sess .In ("id" , ids ).Delete (new (Attachment )); err != nil {
51
+ return err
52
+ }
53
+
54
+ for _ , attachment := range attachements {
55
+ if err := os .RemoveAll (models .AttachmentLocalPath (attachment .UUID )); err != nil {
40
56
return err
41
- })
42
-
43
- if err != nil {
44
- return err
57
+ }
58
+ }
59
+ if len (attachements ) < limit {
60
+ return nil
61
+ }
45
62
}
46
-
47
- return sess .Commit ()
48
63
}
0 commit comments