You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v158.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_14 //nolint
  4. import (
  5. "fmt"
  6. "strconv"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. "xorm.io/xorm"
  10. )
  11. func UpdateCodeCommentReplies(x *xorm.Engine) error {
  12. type Comment struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. CommitSHA string `xorm:"VARCHAR(40)"`
  15. Patch string `xorm:"TEXT patch"`
  16. Invalidated bool
  17. // Not extracted but used in the below query
  18. Type int `xorm:"INDEX"`
  19. Line int64 // - previous line / + proposed line
  20. TreePath string
  21. ReviewID int64 `xorm:"index"`
  22. }
  23. if err := x.Sync(new(Comment)); err != nil {
  24. return err
  25. }
  26. sqlSelect := `SELECT comment.id as id, first.commit_sha as commit_sha, first.patch as patch, first.invalidated as invalidated`
  27. sqlTail := ` FROM comment INNER JOIN (
  28. SELECT C.id, C.review_id, C.line, C.tree_path, C.patch, C.commit_sha, C.invalidated
  29. FROM comment AS C
  30. WHERE C.type = 21
  31. AND C.created_unix =
  32. (SELECT MIN(comment.created_unix)
  33. FROM comment
  34. WHERE comment.review_id = C.review_id
  35. AND comment.type = 21
  36. AND comment.line = C.line
  37. AND comment.tree_path = C.tree_path)
  38. ) AS first
  39. ON comment.review_id = first.review_id
  40. AND comment.tree_path = first.tree_path AND comment.line = first.line
  41. WHERE comment.type = 21
  42. AND comment.id != first.id
  43. AND comment.commit_sha != first.commit_sha`
  44. var (
  45. sqlCmd string
  46. start = 0
  47. batchSize = 100
  48. sess = x.NewSession()
  49. )
  50. defer sess.Close()
  51. for {
  52. if err := sess.Begin(); err != nil {
  53. return err
  54. }
  55. if setting.Database.Type.IsMSSQL() {
  56. if _, err := sess.Exec(sqlSelect + " INTO #temp_comments" + sqlTail); err != nil {
  57. log.Error("unable to create temporary table")
  58. return err
  59. }
  60. }
  61. comments := make([]*Comment, 0, batchSize)
  62. switch {
  63. case setting.Database.Type.IsMySQL():
  64. sqlCmd = sqlSelect + sqlTail + " LIMIT " + strconv.Itoa(batchSize) + ", " + strconv.Itoa(start)
  65. case setting.Database.Type.IsPostgreSQL():
  66. fallthrough
  67. case setting.Database.Type.IsSQLite3():
  68. sqlCmd = sqlSelect + sqlTail + " LIMIT " + strconv.Itoa(batchSize) + " OFFSET " + strconv.Itoa(start)
  69. case setting.Database.Type.IsMSSQL():
  70. sqlCmd = "SELECT TOP " + strconv.Itoa(batchSize) + " * FROM #temp_comments WHERE " +
  71. "(id NOT IN ( SELECT TOP " + strconv.Itoa(start) + " id FROM #temp_comments ORDER BY id )) ORDER BY id"
  72. default:
  73. return fmt.Errorf("Unsupported database type")
  74. }
  75. if err := sess.SQL(sqlCmd).Find(&comments); err != nil {
  76. log.Error("failed to select: %v", err)
  77. return err
  78. }
  79. for _, comment := range comments {
  80. if _, err := sess.Table("comment").ID(comment.ID).Cols("commit_sha", "patch", "invalidated").Update(comment); err != nil {
  81. log.Error("failed to update comment[%d]: %v %v", comment.ID, comment, err)
  82. return err
  83. }
  84. }
  85. start += len(comments)
  86. if err := sess.Commit(); err != nil {
  87. return err
  88. }
  89. if len(comments) < batchSize {
  90. break
  91. }
  92. }
  93. return nil
  94. }