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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. )
  11. // InsertMilestones creates milestones of repository.
  12. func InsertMilestones(ms ...*issues_model.Milestone) (err error) {
  13. if len(ms) == 0 {
  14. return nil
  15. }
  16. ctx, committer, err := db.TxContext()
  17. if err != nil {
  18. return err
  19. }
  20. defer committer.Close()
  21. sess := db.GetEngine(ctx)
  22. // to return the id, so we should not use batch insert
  23. for _, m := range ms {
  24. if _, err = sess.NoAutoTime().Insert(m); err != nil {
  25. return err
  26. }
  27. }
  28. if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
  29. return err
  30. }
  31. return committer.Commit()
  32. }
  33. // InsertIssues insert issues to database
  34. func InsertIssues(issues ...*issues_model.Issue) error {
  35. ctx, committer, err := db.TxContext()
  36. if err != nil {
  37. return err
  38. }
  39. defer committer.Close()
  40. for _, issue := range issues {
  41. if err := insertIssue(ctx, issue); err != nil {
  42. return err
  43. }
  44. }
  45. return committer.Commit()
  46. }
  47. func insertIssue(ctx context.Context, issue *issues_model.Issue) error {
  48. sess := db.GetEngine(ctx)
  49. if _, err := sess.NoAutoTime().Insert(issue); err != nil {
  50. return err
  51. }
  52. issueLabels := make([]issues_model.IssueLabel, 0, len(issue.Labels))
  53. for _, label := range issue.Labels {
  54. issueLabels = append(issueLabels, issues_model.IssueLabel{
  55. IssueID: issue.ID,
  56. LabelID: label.ID,
  57. })
  58. }
  59. if len(issueLabels) > 0 {
  60. if _, err := sess.Insert(issueLabels); err != nil {
  61. return err
  62. }
  63. }
  64. for _, reaction := range issue.Reactions {
  65. reaction.IssueID = issue.ID
  66. }
  67. if len(issue.Reactions) > 0 {
  68. if _, err := sess.Insert(issue.Reactions); err != nil {
  69. return err
  70. }
  71. }
  72. if issue.ForeignReference != nil {
  73. issue.ForeignReference.LocalIndex = issue.Index
  74. if _, err := sess.Insert(issue.ForeignReference); err != nil {
  75. return err
  76. }
  77. }
  78. return nil
  79. }
  80. // InsertIssueComments inserts many comments of issues.
  81. func InsertIssueComments(comments []*issues_model.Comment) error {
  82. if len(comments) == 0 {
  83. return nil
  84. }
  85. issueIDs := make(map[int64]bool)
  86. for _, comment := range comments {
  87. issueIDs[comment.IssueID] = true
  88. }
  89. ctx, committer, err := db.TxContext()
  90. if err != nil {
  91. return err
  92. }
  93. defer committer.Close()
  94. for _, comment := range comments {
  95. if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil {
  96. return err
  97. }
  98. for _, reaction := range comment.Reactions {
  99. reaction.IssueID = comment.IssueID
  100. reaction.CommentID = comment.ID
  101. }
  102. if len(comment.Reactions) > 0 {
  103. if err := db.Insert(ctx, comment.Reactions); err != nil {
  104. return err
  105. }
  106. }
  107. }
  108. for issueID := range issueIDs {
  109. if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
  110. issueID, issues_model.CommentTypeComment, issueID); err != nil {
  111. return err
  112. }
  113. }
  114. return committer.Commit()
  115. }
  116. // InsertPullRequests inserted pull requests
  117. func InsertPullRequests(prs ...*issues_model.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. // UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
  159. func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, userID int64) error {
  160. if err := issues_model.UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
  161. return err
  162. }
  163. if err := issues_model.UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
  164. return err
  165. }
  166. if err := UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil {
  167. return err
  168. }
  169. if err := issues_model.UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
  170. return err
  171. }
  172. return issues_model.UpdateReviewsMigrationsByType(tp, externalUserID, userID)
  173. }