Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,32 @@ var checklist = []check{
isDefault: false,
f: runDoctorEnablePushOptions,
},
{
title: "Recalculate Code-comment-replies commitID",
name: "recalculate_code_comment_replies",
isDefault: true,
f: runDoctorUpdateCodeCommentReplies,
},
// more checks please append here
}

func runDoctorUpdateCodeCommentReplies(ctx *cli.Context) ([]string, error) {
if ctx.Bool("fix") {
_, result, err := models.CountOrFixUpdatableCodeCommentReplies(true)
if err != nil {
return nil, err
}
return result, nil
} else {
count, _, err := models.CountOrFixUpdatableCodeCommentReplies(false)
if err != nil {
return nil, err
}
result := fmt.Sprintf("%d code comment replies without commitID exist", count)
return []string{result}, nil
}
Comment thread
6543 marked this conversation as resolved.
Outdated
}

func runRecreateTable(ctx *cli.Context) error {
// Redirect the default golog to here
golog.SetFlags(0)
Expand Down
61 changes: 61 additions & 0 deletions models/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package models

import (
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -295,3 +296,63 @@ func FixNullArchivedRepository() (int64, error) {
IsArchived: false,
})
}

func CountOrFixUpdatableCodeCommentReplies(fix bool) (int64, []string, error) {
Comment thread
6543 marked this conversation as resolved.
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return 0, nil, err
}

var (
start = 0
batchSize = 100
count int64
result []string
)

for {
var comments = make([]*Comment, 0, batchSize)
if err := sess.SQL(`SELECT comment.id as id, first.commit_sha as commit_sha, first.patch as patch, first.invalidated as invalidated
FROM comment INNER JOIN (
SELECT C.id, C.review_id, C.line, C.tree_path, C.patch, C.commit_sha, C.invalidated
FROM comment AS C
WHERE C.type = 21
AND C.created_unix =
(SELECT MIN(comment.created_unix)
FROM comment
WHERE comment.review_id = C.review_id
AND comment.type = 21
AND comment.line = C.line
AND comment.tree_path = C.tree_path)
) AS first
ON comment.review_id = first.review_id
AND comment.tree_path = first.tree_path AND comment.line = first.line
WHERE comment.type = 21
AND comment.id != first.id
AND comment.commit_sha != first.commit_sha`).Limit(batchSize, start).Find(&comments); err != nil {
return 0, nil, fmt.Errorf("failed to select: %v", err)
}

if fix {
for _, comment := range comments {
if _, err := sess.Table("comment").Cols("commit_sha", "patch", "invalidated").Update(comment); err != nil {
return 0, nil, fmt.Errorf("failed to update comment[%d]: %v %v", comment.ID, comment, err)
}
result = append(result, fmt.Sprintf("update comment[%d]: %s\n", comment.ID, comment.CommitSHA))
}
}

count += int64(len(comments))
start += len(comments)
if len(comments) < batchSize {
break
}
}

if fix {
return count, result, sess.Commit()
}

return count, nil, nil
}