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.

v176.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2021 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. "xorm.io/xorm"
  7. )
  8. // removeInvalidLabels looks through the database to look for comments and issue_labels
  9. // that refer to labels do not belong to the repository or organization that repository
  10. // that the issue is in
  11. func removeInvalidLabels(x *xorm.Engine) error {
  12. type Comment struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. Type int `xorm:"INDEX"`
  15. IssueID int64 `xorm:"INDEX"`
  16. LabelID int64
  17. }
  18. type Issue struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
  21. Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
  22. }
  23. type Repository struct {
  24. ID int64 `xorm:"pk autoincr"`
  25. OwnerID int64 `xorm:"UNIQUE(s) index"`
  26. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  27. }
  28. type Label struct {
  29. ID int64 `xorm:"pk autoincr"`
  30. RepoID int64 `xorm:"INDEX"`
  31. OrgID int64 `xorm:"INDEX"`
  32. }
  33. type IssueLabel struct {
  34. ID int64 `xorm:"pk autoincr"`
  35. IssueID int64 `xorm:"UNIQUE(s)"`
  36. LabelID int64 `xorm:"UNIQUE(s)"`
  37. }
  38. if err := x.Sync2(new(Comment), new(Issue), new(Repository), new(Label), new(IssueLabel)); err != nil {
  39. return err
  40. }
  41. if _, err := x.Exec(`DELETE FROM issue_label WHERE issue_label.id IN (
  42. SELECT il_too.id FROM (
  43. SELECT il_too_too.id
  44. FROM issue_label AS il_too_too
  45. INNER JOIN label ON il_too_too.label_id = label.id
  46. INNER JOIN issue on issue.id = il_too_too.issue_id
  47. INNER JOIN repository on repository.id = issue.repo_id
  48. WHERE
  49. (label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id)
  50. ) AS il_too )`); err != nil {
  51. return err
  52. }
  53. if _, err := x.Exec(`DELETE FROM comment WHERE comment.id IN (
  54. SELECT il_too.id FROM (
  55. SELECT com.id
  56. FROM comment AS com
  57. INNER JOIN label ON com.label_id = label.id
  58. INNER JOIN issue on issue.id = com.issue_id
  59. INNER JOIN repository on repository.id = issue.repo_id
  60. WHERE
  61. com.type = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id))
  62. ) AS il_too)`, 7); err != nil {
  63. return err
  64. }
  65. return nil
  66. }