Skip to content

Commit

Permalink
add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Feb 14, 2019
1 parent 3a5394b commit e757621
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ var migrations = []Migration{
NewMigration("rename repo is_bare to repo is_empty", renameRepoIsBareToIsEmpty),
// v79 -> v80
NewMigration("add can close issues via commit in any branch", addCanCloseIssuesViaCommitInAnyBranch),
// v80 -> v81
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
}

// Migrate database to current version
Expand Down
47 changes: 47 additions & 0 deletions models/migrations/v80.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.IterateBufferSize).
Where("comment_id = ? AND release_id = ?", 0, 0).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.Delete(attachment)
return err
})

if err != nil {
return err
}

return sess.Commit()
}

0 comments on commit e757621

Please sign in to comment.