From 382e7276b82d747f5282357ff62fef01d3b48f6a Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Thu, 10 May 2018 04:18:01 +0300 Subject: [PATCH] Fix multiple asssingee table migration (#3931) --- models/migrations/v64.go | 76 +++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/models/migrations/v64.go b/models/migrations/v64.go index a281ac67e49..67ce3a9196a 100644 --- a/models/migrations/v64.go +++ b/models/migrations/v64.go @@ -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"` @@ -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 { @@ -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 + } } }