Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

migrate.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "context"
  7. "code.gitea.io/gitea/models/db"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. "code.gitea.io/gitea/modules/structs"
  10. "xorm.io/builder"
  11. )
  12. // InsertMilestones creates milestones of repository.
  13. func InsertMilestones(ms ...*issues_model.Milestone) (err error) {
  14. if len(ms) == 0 {
  15. return nil
  16. }
  17. ctx, committer, err := db.TxContext()
  18. if err != nil {
  19. return err
  20. }
  21. defer committer.Close()
  22. sess := db.GetEngine(ctx)
  23. // to return the id, so we should not use batch insert
  24. for _, m := range ms {
  25. if _, err = sess.NoAutoTime().Insert(m); err != nil {
  26. return err
  27. }
  28. }
  29. if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
  30. return err
  31. }
  32. return committer.Commit()
  33. }
  34. // InsertIssues insert issues to database
  35. func InsertIssues(issues ...*Issue) error {
  36. ctx, committer, err := db.TxContext()
  37. if err != nil {
  38. return err
  39. }
  40. defer committer.Close()
  41. for _, issue := range issues {
  42. if err := insertIssue(ctx, issue); err != nil {
  43. return err
  44. }
  45. }
  46. return committer.Commit()
  47. }
  48. func insertIssue(ctx context.Context, issue *Issue) error {
  49. sess := db.GetEngine(ctx)
  50. if _, err := sess.NoAutoTime().Insert(issue); err != nil {
  51. return err
  52. }
  53. issueLabels := make([]IssueLabel, 0, len(issue.Labels))
  54. for _, label := range issue.Labels {
  55. issueLabels = append(issueLabels, IssueLabel{
  56. IssueID: issue.ID,
  57. LabelID: label.ID,
  58. })
  59. }
  60. if len(issueLabels) > 0 {
  61. if _, err := sess.Insert(issueLabels); err != nil {
  62. return err
  63. }
  64. }
  65. for _, reaction := range issue.Reactions {
  66. reaction.IssueID = issue.ID
  67. }
  68. if len(issue.Reactions) > 0 {
  69. if _, err := sess.Insert(issue.Reactions); err != nil {
  70. return err
  71. }
  72. }
  73. if issue.ForeignReference != nil {
  74. issue.ForeignReference.LocalIndex = issue.Index
  75. if _, err := sess.Insert(issue.ForeignReference); err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }
  81. // InsertIssueComments inserts many comments of issues.
  82. func InsertIssueComments(comments []*Comment) error {
  83. if len(comments) == 0 {
  84. return nil
  85. }
  86. issueIDs := make(map[int64]bool)
  87. for _, comment := range comments {
  88. issueIDs[comment.IssueID] = true
  89. }
  90. ctx, committer, err := db.TxContext()
  91. if err != nil {
  92. return err
  93. }
  94. defer committer.Close()
  95. for _, comment := range comments {
  96. if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil {
  97. return err
  98. }
  99. for _, reaction := range comment.Reactions {
  100. reaction.IssueID = comment.IssueID
  101. reaction.CommentID = comment.ID
  102. }
  103. if len(comment.Reactions) > 0 {
  104. if err := db.Insert(ctx, comment.Reactions); err != nil {
  105. return err
  106. }
  107. }
  108. }
  109. for issueID := range issueIDs {
  110. if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?", issueID, CommentTypeComment, issueID); err != nil {
  111. return err
  112. }
  113. }
  114. return committer.Commit()
  115. }
  116. // InsertPullRequests inserted pull requests
  117. func InsertPullRequests(prs ...*PullRequest) error {
  118. ctx, committer, err := db.TxContext()
  119. if err != nil {
  120. return err
  121. }
  122. defer committer.Close()
  123. sess := db.GetEngine(ctx)
  124. for _, pr := range prs {
  125. if err := insertIssue(ctx, pr.Issue); err != nil {
  126. return err
  127. }
  128. pr.IssueID = pr.Issue.ID
  129. if _, err := sess.NoAutoTime().Insert(pr); err != nil {
  130. return err
  131. }
  132. }
  133. return committer.Commit()
  134. }
  135. // InsertReleases migrates release
  136. func InsertReleases(rels ...*Release) error {
  137. ctx, committer, err := db.TxContext()
  138. if err != nil {
  139. return err
  140. }
  141. defer committer.Close()
  142. sess := db.GetEngine(ctx)
  143. for _, rel := range rels {
  144. if _, err := sess.NoAutoTime().Insert(rel); err != nil {
  145. return err
  146. }
  147. if len(rel.Attachments) > 0 {
  148. for i := range rel.Attachments {
  149. rel.Attachments[i].ReleaseID = rel.ID
  150. }
  151. if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
  152. return err
  153. }
  154. }
  155. }
  156. return committer.Commit()
  157. }
  158. func migratedIssueCond(tp structs.GitServiceType) builder.Cond {
  159. return builder.In("issue_id",
  160. builder.Select("issue.id").
  161. From("issue").
  162. InnerJoin("repository", "issue.repo_id = repository.id").
  163. Where(builder.Eq{
  164. "repository.original_service_type": tp,
  165. }),
  166. )
  167. }
  168. // UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
  169. func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
  170. _, err := db.GetEngine(db.DefaultContext).Table("review").
  171. Where("original_author_id = ?", originalAuthorID).
  172. And(migratedIssueCond(tp)).
  173. Update(map[string]interface{}{
  174. "reviewer_id": posterID,
  175. "original_author": "",
  176. "original_author_id": 0,
  177. })
  178. return err
  179. }
  180. // UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
  181. func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, userID int64) error {
  182. if err := UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
  183. return err
  184. }
  185. if err := UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
  186. return err
  187. }
  188. if err := UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil {
  189. return err
  190. }
  191. if err := UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
  192. return err
  193. }
  194. return UpdateReviewsMigrationsByType(tp, externalUserID, userID)
  195. }