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,51 +82,73 @@ 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
} }
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
if err := sess.Where("review_id = 0 and type = 21").Iterate(new(Comment), func(idx int, bean interface{}) error {
comment := bean.(*Comment)
review := &Review{ var updateComment = func(comments []*Comment) error {
Type: ReviewTypeComment, sess := x.NewSession()
ReviewerID: comment.PosterID, defer sess.Close()
IssueID: comment.IssueID, if err := sess.Begin(); err != nil {
Official: false,
CommitID: comment.CommitSHA,
Stale: comment.Invalidated,
OriginalAuthor: comment.OriginalAuthor,
OriginalAuthorID: comment.OriginalAuthorID,
CreatedUnix: comment.CreatedUnix,
UpdatedUnix: comment.CreatedUnix,
}
if _, err := sess.NoAutoTime().Insert(review); err != nil {
return err return err
} }
reviewComment := &Comment{ for _, comment := range comments {
Type: 22, review := &Review{
PosterID: comment.PosterID, Type: ReviewTypeComment,
Content: "", ReviewerID: comment.PosterID,
IssueID: comment.IssueID, IssueID: comment.IssueID,
ReviewID: review.ID, Official: false,
OriginalAuthor: comment.OriginalAuthor, CommitID: comment.CommitSHA,
OriginalAuthorID: comment.OriginalAuthorID, Stale: comment.Invalidated,
CreatedUnix: comment.CreatedUnix, OriginalAuthor: comment.OriginalAuthor,
UpdatedUnix: comment.CreatedUnix, OriginalAuthorID: comment.OriginalAuthorID,
CreatedUnix: comment.CreatedUnix,
UpdatedUnix: comment.CreatedUnix,
}
if _, err := sess.NoAutoTime().Insert(review); err != nil {
return err
}
reviewComment := &Comment{
Type: 22,
PosterID: comment.PosterID,
Content: "",
IssueID: comment.IssueID,
ReviewID: review.ID,
OriginalAuthor: comment.OriginalAuthor,
OriginalAuthorID: comment.OriginalAuthorID,
CreatedUnix: comment.CreatedUnix,
UpdatedUnix: comment.CreatedUnix,
}
if _, err := sess.NoAutoTime().Insert(reviewComment); err != nil {
return err
}
comment.ReviewID = review.ID
if _, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment); err != nil {
return err
}
} }
if _, err := sess.NoAutoTime().Insert(reviewComment); err != nil {
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 return err
} }
comment.ReviewID = review.ID if err := updateComment(comments); err != nil {
_, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment) return err
return err }
}); err != nil {
return err start += len(comments)
if len(comments) < batchSize {
break
}
} }
return sess.Commit() return nil
} }