Skip to content

Commit e757621

Browse files
committed
add migration
1 parent 3a5394b commit e757621

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Diff for: models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff 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

Diff for: models/migrations/v80.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)