Skip to content

Commit bfcce5f

Browse files
lunnytechknowlogick
authored andcommitted
Remove release attachments which repository has been deleted (#9334)
1 parent f46176a commit bfcce5f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ var migrations = []Migration{
278278
NewMigration("change review content type to text", changeReviewContentToText),
279279
// v111 -> v112
280280
NewMigration("update branch protection for can push and whitelist enable", addBranchProtectionCanPushAndEnableWhitelist),
281+
// v112 -> v113
282+
NewMigration("remove release attachments which repository deleted", removeAttachmentMissedRepo),
281283
}
282284

283285
// Migrate database to current version

models/migrations/v112.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
"xorm.io/builder"
12+
"xorm.io/xorm"
13+
)
14+
15+
func removeAttachmentMissedRepo(x *xorm.Engine) error {
16+
type Attachment struct {
17+
UUID string `xorm:"uuid"`
18+
}
19+
var start int
20+
attachments := make([]*Attachment, 0, 50)
21+
for {
22+
err := x.Select("uuid").Where(builder.NotIn("release_id", builder.Select("id").From("`release`"))).
23+
OrderBy("id").Limit(50, start).Find(&attachments)
24+
if err != nil {
25+
return err
26+
}
27+
28+
for i := 0; i < len(attachments); i++ {
29+
os.RemoveAll(models.AttachmentLocalPath(attachments[i].UUID))
30+
}
31+
32+
if len(attachments) < 50 {
33+
break
34+
}
35+
start += 50
36+
attachments = attachments[:0]
37+
}
38+
39+
_, err := x.Exec("DELETE FROM attachment WHERE release_id NOT IN (SELECT id FROM `release`)")
40+
return err
41+
}

0 commit comments

Comments
 (0)