Skip to content

Commit c632e48

Browse files
committed
add migration
1 parent 4ac0923 commit c632e48

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ var migrations = []Migration{
227227
NewMigration("hash application token", hashAppToken),
228228
// v86 -> v87
229229
NewMigration("add http method to webhook", addHTTPMethodToWebhook),
230+
// v87 -> v88
231+
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
230232
}
231233

232234
// Migrate database to current version

models/migrations/v80.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ func addIsLockedToIssues(x *xorm.Engine) error {
1414
}
1515

1616
return x.Sync2(new(Issue))
17-
1817
}

models/migrations/v87.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package models
2+
3+
import (
4+
"os"
5+
6+
"code.gitea.io/gitea/models"
7+
"code.gitea.io/gitea/modules/setting"
8+
"github.com/go-xorm/xorm"
9+
)
10+
11+
func deleteOrphanedAttachments(x *xorm.Engine) error {
12+
13+
type Attachment struct {
14+
ID int64 `xorm:"pk autoincr"`
15+
UUID string `xorm:"uuid UNIQUE"`
16+
IssueID int64 `xorm:"INDEX"`
17+
ReleaseID int64 `xorm:"INDEX"`
18+
CommentID int64
19+
}
20+
21+
sess := x.NewSession()
22+
defer sess.Close()
23+
24+
err := sess.BufferSize(setting.IterateBufferSize).
25+
Where("comment_id = ? AND release_id = ?", 0, 0).Cols("uuid").
26+
Iterate(new(Attachment),
27+
func(idx int, bean interface{}) error {
28+
attachment := bean.(*Attachment)
29+
30+
if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
31+
return err
32+
}
33+
34+
_, err := sess.Delete(attachment)
35+
return err
36+
})
37+
38+
if err != nil {
39+
return err
40+
}
41+
42+
return sess.Commit()
43+
}

0 commit comments

Comments
 (0)