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.

v64.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/util"
  7. "github.com/go-xorm/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. Ref string
  25. DeadlineUnix util.TimeStamp `xorm:"INDEX"`
  26. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  27. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  28. ClosedUnix util.TimeStamp `xorm:"INDEX"`
  29. }
  30. // Updated the comment table
  31. type Comment struct {
  32. ID int64 `xorm:"pk autoincr"`
  33. Type int
  34. PosterID int64 `xorm:"INDEX"`
  35. IssueID int64 `xorm:"INDEX"`
  36. LabelID int64
  37. OldMilestoneID int64
  38. MilestoneID int64
  39. OldAssigneeID int64
  40. AssigneeID int64
  41. RemovedAssignee bool
  42. OldTitle string
  43. NewTitle string
  44. CommitID int64
  45. Line int64
  46. Content string `xorm:"TEXT"`
  47. RenderedContent string `xorm:"-"`
  48. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  49. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  50. // Reference issue in commit message
  51. CommitSHA string `xorm:"VARCHAR(40)"`
  52. }
  53. // Create the table
  54. type IssueAssignees struct {
  55. ID int64 `xorm:"pk autoincr"`
  56. AssigneeID int64 `xorm:"INDEX"`
  57. IssueID int64 `xorm:"INDEX"`
  58. }
  59. if err := x.Sync2(IssueAssignees{}); err != nil {
  60. return err
  61. }
  62. if err := x.Sync2(Comment{}); err != nil {
  63. return err
  64. }
  65. // Range over all issues and insert a new entry for each issue/assignee
  66. sess := x.NewSession()
  67. defer sess.Close()
  68. if err := sess.Begin(); err != nil {
  69. return err
  70. }
  71. allIssues := []Issue{}
  72. if err := sess.Find(&allIssues); err != nil {
  73. return err
  74. }
  75. for _, issue := range allIssues {
  76. if issue.AssigneeID != 0 {
  77. _, err := sess.Insert(IssueAssignees{IssueID: issue.ID, AssigneeID: issue.AssigneeID})
  78. if err != nil {
  79. sess.Rollback()
  80. return err
  81. }
  82. }
  83. }
  84. // Migrate comments
  85. // First update everything to not have nulls in db
  86. if _, err := sess.Where("type = ?", 9).Cols("removed_assignee").Update(Comment{RemovedAssignee: false}); err != nil {
  87. return err
  88. }
  89. allAssignementComments := []Comment{}
  90. if err := sess.Where("type = ?", 9).Find(&allAssignementComments); err != nil {
  91. return err
  92. }
  93. for _, comment := range allAssignementComments {
  94. // Everytime where OldAssigneeID is > 0, the assignement was removed.
  95. if comment.OldAssigneeID > 0 {
  96. _, err := sess.ID(comment.ID).Update(Comment{RemovedAssignee: true})
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. }
  102. // Commit and begin new transaction for dropping columns
  103. if err := sess.Commit(); err != nil {
  104. return err
  105. }
  106. if err := sess.Begin(); err != nil {
  107. return err
  108. }
  109. if err := dropTableColumns(sess, "issue", "assignee_id"); err != nil {
  110. return err
  111. }
  112. if err := dropTableColumns(sess, "issue_user", "is_assigned"); err != nil {
  113. return err
  114. }
  115. return sess.Commit()
  116. }