File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,8 @@ var migrations = []Migration{
212212 NewMigration ("rename repo is_bare to repo is_empty" , renameRepoIsBareToIsEmpty ),
213213 // v79 -> v80
214214 NewMigration ("add can close issues via commit in any branch" , addCanCloseIssuesViaCommitInAnyBranch ),
215+ // v80 -> v81
216+ NewMigration ("delete orphaned attachments" , deleteOrphanedAttachments ),
215217}
216218
217219// Migrate database to current version
Original file line number Diff line number Diff line change 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+
5+ package migrations
6+
7+ import (
8+ "os"
9+
10+ "code.gitea.io/gitea/models"
11+ "code.gitea.io/gitea/modules/setting"
12+ "github.com/go-xorm/xorm"
13+ )
14+
15+ func deleteOrphanedAttachments (x * xorm.Engine ) error {
16+
17+ type Attachment struct {
18+ ID int64 `xorm:"pk autoincr"`
19+ UUID string `xorm:"uuid UNIQUE"`
20+ IssueID int64 `xorm:"INDEX"`
21+ ReleaseID int64 `xorm:"INDEX"`
22+ CommentID int64
23+ }
24+
25+ sess := x .NewSession ()
26+ defer sess .Close ()
27+
28+ err := sess .BufferSize (setting .IterateBufferSize ).
29+ Where ("comment_id = ? AND release_id = ?" , 0 , 0 ).Cols ("uuid" ).
30+ Iterate (new (Attachment ),
31+ func (idx int , bean interface {}) error {
32+ attachment := bean .(* Attachment )
33+
34+ if err := os .RemoveAll (models .AttachmentLocalPath (attachment .UUID )); err != nil {
35+ return err
36+ }
37+
38+ _ , err := sess .Delete (attachment )
39+ return err
40+ })
41+
42+ if err != nil {
43+ return err
44+ }
45+
46+ return sess .Commit ()
47+ }
You can’t perform that action at this time.
0 commit comments