Skip to content

Commit

Permalink
add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed May 14, 2019
1 parent 4ac0923 commit c632e48
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ var migrations = []Migration{
NewMigration("hash application token", hashAppToken),
// v86 -> v87
NewMigration("add http method to webhook", addHTTPMethodToWebhook),
// v87 -> v88
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))

}
43 changes: 43 additions & 0 deletions models/migrations/v87.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package models

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 c632e48

Please sign in to comment.