2020-12-02 10:26:04 +05:30
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package doctor
import (
"context"
"code.gitea.io/gitea/models"
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2020-12-02 10:26:04 +05:30
"code.gitea.io/gitea/models/migrations"
"code.gitea.io/gitea/modules/log"
2021-03-18 11:36:40 +05:30
"code.gitea.io/gitea/modules/setting"
2020-12-02 10:26:04 +05:30
)
2021-09-27 23:37:19 +05:30
type consistencyCheck struct {
Name string
Counter func ( ) ( int64 , error )
Fixer func ( ) ( int64 , error )
FixedMessage string
}
2020-12-02 10:26:04 +05:30
2021-09-27 23:37:19 +05:30
func ( c * consistencyCheck ) Run ( logger log . Logger , autofix bool ) error {
count , err := c . Counter ( )
2021-09-15 01:11:40 +05:30
if err != nil {
2021-09-27 23:37:19 +05:30
logger . Critical ( "Error: %v whilst counting %s" , err , c . Name )
2021-09-15 01:11:40 +05:30
return err
}
if count > 0 {
if autofix {
2021-09-27 23:37:19 +05:30
var fixed int64
if fixed , err = c . Fixer ( ) ; err != nil {
logger . Critical ( "Error: %v whilst fixing %s" , err , c . Name )
2021-09-15 01:11:40 +05:30
return err
}
2021-09-27 23:37:19 +05:30
prompt := "Deleted"
if c . FixedMessage != "" {
prompt = c . FixedMessage
2020-12-02 10:26:04 +05:30
}
2021-09-27 23:37:19 +05:30
if fixed < 0 {
logger . Info ( prompt + " %d %s" , count , c . Name )
} else {
logger . Info ( prompt + " %d/%d %s" , fixed , count , c . Name )
2021-09-15 01:11:40 +05:30
}
} else {
2021-09-27 23:37:19 +05:30
logger . Warn ( "Found %d %s" , count , c . Name )
2021-09-15 01:11:40 +05:30
}
}
2021-09-27 23:37:19 +05:30
return nil
}
2021-09-15 01:11:40 +05:30
2021-09-27 23:37:19 +05:30
func asFixer ( fn func ( ) error ) func ( ) ( int64 , error ) {
return func ( ) ( int64 , error ) {
err := fn ( )
return - 1 , err
2020-12-02 10:26:04 +05:30
}
2021-09-27 23:37:19 +05:30
}
2020-12-02 10:26:04 +05:30
2021-09-27 23:37:19 +05:30
func genericOrphanCheck ( name , subject , refobject , joincond string ) consistencyCheck {
return consistencyCheck {
Name : name ,
Counter : func ( ) ( int64 , error ) {
return models . CountOrphanedObjects ( subject , refobject , joincond )
} ,
Fixer : func ( ) ( int64 , error ) {
err := models . DeleteOrphanedObjects ( subject , refobject , joincond )
return - 1 , err
} ,
2021-02-10 08:20:44 +05:30
}
2021-09-27 23:37:19 +05:30
}
2021-03-19 18:55:14 +05:30
2021-09-27 23:37:19 +05:30
func checkDBConsistency ( logger log . Logger , autofix bool ) error {
// make sure DB version is uptodate
2021-10-30 20:02:11 +05:30
if err := db . InitEngineWithMigration ( context . Background ( ) , migrations . EnsureUpToDate ) ; err != nil {
2021-09-27 23:37:19 +05:30
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" )
2021-03-19 18:55:14 +05:30
return err
}
2021-09-27 23:37:19 +05:30
consistencyChecks := [ ] consistencyCheck {
{
// find labels without existing repo or org
Name : "Orphaned Labels without existing repository or organisation" ,
Counter : models . CountOrphanedLabels ,
Fixer : asFixer ( models . DeleteOrphanedLabels ) ,
} ,
{
// find IssueLabels without existing label
Name : "Orphaned Issue Labels without existing label" ,
Counter : models . CountOrphanedIssueLabels ,
Fixer : asFixer ( models . DeleteOrphanedIssueLabels ) ,
} ,
{
// find issues without existing repository
Name : "Orphaned Issues without existing repository" ,
Counter : models . CountOrphanedIssues ,
Fixer : asFixer ( models . DeleteOrphanedIssues ) ,
} ,
// find releases without existing repository
genericOrphanCheck ( "Orphaned Releases without existing repository" ,
"release" , "repository" , "release.repo_id=repository.id" ) ,
// find pulls without existing issues
genericOrphanCheck ( "Orphaned PullRequests without existing issue" ,
"pull_request" , "issue" , "pull_request.issue_id=issue.id" ) ,
// find tracked times without existing issues/pulls
genericOrphanCheck ( "Orphaned TrackedTimes without existing issue" ,
"tracked_time" , "issue" , "tracked_time.issue_id=issue.id" ) ,
// find attachments without existing issues or releases
{
Name : "Orphaned Attachments without existing issues or releases" ,
Counter : models . CountOrphanedAttachments ,
Fixer : asFixer ( models . DeleteOrphanedAttachments ) ,
} ,
// find null archived repositories
{
Name : "Repositories with is_archived IS NULL" ,
Counter : models . CountNullArchivedRepository ,
Fixer : models . FixNullArchivedRepository ,
FixedMessage : "Fixed" ,
} ,
// find label comments with empty labels
{
Name : "Label comments with empty labels" ,
Counter : models . CountCommentTypeLabelWithEmptyLabel ,
Fixer : models . FixCommentTypeLabelWithEmptyLabel ,
FixedMessage : "Fixed" ,
} ,
// find label comments with labels from outside the repository
{
Name : "Label comments with labels from outside the repository" ,
Counter : models . CountCommentTypeLabelWithOutsideLabels ,
Fixer : models . FixCommentTypeLabelWithOutsideLabels ,
FixedMessage : "Removed" ,
} ,
// find issue_label with labels from outside the repository
{
Name : "IssueLabels with Labels from outside the repository" ,
Counter : models . CountIssueLabelWithOutsideLabels ,
Fixer : models . FixIssueLabelWithOutsideLabels ,
FixedMessage : "Removed" ,
} ,
2021-03-19 18:55:14 +05:30
}
2020-12-02 10:26:04 +05:30
// TODO: function to recalc all counters
2021-03-18 11:36:40 +05:30
if setting . Database . UsePostgreSQL {
2021-09-27 23:37:19 +05:30
consistencyChecks = append ( consistencyChecks , consistencyCheck {
Name : "Sequence values" ,
Counter : db . CountBadSequences ,
Fixer : asFixer ( db . FixBadSequences ) ,
FixedMessage : "Updated" ,
} )
}
consistencyChecks = append ( consistencyChecks ,
// find protected branches without existing repository
genericOrphanCheck ( "Protected Branches without existing repository" ,
"protected_branch" , "repository" , "protected_branch.repo_id=repository.id" ) ,
// find deleted branches without existing repository
genericOrphanCheck ( "Deleted Branches without existing repository" ,
"deleted_branch" , "repository" , "deleted_branch.repo_id=repository.id" ) ,
// find LFS locks without existing repository
genericOrphanCheck ( "LFS locks without existing repository" ,
"lfs_lock" , "repository" , "lfs_lock.repo_id=repository.id" ) ,
// find collaborations without users
genericOrphanCheck ( "Collaborations without existing user" ,
"collaboration" , "user" , "collaboration.user_id=user.id" ) ,
// find collaborations without repository
genericOrphanCheck ( "Collaborations without existing repository" ,
"collaboration" , "repository" , "collaboration.repo_id=repository.id" ) ,
// find access without users
genericOrphanCheck ( "Access entries without existing user" ,
"access" , "user" , "access.user_id=user.id" ) ,
// find access without repository
genericOrphanCheck ( "Access entries without existing repository" ,
"access" , "repository" , "access.repo_id=repository.id" ) ,
)
for _ , c := range consistencyChecks {
if err := c . Run ( logger , autofix ) ; err != nil {
2021-05-01 00:40:39 +05:30
return err
2021-03-18 11:36:40 +05:30
}
2021-05-01 00:40:39 +05:30
}
2020-12-02 10:26:04 +05:30
return nil
}
func init ( ) {
Register ( & Check {
Title : "Check consistency of database" ,
Name : "check-db-consistency" ,
IsDefault : false ,
Run : checkDBConsistency ,
Priority : 3 ,
} )
}