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.

138 lines
3.5 KiB

  1. // Copyright 2018 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 migrations
  5. import (
  6. "code.gitea.io/gitea/modules/timeutil"
  7. "xorm.io/xorm"
  8. )
  9. func addMultipleAssignees(x *xorm.Engine) error {
  10. // Redeclare issue struct
  11. type Issue struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
  14. Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
  15. PosterID int64 `xorm:"INDEX"`
  16. Title string `xorm:"name"`
  17. Content string `xorm:"TEXT"`
  18. MilestoneID int64 `xorm:"INDEX"`
  19. Priority int
  20. AssigneeID int64 `xorm:"INDEX"`
  21. IsClosed bool `xorm:"INDEX"`
  22. IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
  23. NumComments int
  24. DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`
  25. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  26. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  27. ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
  28. }
  29. // Updated the comment table
  30. type Comment struct {
  31. ID int64 `xorm:"pk autoincr"`
  32. Type int
  33. PosterID int64 `xorm:"INDEX"`
  34. IssueID int64 `xorm:"INDEX"`
  35. LabelID int64
  36. OldMilestoneID int64
  37. MilestoneID int64
  38. OldAssigneeID int64
  39. AssigneeID int64
  40. RemovedAssignee bool
  41. OldTitle string
  42. NewTitle string
  43. CommitID int64
  44. Line int64
  45. Content string `xorm:"TEXT"`
  46. RenderedContent string `xorm:"-"`
  47. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  48. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  49. // Reference issue in commit message
  50. CommitSHA string `xorm:"VARCHAR(40)"`
  51. }
  52. // Create the table
  53. type IssueAssignees struct {
  54. ID int64 `xorm:"pk autoincr"`
  55. AssigneeID int64 `xorm:"INDEX"`
  56. IssueID int64 `xorm:"INDEX"`
  57. }
  58. if err := x.Sync2(IssueAssignees{}); err != nil {
  59. return err
  60. }
  61. if err := x.Sync2(Comment{}); err != nil {
  62. return err
  63. }
  64. // Range over all issues and insert a new entry for each issue/assignee
  65. sess := x.NewSession()
  66. defer sess.Close()
  67. if err := sess.Begin(); err != nil {
  68. return err
  69. }
  70. allIssues := []*Issue{}
  71. if err := sess.Find(&allIssues); err != nil {
  72. return err
  73. }
  74. for _, issue := range allIssues {
  75. if issue.AssigneeID != 0 {
  76. _, err := sess.Insert(IssueAssignees{IssueID: issue.ID, AssigneeID: issue.AssigneeID})
  77. if err != nil {
  78. sess.Rollback()
  79. return err
  80. }
  81. }
  82. }
  83. // Migrate comments
  84. // First update everything to not have nulls in db
  85. if _, err := sess.Where("type = ?", 9).Cols("removed_assignee").Update(Comment{RemovedAssignee: false}); err != nil {
  86. return err
  87. }
  88. allAssignementComments := []*Comment{}
  89. if err := sess.Where("type = ?", 9).Find(&allAssignementComments); err != nil {
  90. return err
  91. }
  92. for _, comment := range allAssignementComments {
  93. // Everytime where OldAssigneeID is > 0, the assignement was removed.
  94. if comment.OldAssigneeID > 0 {
  95. _, err := sess.ID(comment.ID).Update(Comment{RemovedAssignee: true})
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. }
  101. // Commit and begin new transaction for dropping columns
  102. if err := sess.Commit(); err != nil {
  103. return err
  104. }
  105. if err := sess.Begin(); err != nil {
  106. return err
  107. }
  108. if err := dropTableColumns(sess, "issue", "assignee_id"); err != nil {
  109. return err
  110. }
  111. if err := dropTableColumns(sess, "issue_user", "is_assigned"); err != nil {
  112. return err
  113. }
  114. return sess.Commit()
  115. }