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.

297 lines
9.7 KiB

  1. // Copyright 2017 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. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "xorm.io/builder"
  11. )
  12. // consistencyCheckable a type that can be tested for database consistency
  13. type consistencyCheckable interface {
  14. checkForConsistency(t *testing.T)
  15. }
  16. // CheckConsistencyForAll test that the entire database is consistent
  17. func CheckConsistencyForAll(t *testing.T) {
  18. CheckConsistencyFor(t,
  19. &User{},
  20. &Repository{},
  21. &Issue{},
  22. &PullRequest{},
  23. &Milestone{},
  24. &Label{},
  25. &Team{},
  26. &Action{})
  27. }
  28. // CheckConsistencyFor test that all matching database entries are consistent
  29. func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
  30. for _, bean := range beansToCheck {
  31. sliceType := reflect.SliceOf(reflect.TypeOf(bean))
  32. sliceValue := reflect.MakeSlice(sliceType, 0, 10)
  33. ptrToSliceValue := reflect.New(sliceType)
  34. ptrToSliceValue.Elem().Set(sliceValue)
  35. assert.NoError(t, x.Table(bean).Find(ptrToSliceValue.Interface()))
  36. sliceValue = ptrToSliceValue.Elem()
  37. for i := 0; i < sliceValue.Len(); i++ {
  38. entity := sliceValue.Index(i).Interface()
  39. checkable, ok := entity.(consistencyCheckable)
  40. if !ok {
  41. t.Errorf("Expected %+v (of type %T) to be checkable for consistency",
  42. entity, entity)
  43. } else {
  44. checkable.checkForConsistency(t)
  45. }
  46. }
  47. }
  48. }
  49. // getCount get the count of database entries matching bean
  50. func getCount(t *testing.T, e Engine, bean interface{}) int64 {
  51. count, err := e.Count(bean)
  52. assert.NoError(t, err)
  53. return count
  54. }
  55. // assertCount test the count of database entries matching bean
  56. func assertCount(t *testing.T, bean interface{}, expected int) {
  57. assert.EqualValues(t, expected, getCount(t, x, bean),
  58. "Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
  59. }
  60. func (user *User) checkForConsistency(t *testing.T) {
  61. assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
  62. assertCount(t, &Star{UID: user.ID}, user.NumStars)
  63. assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
  64. assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
  65. assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing)
  66. assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers)
  67. if user.Type != UserTypeOrganization {
  68. assert.EqualValues(t, 0, user.NumMembers)
  69. assert.EqualValues(t, 0, user.NumTeams)
  70. }
  71. }
  72. func (repo *Repository) checkForConsistency(t *testing.T) {
  73. assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
  74. assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
  75. assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
  76. assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
  77. if repo.IsFork {
  78. AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
  79. }
  80. actual := getCount(t, x.Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID})
  81. assert.EqualValues(t, repo.NumWatches, actual,
  82. "Unexpected number of watches for repo %+v", repo)
  83. actual = getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
  84. assert.EqualValues(t, repo.NumIssues, actual,
  85. "Unexpected number of issues for repo %+v", repo)
  86. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
  87. assert.EqualValues(t, repo.NumClosedIssues, actual,
  88. "Unexpected number of closed issues for repo %+v", repo)
  89. actual = getCount(t, x.Where("is_pull=?", true), &Issue{RepoID: repo.ID})
  90. assert.EqualValues(t, repo.NumPulls, actual,
  91. "Unexpected number of pulls for repo %+v", repo)
  92. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
  93. assert.EqualValues(t, repo.NumClosedPulls, actual,
  94. "Unexpected number of closed pulls for repo %+v", repo)
  95. actual = getCount(t, x.Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
  96. assert.EqualValues(t, repo.NumClosedMilestones, actual,
  97. "Unexpected number of closed milestones for repo %+v", repo)
  98. }
  99. func (issue *Issue) checkForConsistency(t *testing.T) {
  100. actual := getCount(t, x.Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
  101. assert.EqualValues(t, issue.NumComments, actual,
  102. "Unexpected number of comments for issue %+v", issue)
  103. if issue.IsPull {
  104. pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest)
  105. assert.EqualValues(t, pr.Index, issue.Index)
  106. }
  107. }
  108. func (pr *PullRequest) checkForConsistency(t *testing.T) {
  109. issue := AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
  110. assert.True(t, issue.IsPull)
  111. assert.EqualValues(t, issue.Index, pr.Index)
  112. }
  113. func (milestone *Milestone) checkForConsistency(t *testing.T) {
  114. assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
  115. actual := getCount(t, x.Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
  116. assert.EqualValues(t, milestone.NumClosedIssues, actual,
  117. "Unexpected number of closed issues for milestone %+v", milestone)
  118. }
  119. func (label *Label) checkForConsistency(t *testing.T) {
  120. issueLabels := make([]*IssueLabel, 0, 10)
  121. assert.NoError(t, x.Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
  122. assert.EqualValues(t, label.NumIssues, len(issueLabels),
  123. "Unexpected number of issue for label %+v", label)
  124. issueIDs := make([]int64, len(issueLabels))
  125. for i, issueLabel := range issueLabels {
  126. issueIDs[i] = issueLabel.IssueID
  127. }
  128. expected := int64(0)
  129. if len(issueIDs) > 0 {
  130. expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
  131. }
  132. assert.EqualValues(t, expected, label.NumClosedIssues,
  133. "Unexpected number of closed issues for label %+v", label)
  134. }
  135. func (team *Team) checkForConsistency(t *testing.T) {
  136. assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
  137. assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
  138. }
  139. func (action *Action) checkForConsistency(t *testing.T) {
  140. repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
  141. assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
  142. }
  143. // CountOrphanedLabels return count of labels witch are broken and not accessible via ui anymore
  144. func CountOrphanedLabels() (int64, error) {
  145. noref, err := x.Table("label").Where("repo_id=? AND org_id=?", 0, 0).Count("label.id")
  146. if err != nil {
  147. return 0, err
  148. }
  149. norepo, err := x.Table("label").
  150. Join("LEFT", "repository", "label.repo_id=repository.id").
  151. Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0}).
  152. Count("id")
  153. if err != nil {
  154. return 0, err
  155. }
  156. noorg, err := x.Table("label").
  157. Join("LEFT", "`user`", "label.org_id=`user`.id").
  158. Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0}).
  159. Count("id")
  160. if err != nil {
  161. return 0, err
  162. }
  163. return noref + norepo + noorg, nil
  164. }
  165. // DeleteOrphanedLabels delete labels witch are broken and not accessible via ui anymore
  166. func DeleteOrphanedLabels() error {
  167. // delete labels with no reference
  168. if _, err := x.Table("label").Where("repo_id=? AND org_id=?", 0, 0).Delete(new(Label)); err != nil {
  169. return err
  170. }
  171. // delete labels with none existing repos
  172. if _, err := x.In("id", builder.Select("label.id").From("label").
  173. Join("LEFT", "repository", "label.repo_id=repository.id").
  174. Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0})).
  175. Delete(Label{}); err != nil {
  176. return err
  177. }
  178. // delete labels with none existing orgs
  179. if _, err := x.In("id", builder.Select("label.id").From("label").
  180. Join("LEFT", "`user`", "label.org_id=`user`.id").
  181. Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0})).
  182. Delete(Label{}); err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. // CountOrphanedIssues count issues without a repo
  188. func CountOrphanedIssues() (int64, error) {
  189. return x.Table("issue").
  190. Join("LEFT", "repository", "issue.repo_id=repository.id").
  191. Where(builder.IsNull{"repository.id"}).
  192. Count("id")
  193. }
  194. // DeleteOrphanedIssues delete issues without a repo
  195. func DeleteOrphanedIssues() error {
  196. sess := x.NewSession()
  197. defer sess.Close()
  198. if err := sess.Begin(); err != nil {
  199. return err
  200. }
  201. var ids []int64
  202. if err := sess.Table("issue").Distinct("issue.repo_id").
  203. Join("LEFT", "repository", "issue.repo_id=repository.id").
  204. Where(builder.IsNull{"repository.id"}).GroupBy("issue.repo_id").
  205. Find(&ids); err != nil {
  206. return err
  207. }
  208. var attachmentPaths []string
  209. for i := range ids {
  210. paths, err := deleteIssuesByRepoID(sess, ids[i])
  211. if err != nil {
  212. return err
  213. }
  214. attachmentPaths = append(attachmentPaths, paths...)
  215. }
  216. if err := sess.Commit(); err != nil {
  217. return err
  218. }
  219. // Remove issue attachment files.
  220. for i := range attachmentPaths {
  221. removeAllWithNotice(x, "Delete issue attachment", attachmentPaths[i])
  222. }
  223. return nil
  224. }
  225. // CountOrphanedObjects count subjects with have no existing refobject anymore
  226. func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
  227. return x.Table("`"+subject+"`").
  228. Join("LEFT", refobject, joinCond).
  229. Where(builder.IsNull{"`" + refobject + "`.id"}).
  230. Count("id")
  231. }
  232. // DeleteOrphanedObjects delete subjects with have no existing refobject anymore
  233. func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
  234. _, err := x.In("id", builder.Select("`"+subject+"`.id").
  235. From("`"+subject+"`").
  236. Join("LEFT", "`"+refobject+"`", joinCond).
  237. Where(builder.IsNull{"`" + refobject + "`.id"})).
  238. Delete("`" + subject + "`")
  239. return err
  240. }
  241. // CountNullArchivedRepository counts the number of repositories with is_archived is null
  242. func CountNullArchivedRepository() (int64, error) {
  243. return x.Where(builder.IsNull{"is_archived"}).Count(new(Repository))
  244. }
  245. // FixNullArchivedRepository sets is_archived to false where it is null
  246. func FixNullArchivedRepository() (int64, error) {
  247. return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{
  248. IsArchived: false,
  249. })
  250. }