Skip to content

Commit abb534b

Browse files
guillep2klunny
andauthored
Fix migration bug on v96.go (#10572) (#10573)
Co-authored-by: Lunny Xiao <[email protected]>
1 parent 65dceb6 commit abb534b

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

models/migrations/v96.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,38 @@ func deleteOrphanedAttachments(x *xorm.Engine) error {
2626
sess := x.NewSession()
2727
defer sess.Close()
2828

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+
}
3833

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 {
4056
return err
41-
})
42-
43-
if err != nil {
44-
return err
57+
}
58+
}
59+
if len(attachements) < limit {
60+
return nil
61+
}
4562
}
46-
47-
return sess.Commit()
4863
}

0 commit comments

Comments
 (0)