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.

migrate.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "code.gitea.io/gitea/modules/structs"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. // InsertMilestones creates milestones of repository.
  11. func InsertMilestones(ms ...*Milestone) (err error) {
  12. if len(ms) == 0 {
  13. return nil
  14. }
  15. sess := x.NewSession()
  16. defer sess.Close()
  17. if err = sess.Begin(); err != nil {
  18. return err
  19. }
  20. // to return the id, so we should not use batch insert
  21. for _, m := range ms {
  22. if _, err = sess.NoAutoTime().Insert(m); err != nil {
  23. return err
  24. }
  25. }
  26. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
  27. return err
  28. }
  29. return sess.Commit()
  30. }
  31. // InsertIssues insert issues to database
  32. func InsertIssues(issues ...*Issue) error {
  33. sess := x.NewSession()
  34. if err := sess.Begin(); err != nil {
  35. return err
  36. }
  37. for _, issue := range issues {
  38. if err := insertIssue(sess, issue); err != nil {
  39. return err
  40. }
  41. }
  42. return sess.Commit()
  43. }
  44. func insertIssue(sess *xorm.Session, issue *Issue) error {
  45. if _, err := sess.NoAutoTime().Insert(issue); err != nil {
  46. return err
  47. }
  48. var issueLabels = make([]IssueLabel, 0, len(issue.Labels))
  49. var labelIDs = make([]int64, 0, len(issue.Labels))
  50. for _, label := range issue.Labels {
  51. issueLabels = append(issueLabels, IssueLabel{
  52. IssueID: issue.ID,
  53. LabelID: label.ID,
  54. })
  55. labelIDs = append(labelIDs, label.ID)
  56. }
  57. if _, err := sess.Insert(issueLabels); err != nil {
  58. return err
  59. }
  60. for _, reaction := range issue.Reactions {
  61. reaction.IssueID = issue.ID
  62. }
  63. if _, err := sess.Insert(issue.Reactions); err != nil {
  64. return err
  65. }
  66. cols := make([]string, 0)
  67. if !issue.IsPull {
  68. sess.ID(issue.RepoID).Incr("num_issues")
  69. cols = append(cols, "num_issues")
  70. if issue.IsClosed {
  71. sess.Incr("num_closed_issues")
  72. cols = append(cols, "num_closed_issues")
  73. }
  74. } else {
  75. sess.ID(issue.RepoID).Incr("num_pulls")
  76. cols = append(cols, "num_pulls")
  77. if issue.IsClosed {
  78. sess.Incr("num_closed_pulls")
  79. cols = append(cols, "num_closed_pulls")
  80. }
  81. }
  82. if _, err := sess.NoAutoTime().Cols(cols...).Update(issue.Repo); err != nil {
  83. return err
  84. }
  85. cols = []string{"num_issues"}
  86. sess.Incr("num_issues")
  87. if issue.IsClosed {
  88. sess.Incr("num_closed_issues")
  89. cols = append(cols, "num_closed_issues")
  90. }
  91. if _, err := sess.In("id", labelIDs).NoAutoTime().Cols(cols...).Update(new(Label)); err != nil {
  92. return err
  93. }
  94. if issue.MilestoneID > 0 {
  95. cols = []string{"num_issues"}
  96. sess.Incr("num_issues")
  97. cl := "num_closed_issues"
  98. if issue.IsClosed {
  99. sess.Incr("num_closed_issues")
  100. cols = append(cols, "num_closed_issues")
  101. cl = "(num_closed_issues + 1)"
  102. }
  103. if _, err := sess.ID(issue.MilestoneID).
  104. SetExpr("completeness", cl+" * 100 / (num_issues + 1)").
  105. NoAutoTime().Cols(cols...).
  106. Update(new(Milestone)); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }
  112. // InsertIssueComments inserts many comments of issues.
  113. func InsertIssueComments(comments []*Comment) error {
  114. if len(comments) == 0 {
  115. return nil
  116. }
  117. var issueIDs = make(map[int64]bool)
  118. for _, comment := range comments {
  119. issueIDs[comment.IssueID] = true
  120. }
  121. sess := x.NewSession()
  122. defer sess.Close()
  123. if err := sess.Begin(); err != nil {
  124. return err
  125. }
  126. for _, comment := range comments {
  127. if _, err := sess.NoAutoTime().Insert(comment); err != nil {
  128. return err
  129. }
  130. for _, reaction := range comment.Reactions {
  131. reaction.IssueID = comment.IssueID
  132. reaction.CommentID = comment.ID
  133. }
  134. if _, err := sess.Insert(comment.Reactions); err != nil {
  135. return err
  136. }
  137. }
  138. for issueID := range issueIDs {
  139. if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
  140. return err
  141. }
  142. }
  143. return sess.Commit()
  144. }
  145. // InsertPullRequests inserted pull requests
  146. func InsertPullRequests(prs ...*PullRequest) error {
  147. sess := x.NewSession()
  148. defer sess.Close()
  149. if err := sess.Begin(); err != nil {
  150. return err
  151. }
  152. for _, pr := range prs {
  153. if err := insertIssue(sess, pr.Issue); err != nil {
  154. return err
  155. }
  156. pr.IssueID = pr.Issue.ID
  157. if _, err := sess.NoAutoTime().Insert(pr); err != nil {
  158. return err
  159. }
  160. }
  161. return sess.Commit()
  162. }
  163. // InsertReleases migrates release
  164. func InsertReleases(rels ...*Release) error {
  165. sess := x.NewSession()
  166. if err := sess.Begin(); err != nil {
  167. return err
  168. }
  169. for _, rel := range rels {
  170. if _, err := sess.NoAutoTime().Insert(rel); err != nil {
  171. return err
  172. }
  173. for i := 0; i < len(rel.Attachments); i++ {
  174. rel.Attachments[i].ReleaseID = rel.ID
  175. }
  176. if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
  177. return err
  178. }
  179. }
  180. return sess.Commit()
  181. }
  182. func migratedIssueCond(tp structs.GitServiceType) builder.Cond {
  183. return builder.In("issue_id",
  184. builder.Select("issue.id").
  185. From("issue").
  186. InnerJoin("repository", "issue.repo_id = repository.id").
  187. Where(builder.Eq{
  188. "repository.original_service_type": tp,
  189. }),
  190. )
  191. }
  192. // UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
  193. func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
  194. _, err := x.Table("review").
  195. Where("original_author_id = ?", originalAuthorID).
  196. And(migratedIssueCond(tp)).
  197. Update(map[string]interface{}{
  198. "reviewer_id": posterID,
  199. "original_author": "",
  200. "original_author_id": 0,
  201. })
  202. return err
  203. }