You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbconsistency.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/migrations"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. func checkDBConsistency(logger log.Logger, autofix bool) error {
  12. // make sure DB version is uptodate
  13. if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
  14. 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")
  15. return err
  16. }
  17. // find labels without existing repo or org
  18. count, err := models.CountOrphanedLabels()
  19. if err != nil {
  20. logger.Critical("Error: %v whilst counting orphaned labels")
  21. return err
  22. }
  23. if count > 0 {
  24. if autofix {
  25. if err = models.DeleteOrphanedLabels(); err != nil {
  26. logger.Critical("Error: %v whilst deleting orphaned labels")
  27. return err
  28. }
  29. logger.Info("%d labels without existing repository/organisation deleted", count)
  30. } else {
  31. logger.Warn("%d labels without existing repository/organisation", count)
  32. }
  33. }
  34. // find issues without existing repository
  35. count, err = models.CountOrphanedIssues()
  36. if err != nil {
  37. logger.Critical("Error: %v whilst counting orphaned issues")
  38. return err
  39. }
  40. if count > 0 {
  41. if autofix {
  42. if err = models.DeleteOrphanedIssues(); err != nil {
  43. logger.Critical("Error: %v whilst deleting orphaned issues")
  44. return err
  45. }
  46. logger.Info("%d issues without existing repository deleted", count)
  47. } else {
  48. logger.Warn("%d issues without existing repository", count)
  49. }
  50. }
  51. // find pulls without existing issues
  52. count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
  53. if err != nil {
  54. logger.Critical("Error: %v whilst counting orphaned objects")
  55. return err
  56. }
  57. if count > 0 {
  58. if autofix {
  59. if err = models.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id"); err != nil {
  60. logger.Critical("Error: %v whilst deleting orphaned objects")
  61. return err
  62. }
  63. logger.Info("%d pull requests without existing issue deleted", count)
  64. } else {
  65. logger.Warn("%d pull requests without existing issue", count)
  66. }
  67. }
  68. // find tracked times without existing issues/pulls
  69. count, err = models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
  70. if err != nil {
  71. logger.Critical("Error: %v whilst counting orphaned objects")
  72. return err
  73. }
  74. if count > 0 {
  75. if autofix {
  76. if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil {
  77. logger.Critical("Error: %v whilst deleting orphaned objects")
  78. return err
  79. }
  80. logger.Info("%d tracked times without existing issue deleted", count)
  81. } else {
  82. logger.Warn("%d tracked times without existing issue", count)
  83. }
  84. }
  85. // find null archived repositories
  86. count, err = models.CountNullArchivedRepository()
  87. if err != nil {
  88. logger.Critical("Error: %v whilst counting null archived repositories")
  89. return err
  90. }
  91. if count > 0 {
  92. if autofix {
  93. updatedCount, err := models.FixNullArchivedRepository()
  94. if err != nil {
  95. logger.Critical("Error: %v whilst fixing null archived repositories")
  96. return err
  97. }
  98. logger.Info("%d repositories with null is_archived updated", updatedCount)
  99. } else {
  100. logger.Warn("%d repositories with null is_archived", count)
  101. }
  102. }
  103. // TODO: function to recalc all counters
  104. return nil
  105. }
  106. func init() {
  107. Register(&Check{
  108. Title: "Check consistency of database",
  109. Name: "check-db-consistency",
  110. IsDefault: false,
  111. Run: checkDBConsistency,
  112. Priority: 3,
  113. })
  114. }