Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete releases attachments if release is deleted #6068

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ var migrations = []Migration{
NewMigration("add enable_status_check, status_check_contexts to protected_branch", addStatusCheckColumnsForProtectedBranches),
// v95 -> v96
NewMigration("add table columns for cross referencing issues", addCrossReferenceColumns),
// v96 -> v97
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
}

// Migrate database to current version
Expand Down
1 change: 0 additions & 1 deletion models/migrations/v80.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ func addIsLockedToIssues(x *xorm.Engine) error {
}

return x.Sync2(new(Issue))

}
48 changes: 48 additions & 0 deletions models/migrations/v96.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"os"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"

"github.com/go-xorm/xorm"
)

func deleteOrphanedAttachments(x *xorm.Engine) error {

type Attachment struct {
ID int64 `xorm:"pk autoincr"`
UUID string `xorm:"uuid UNIQUE"`
IssueID int64 `xorm:"INDEX"`
ReleaseID int64 `xorm:"INDEX"`
CommentID int64
}

sess := x.NewSession()
defer sess.Close()

err := sess.BufferSize(setting.Database.IterateBufferSize).
Where("`comment_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").Cols("uuid").
Iterate(new(Attachment),
func(idx int, bean interface{}) error {
attachment := bean.(*Attachment)

if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
return err
}

_, err := sess.ID(attachment.ID).NoAutoCondition().Delete(attachment)
return err
})

if err != nil {
return err
}

return sess.Commit()
}
13 changes: 13 additions & 0 deletions models/release.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"fmt"
"os"
adelowo marked this conversation as resolved.
Show resolved Hide resolved
"sort"
"strings"

Expand Down Expand Up @@ -357,6 +359,17 @@ func DeleteReleaseByID(id int64, doer *User, delTag bool) error {
return fmt.Errorf("LoadAttributes: %v", err)
}

if _, err := x.Delete(&Attachment{ReleaseID: id}); err != nil {
lafriks marked this conversation as resolved.
Show resolved Hide resolved
return err
}

for i := range rel.Attachments {
attachment := rel.Attachments[i]
if err := os.RemoveAll(attachment.LocalPath()); err != nil {
return err
}
}

mode, _ := AccessLevel(doer, rel.Repo)
if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
Action: api.HookReleaseDeleted,
Expand Down