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.

dbconsistency.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2020 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 doctor
  5. import (
  6. "context"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/migrations"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. type consistencyCheck struct {
  15. Name string
  16. Counter func() (int64, error)
  17. Fixer func() (int64, error)
  18. FixedMessage string
  19. }
  20. func (c *consistencyCheck) Run(logger log.Logger, autofix bool) error {
  21. count, err := c.Counter()
  22. if err != nil {
  23. logger.Critical("Error: %v whilst counting %s", err, c.Name)
  24. return err
  25. }
  26. if count > 0 {
  27. if autofix {
  28. var fixed int64
  29. if fixed, err = c.Fixer(); err != nil {
  30. logger.Critical("Error: %v whilst fixing %s", err, c.Name)
  31. return err
  32. }
  33. prompt := "Deleted"
  34. if c.FixedMessage != "" {
  35. prompt = c.FixedMessage
  36. }
  37. if fixed < 0 {
  38. logger.Info(prompt+" %d %s", count, c.Name)
  39. } else {
  40. logger.Info(prompt+" %d/%d %s", fixed, count, c.Name)
  41. }
  42. } else {
  43. logger.Warn("Found %d %s", count, c.Name)
  44. }
  45. }
  46. return nil
  47. }
  48. func asFixer(fn func() error) func() (int64, error) {
  49. return func() (int64, error) {
  50. err := fn()
  51. return -1, err
  52. }
  53. }
  54. func genericOrphanCheck(name, subject, refobject, joincond string) consistencyCheck {
  55. return consistencyCheck{
  56. Name: name,
  57. Counter: func() (int64, error) {
  58. return models.CountOrphanedObjects(subject, refobject, joincond)
  59. },
  60. Fixer: func() (int64, error) {
  61. err := models.DeleteOrphanedObjects(subject, refobject, joincond)
  62. return -1, err
  63. },
  64. }
  65. }
  66. func checkDBConsistency(logger log.Logger, autofix bool) error {
  67. // make sure DB version is uptodate
  68. if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
  69. logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
  70. return err
  71. }
  72. consistencyChecks := []consistencyCheck{
  73. {
  74. // find labels without existing repo or org
  75. Name: "Orphaned Labels without existing repository or organisation",
  76. Counter: models.CountOrphanedLabels,
  77. Fixer: asFixer(models.DeleteOrphanedLabels),
  78. },
  79. {
  80. // find IssueLabels without existing label
  81. Name: "Orphaned Issue Labels without existing label",
  82. Counter: models.CountOrphanedIssueLabels,
  83. Fixer: asFixer(models.DeleteOrphanedIssueLabels),
  84. },
  85. {
  86. // find issues without existing repository
  87. Name: "Orphaned Issues without existing repository",
  88. Counter: models.CountOrphanedIssues,
  89. Fixer: asFixer(models.DeleteOrphanedIssues),
  90. },
  91. // find releases without existing repository
  92. genericOrphanCheck("Orphaned Releases without existing repository",
  93. "release", "repository", "release.repo_id=repository.id"),
  94. // find pulls without existing issues
  95. genericOrphanCheck("Orphaned PullRequests without existing issue",
  96. "pull_request", "issue", "pull_request.issue_id=issue.id"),
  97. // find tracked times without existing issues/pulls
  98. genericOrphanCheck("Orphaned TrackedTimes without existing issue",
  99. "tracked_time", "issue", "tracked_time.issue_id=issue.id"),
  100. // find attachments without existing issues or releases
  101. {
  102. Name: "Orphaned Attachments without existing issues or releases",
  103. Counter: repo_model.CountOrphanedAttachments,
  104. Fixer: asFixer(repo_model.DeleteOrphanedAttachments),
  105. },
  106. // find null archived repositories
  107. {
  108. Name: "Repositories with is_archived IS NULL",
  109. Counter: models.CountNullArchivedRepository,
  110. Fixer: models.FixNullArchivedRepository,
  111. FixedMessage: "Fixed",
  112. },
  113. // find label comments with empty labels
  114. {
  115. Name: "Label comments with empty labels",
  116. Counter: models.CountCommentTypeLabelWithEmptyLabel,
  117. Fixer: models.FixCommentTypeLabelWithEmptyLabel,
  118. FixedMessage: "Fixed",
  119. },
  120. // find label comments with labels from outside the repository
  121. {
  122. Name: "Label comments with labels from outside the repository",
  123. Counter: models.CountCommentTypeLabelWithOutsideLabels,
  124. Fixer: models.FixCommentTypeLabelWithOutsideLabels,
  125. FixedMessage: "Removed",
  126. },
  127. // find issue_label with labels from outside the repository
  128. {
  129. Name: "IssueLabels with Labels from outside the repository",
  130. Counter: models.CountIssueLabelWithOutsideLabels,
  131. Fixer: models.FixIssueLabelWithOutsideLabels,
  132. FixedMessage: "Removed",
  133. },
  134. }
  135. // TODO: function to recalc all counters
  136. if setting.Database.UsePostgreSQL {
  137. consistencyChecks = append(consistencyChecks, consistencyCheck{
  138. Name: "Sequence values",
  139. Counter: db.CountBadSequences,
  140. Fixer: asFixer(db.FixBadSequences),
  141. FixedMessage: "Updated",
  142. })
  143. }
  144. consistencyChecks = append(consistencyChecks,
  145. // find protected branches without existing repository
  146. genericOrphanCheck("Protected Branches without existing repository",
  147. "protected_branch", "repository", "protected_branch.repo_id=repository.id"),
  148. // find deleted branches without existing repository
  149. genericOrphanCheck("Deleted Branches without existing repository",
  150. "deleted_branch", "repository", "deleted_branch.repo_id=repository.id"),
  151. // find LFS locks without existing repository
  152. genericOrphanCheck("LFS locks without existing repository",
  153. "lfs_lock", "repository", "lfs_lock.repo_id=repository.id"),
  154. // find collaborations without users
  155. genericOrphanCheck("Collaborations without existing user",
  156. "collaboration", "user", "collaboration.user_id=`user`.id"),
  157. // find collaborations without repository
  158. genericOrphanCheck("Collaborations without existing repository",
  159. "collaboration", "repository", "collaboration.repo_id=repository.id"),
  160. // find access without users
  161. genericOrphanCheck("Access entries without existing user",
  162. "access", "user", "access.user_id=`user`.id"),
  163. // find access without repository
  164. genericOrphanCheck("Access entries without existing repository",
  165. "access", "repository", "access.repo_id=repository.id"),
  166. )
  167. for _, c := range consistencyChecks {
  168. if err := c.Run(logger, autofix); err != nil {
  169. return err
  170. }
  171. }
  172. return nil
  173. }
  174. func init() {
  175. Register(&Check{
  176. Title: "Check consistency of database",
  177. Name: "check-db-consistency",
  178. IsDefault: false,
  179. Run: checkDBConsistency,
  180. Priority: 3,
  181. })
  182. }