Skip to content

Commit

Permalink
Fix multiple asssingee table migration (go-gitea#3931)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored and lunny committed May 10, 2018
1 parent 95f2e2b commit 382e727
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions models/migrations/v64.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,6 @@ func addMultipleAssignees(x *xorm.Engine) error {
ClosedUnix util.TimeStamp `xorm:"INDEX"`
}

allIssues := []Issue{}
err := x.Find(&allIssues)
if err != nil {
return err
}

// Create the table
type IssueAssignees struct {
ID int64 `xorm:"pk autoincr"`
AssigneeID int64 `xorm:"INDEX"`
IssueID int64 `xorm:"INDEX"`
}
err = x.Sync2(IssueAssignees{})
if err != nil {
return err
}

// Range over all issues and insert a new entry for each issue/assignee
sess := x.NewSession()
defer sess.Close()

err = sess.Begin()
if err != nil {
return err
}

for _, issue := range allIssues {
if issue.AssigneeID != 0 {
_, err := sess.Insert(IssueAssignees{IssueID: issue.ID, AssigneeID: issue.AssigneeID})
if err != nil {
sess.Rollback()
return err
}
}
}

// Updated the comment table
type Comment struct {
ID int64 `xorm:"pk autoincr"`
Expand All @@ -96,10 +60,45 @@ func addMultipleAssignees(x *xorm.Engine) error {
// Reference issue in commit message
CommitSHA string `xorm:"VARCHAR(40)"`
}

// Create the table
type IssueAssignees struct {
ID int64 `xorm:"pk autoincr"`
AssigneeID int64 `xorm:"INDEX"`
IssueID int64 `xorm:"INDEX"`
}

if err := x.Sync2(IssueAssignees{}); err != nil {
return err
}

if err := x.Sync2(Comment{}); err != nil {
return err
}

// Range over all issues and insert a new entry for each issue/assignee
sess := x.NewSession()
defer sess.Close()

if err := sess.Begin(); err != nil {
return err
}

allIssues := []Issue{}
if err := sess.Find(&allIssues); err != nil {
return err
}

for _, issue := range allIssues {
if issue.AssigneeID != 0 {
_, err := sess.Insert(IssueAssignees{IssueID: issue.ID, AssigneeID: issue.AssigneeID})
if err != nil {
sess.Rollback()
return err
}
}
}

// Migrate comments
// First update everything to not have nulls in db
if _, err := sess.Where("type = ?", 9).Cols("removed_assignee").Update(Comment{RemovedAssignee: false}); err != nil {
Expand All @@ -114,7 +113,10 @@ func addMultipleAssignees(x *xorm.Engine) error {
for _, comment := range allAssignementComments {
// Everytime where OldAssigneeID is > 0, the assignement was removed.
if comment.OldAssigneeID > 0 {
_, err = sess.ID(comment.ID).Update(Comment{RemovedAssignee: true})
_, err := sess.ID(comment.ID).Update(Comment{RemovedAssignee: true})
if err != nil {
return err
}
}
}

Expand Down

0 comments on commit 382e727

Please sign in to comment.