Fix bug on migration 147 (#12565)

This commit is contained in:
Lunny Xiao 2020-08-23 01:12:40 +08:00 committed by GitHub
parent 7a8a05cc44
commit 2026d885d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 37 deletions

View File

@ -82,14 +82,15 @@ func createReviewsForCodeComments(x *xorm.Engine) error {
if err := x.Sync2(new(Review), new(Comment)); err != nil { if err := x.Sync2(new(Review), new(Comment)); err != nil {
return err return err
} }
var updateComment = func(comments []*Comment) error {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if err := sess.Begin(); err != nil { if err := sess.Begin(); err != nil {
return err return err
} }
if err := sess.Where("review_id = 0 and type = 21").Iterate(new(Comment), func(idx int, bean interface{}) error {
comment := bean.(*Comment)
for _, comment := range comments {
review := &Review{ review := &Review{
Type: ReviewTypeComment, Type: ReviewTypeComment,
ReviewerID: comment.PosterID, ReviewerID: comment.PosterID,
@ -122,11 +123,32 @@ func createReviewsForCodeComments(x *xorm.Engine) error {
} }
comment.ReviewID = review.ID comment.ReviewID = review.ID
_, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment) if _, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment); err != nil {
return err
}); err != nil {
return err return err
} }
}
return sess.Commit() return sess.Commit()
} }
var start = 0
var batchSize = 100
for {
var comments = make([]*Comment, 0, batchSize)
if err := x.Where("review_id = 0 and type = 21").Limit(batchSize, start).Find(&comments); err != nil {
return err
}
if err := updateComment(comments); err != nil {
return err
}
start += len(comments)
if len(comments) < batchSize {
break
}
}
return nil
}