File tree Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -14,5 +14,4 @@ func addIsLockedToIssues(x *xorm.Engine) error {
1414 }
1515
1616 return x .Sync2 (new (Issue ))
17-
1817}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments