2017-02-07 12:47:55 +01:00
// Copyright 2017 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 models
import (
"reflect"
2017-02-28 02:42:10 +01:00
"strings"
2017-02-07 12:47:55 +01:00
2021-09-19 13:49:59 +02:00
"code.gitea.io/gitea/models/db"
2021-11-12 15:36:47 +01:00
"code.gitea.io/gitea/modules/unittestbridge"
2021-11-12 01:56:45 +01:00
2020-05-29 15:24:15 +02:00
"xorm.io/builder"
2017-02-07 12:47:55 +01:00
)
// CheckConsistencyFor test that all matching database entries are consistent
2021-11-12 15:36:47 +01:00
func CheckConsistencyFor ( t unittestbridge . Tester , beansToCheck ... interface { } ) {
ta := unittestbridge . NewAsserter ( t )
2017-02-07 12:47:55 +01:00
for _ , bean := range beansToCheck {
sliceType := reflect . SliceOf ( reflect . TypeOf ( bean ) )
sliceValue := reflect . MakeSlice ( sliceType , 0 , 10 )
ptrToSliceValue := reflect . New ( sliceType )
ptrToSliceValue . Elem ( ) . Set ( sliceValue )
2021-11-12 15:36:47 +01:00
ta . NoError ( db . GetEngine ( db . DefaultContext ) . Table ( bean ) . Find ( ptrToSliceValue . Interface ( ) ) )
2017-02-07 12:47:55 +01:00
sliceValue = ptrToSliceValue . Elem ( )
for i := 0 ; i < sliceValue . Len ( ) ; i ++ {
entity := sliceValue . Index ( i ) . Interface ( )
2021-11-12 15:36:47 +01:00
checkForConsistency ( ta , entity )
2017-02-07 12:47:55 +01:00
}
}
}
2021-11-12 15:36:47 +01:00
func checkForConsistency ( ta unittestbridge . Asserter , bean interface { } ) {
2021-11-12 01:56:45 +01:00
switch b := bean . ( type ) {
case * User :
2021-11-12 15:36:47 +01:00
checkForUserConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Repository :
2021-11-12 15:36:47 +01:00
checkForRepoConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Issue :
2021-11-12 15:36:47 +01:00
checkForIssueConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * PullRequest :
2021-11-12 15:36:47 +01:00
checkForPullRequestConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Milestone :
2021-11-12 15:36:47 +01:00
checkForMilestoneConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Label :
2021-11-12 15:36:47 +01:00
checkForLabelConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Team :
2021-11-12 15:36:47 +01:00
checkForTeamConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
case * Action :
2021-11-12 15:36:47 +01:00
checkForActionConsistency ( b , ta )
2021-11-12 01:56:45 +01:00
default :
2021-11-12 15:36:47 +01:00
ta . Errorf ( "unknown bean type: %#v" , bean )
2021-11-12 01:56:45 +01:00
}
}
2017-02-07 12:47:55 +01:00
// getCount get the count of database entries matching bean
2021-11-12 15:36:47 +01:00
func getCount ( ta unittestbridge . Asserter , e db . Engine , bean interface { } ) int64 {
2017-02-07 12:47:55 +01:00
count , err := e . Count ( bean )
2021-11-12 15:36:47 +01:00
ta . NoError ( err )
2017-02-07 12:47:55 +01:00
return count
}
// assertCount test the count of database entries matching bean
2021-11-12 15:36:47 +01:00
func assertCount ( ta unittestbridge . Asserter , bean interface { } , expected int ) {
ta . EqualValues ( expected , getCount ( ta , db . GetEngine ( db . DefaultContext ) , bean ) ,
2017-02-07 12:47:55 +01:00
"Failed consistency test, the counted bean (of type %T) was %+v" , bean , bean )
}
2021-11-12 15:36:47 +01:00
func checkForUserConsistency ( user * User , ta unittestbridge . Asserter ) {
assertCount ( ta , & Repository { OwnerID : user . ID } , user . NumRepos )
assertCount ( ta , & Star { UID : user . ID } , user . NumStars )
assertCount ( ta , & OrgUser { OrgID : user . ID } , user . NumMembers )
assertCount ( ta , & Team { OrgID : user . ID } , user . NumTeams )
assertCount ( ta , & Follow { UserID : user . ID } , user . NumFollowing )
assertCount ( ta , & Follow { FollowID : user . ID } , user . NumFollowers )
2017-02-08 07:29:21 +01:00
if user . Type != UserTypeOrganization {
2021-11-12 15:36:47 +01:00
ta . EqualValues ( 0 , user . NumMembers )
ta . EqualValues ( 0 , user . NumTeams )
2017-02-08 07:29:21 +01:00
}
2017-02-07 12:47:55 +01:00
}
2021-11-12 15:36:47 +01:00
func checkForRepoConsistency ( repo * Repository , ta unittestbridge . Asserter ) {
ta . Equal ( repo . LowerName , strings . ToLower ( repo . Name ) , "repo: %+v" , repo )
assertCount ( ta , & Star { RepoID : repo . ID } , repo . NumStars )
assertCount ( ta , & Milestone { RepoID : repo . ID } , repo . NumMilestones )
assertCount ( ta , & Repository { ForkID : repo . ID } , repo . NumForks )
2017-02-07 12:47:55 +01:00
if repo . IsFork {
2021-11-12 15:36:47 +01:00
db . AssertExistsAndLoadBean ( ta , & Repository { ID : repo . ForkID } )
2017-02-07 12:47:55 +01:00
}
2021-11-12 15:36:47 +01:00
actual := getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "Mode<>?" , RepoWatchModeDont ) , & Watch { RepoID : repo . ID } )
ta . EqualValues ( repo . NumWatches , actual ,
2019-11-10 10:22:19 +01:00
"Unexpected number of watches for repo %+v" , repo )
2021-11-12 15:36:47 +01:00
actual = getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_pull=?" , false ) , & Issue { RepoID : repo . ID } )
ta . EqualValues ( repo . NumIssues , actual ,
2017-02-08 07:29:21 +01:00
"Unexpected number of issues for repo %+v" , repo )
2021-11-12 15:36:47 +01:00
actual = getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_pull=? AND is_closed=?" , false , true ) , & Issue { RepoID : repo . ID } )
ta . EqualValues ( repo . NumClosedIssues , actual ,
2017-02-07 12:47:55 +01:00
"Unexpected number of closed issues for repo %+v" , repo )
2021-11-12 15:36:47 +01:00
actual = getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_pull=?" , true ) , & Issue { RepoID : repo . ID } )
ta . EqualValues ( repo . NumPulls , actual ,
2017-02-07 12:47:55 +01:00
"Unexpected number of pulls for repo %+v" , repo )
2021-11-12 15:36:47 +01:00
actual = getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_pull=? AND is_closed=?" , true , true ) , & Issue { RepoID : repo . ID } )
ta . EqualValues ( repo . NumClosedPulls , actual ,
2017-02-07 12:47:55 +01:00
"Unexpected number of closed pulls for repo %+v" , repo )
2021-11-12 15:36:47 +01:00
actual = getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_closed=?" , true ) , & Milestone { RepoID : repo . ID } )
ta . EqualValues ( repo . NumClosedMilestones , actual ,
2017-02-07 12:47:55 +01:00
"Unexpected number of closed milestones for repo %+v" , repo )
}
2021-11-12 15:36:47 +01:00
func checkForIssueConsistency ( issue * Issue , ta unittestbridge . Asserter ) {
actual := getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "type=?" , CommentTypeComment ) , & Comment { IssueID : issue . ID } )
ta . EqualValues ( issue . NumComments , actual ,
2017-02-08 07:29:21 +01:00
"Unexpected number of comments for issue %+v" , issue )
2017-02-07 12:47:55 +01:00
if issue . IsPull {
2021-11-12 15:36:47 +01:00
pr := db . AssertExistsAndLoadBean ( ta , & PullRequest { IssueID : issue . ID } ) . ( * PullRequest )
ta . EqualValues ( pr . Index , issue . Index )
2017-02-07 12:47:55 +01:00
}
}
2021-11-12 15:36:47 +01:00
func checkForPullRequestConsistency ( pr * PullRequest , ta unittestbridge . Asserter ) {
issue := db . AssertExistsAndLoadBean ( ta , & Issue { ID : pr . IssueID } ) . ( * Issue )
ta . True ( issue . IsPull )
ta . EqualValues ( issue . Index , pr . Index )
2017-02-07 12:47:55 +01:00
}
2021-11-12 15:36:47 +01:00
func checkForMilestoneConsistency ( milestone * Milestone , ta unittestbridge . Asserter ) {
assertCount ( ta , & Issue { MilestoneID : milestone . ID } , milestone . NumIssues )
2017-02-07 12:47:55 +01:00
2021-11-12 15:36:47 +01:00
actual := getCount ( ta , db . GetEngine ( db . DefaultContext ) . Where ( "is_closed=?" , true ) , & Issue { MilestoneID : milestone . ID } )
ta . EqualValues ( milestone . NumClosedIssues , actual ,
2017-02-07 12:47:55 +01:00
"Unexpected number of closed issues for milestone %+v" , milestone )
2021-06-21 20:34:58 +02:00
completeness := 0
if milestone . NumIssues > 0 {
completeness = milestone . NumClosedIssues * 100 / milestone . NumIssues
}
2021-11-12 15:36:47 +01:00
ta . Equal ( completeness , milestone . Completeness )
2017-02-07 12:47:55 +01:00
}
2021-11-12 15:36:47 +01:00
func checkForLabelConsistency ( label * Label , ta unittestbridge . Asserter ) {
2017-02-07 12:47:55 +01:00
issueLabels := make ( [ ] * IssueLabel , 0 , 10 )
2021-11-12 15:36:47 +01:00
ta . NoError ( db . GetEngine ( db . DefaultContext ) . Find ( & issueLabels , & IssueLabel { LabelID : label . ID } ) )
ta . EqualValues ( label . NumIssues , len ( issueLabels ) ,
2017-02-07 12:47:55 +01:00
"Unexpected number of issue for label %+v" , label )
issueIDs := make ( [ ] int64 , len ( issueLabels ) )
for i , issueLabel := range issueLabels {
issueIDs [ i ] = issueLabel . IssueID
}
expected := int64 ( 0 )
if len ( issueIDs ) > 0 {
2021-11-12 15:36:47 +01:00
expected = getCount ( ta , db . GetEngine ( db . DefaultContext ) . In ( "id" , issueIDs ) . Where ( "is_closed=?" , true ) , & Issue { } )
2017-02-07 12:47:55 +01:00
}
2021-11-12 15:36:47 +01:00
ta . EqualValues ( expected , label . NumClosedIssues ,
2017-02-07 12:47:55 +01:00
"Unexpected number of closed issues for label %+v" , label )
}
2021-11-12 15:36:47 +01:00
func checkForTeamConsistency ( team * Team , ta unittestbridge . Asserter ) {
assertCount ( ta , & TeamUser { TeamID : team . ID } , team . NumMembers )
assertCount ( ta , & TeamRepo { TeamID : team . ID } , team . NumRepos )
2017-02-07 12:47:55 +01:00
}
2017-02-28 02:42:10 +01:00
2021-11-12 15:36:47 +01:00
func checkForActionConsistency ( action * Action , ta unittestbridge . Asserter ) {
repo := db . AssertExistsAndLoadBean ( ta , & Repository { ID : action . RepoID } ) . ( * Repository )
ta . Equal ( repo . IsPrivate , action . IsPrivate , "action: %+v" , action )
2017-02-28 02:42:10 +01:00
}
2020-05-29 15:24:15 +02:00
// CountOrphanedLabels return count of labels witch are broken and not accessible via ui anymore
func CountOrphanedLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
noref , err := db . GetEngine ( db . DefaultContext ) . Table ( "label" ) . Where ( "repo_id=? AND org_id=?" , 0 , 0 ) . Count ( "label.id" )
2020-05-29 15:24:15 +02:00
if err != nil {
return 0 , err
}
2021-09-23 17:45:36 +02:00
norepo , err := db . GetEngine ( db . DefaultContext ) . Table ( "label" ) .
2021-06-08 06:36:23 +02:00
Where ( builder . And (
builder . Gt { "repo_id" : 0 } ,
builder . NotIn ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) ) ,
) ) .
Count ( )
2020-05-29 15:24:15 +02:00
if err != nil {
return 0 , err
}
2021-09-23 17:45:36 +02:00
noorg , err := db . GetEngine ( db . DefaultContext ) . Table ( "label" ) .
2021-06-08 06:36:23 +02:00
Where ( builder . And (
builder . Gt { "org_id" : 0 } ,
builder . NotIn ( "org_id" , builder . Select ( "id" ) . From ( "user" ) ) ,
) ) .
Count ( )
2020-05-29 15:24:15 +02:00
if err != nil {
return 0 , err
}
return noref + norepo + noorg , nil
}
// DeleteOrphanedLabels delete labels witch are broken and not accessible via ui anymore
func DeleteOrphanedLabels ( ) error {
// delete labels with no reference
2021-09-23 17:45:36 +02:00
if _ , err := db . GetEngine ( db . DefaultContext ) . Table ( "label" ) . Where ( "repo_id=? AND org_id=?" , 0 , 0 ) . Delete ( new ( Label ) ) ; err != nil {
2020-05-29 15:24:15 +02:00
return err
}
// delete labels with none existing repos
2021-09-23 17:45:36 +02:00
if _ , err := db . GetEngine ( db . DefaultContext ) .
2021-06-08 06:36:23 +02:00
Where ( builder . And (
builder . Gt { "repo_id" : 0 } ,
builder . NotIn ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) ) ,
) ) .
2020-05-29 15:24:15 +02:00
Delete ( Label { } ) ; err != nil {
return err
}
// delete labels with none existing orgs
2021-09-23 17:45:36 +02:00
if _ , err := db . GetEngine ( db . DefaultContext ) .
2021-06-08 06:36:23 +02:00
Where ( builder . And (
builder . Gt { "org_id" : 0 } ,
builder . NotIn ( "org_id" , builder . Select ( "id" ) . From ( "user" ) ) ,
) ) .
2020-05-29 15:24:15 +02:00
Delete ( Label { } ) ; err != nil {
return err
}
return nil
}
2021-03-19 20:01:24 +01:00
// CountOrphanedIssueLabels return count of IssueLabels witch have no label behind anymore
func CountOrphanedIssueLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Table ( "issue_label" ) .
2021-06-08 06:36:23 +02:00
NotIn ( "label_id" , builder . Select ( "id" ) . From ( "label" ) ) .
Count ( )
2021-03-19 20:01:24 +01:00
}
// DeleteOrphanedIssueLabels delete IssueLabels witch have no label behind anymore
func DeleteOrphanedIssueLabels ( ) error {
2021-09-23 17:45:36 +02:00
_ , err := db . GetEngine ( db . DefaultContext ) .
2021-06-08 06:36:23 +02:00
NotIn ( "label_id" , builder . Select ( "id" ) . From ( "label" ) ) .
2021-03-19 20:01:24 +01:00
Delete ( IssueLabel { } )
return err
}
2020-05-29 15:24:15 +02:00
// CountOrphanedIssues count issues without a repo
func CountOrphanedIssues ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Table ( "issue" ) .
2020-05-29 15:24:15 +02:00
Join ( "LEFT" , "repository" , "issue.repo_id=repository.id" ) .
Where ( builder . IsNull { "repository.id" } ) .
Count ( "id" )
}
// DeleteOrphanedIssues delete issues without a repo
func DeleteOrphanedIssues ( ) error {
2021-09-19 13:49:59 +02:00
ctx , committer , err := db . TxContext ( )
if err != nil {
2020-05-29 15:24:15 +02:00
return err
}
2021-09-19 13:49:59 +02:00
defer committer . Close ( )
2020-05-29 15:24:15 +02:00
var ids [ ] int64
2021-09-23 17:45:36 +02:00
if err := db . GetEngine ( ctx ) . Table ( "issue" ) . Distinct ( "issue.repo_id" ) .
2020-05-29 15:24:15 +02:00
Join ( "LEFT" , "repository" , "issue.repo_id=repository.id" ) .
Where ( builder . IsNull { "repository.id" } ) . GroupBy ( "issue.repo_id" ) .
Find ( & ids ) ; err != nil {
return err
}
var attachmentPaths [ ] string
for i := range ids {
2021-09-23 17:45:36 +02:00
paths , err := deleteIssuesByRepoID ( db . GetEngine ( ctx ) , ids [ i ] )
2020-05-29 15:24:15 +02:00
if err != nil {
return err
}
attachmentPaths = append ( attachmentPaths , paths ... )
}
2021-09-19 13:49:59 +02:00
if err := committer . Commit ( ) ; err != nil {
2020-05-29 15:24:15 +02:00
return err
}
2021-09-19 13:49:59 +02:00
committer . Close ( )
2020-05-29 15:24:15 +02:00
// Remove issue attachment files.
for i := range attachmentPaths {
2021-09-23 17:45:36 +02:00
removeAllWithNotice ( db . GetEngine ( db . DefaultContext ) , "Delete issue attachment" , attachmentPaths [ i ] )
2020-05-29 15:24:15 +02:00
}
return nil
}
// CountOrphanedObjects count subjects with have no existing refobject anymore
func CountOrphanedObjects ( subject , refobject , joinCond string ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Table ( "`" + subject + "`" ) .
2021-10-30 11:17:40 +02:00
Join ( "LEFT" , "`" + refobject + "`" , joinCond ) .
2020-05-29 15:24:15 +02:00
Where ( builder . IsNull { "`" + refobject + "`.id" } ) .
Count ( "id" )
}
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
func DeleteOrphanedObjects ( subject , refobject , joinCond string ) error {
2021-04-30 20:08:46 +02:00
subQuery := builder . Select ( "`" + subject + "`.id" ) .
2020-05-29 15:24:15 +02:00
From ( "`" + subject + "`" ) .
Join ( "LEFT" , "`" + refobject + "`" , joinCond ) .
2021-04-30 20:08:46 +02:00
Where ( builder . IsNull { "`" + refobject + "`.id" } )
sql , args , err := builder . Delete ( builder . In ( "id" , subQuery ) ) . From ( "`" + subject + "`" ) . ToSQL ( )
if err != nil {
return err
}
2021-09-23 17:45:36 +02:00
_ , err = db . GetEngine ( db . DefaultContext ) . Exec ( append ( [ ] interface { } { sql } , args ... ) ... )
2020-05-29 15:24:15 +02:00
return err
}
2020-06-11 22:18:11 +02:00
// CountNullArchivedRepository counts the number of repositories with is_archived is null
func CountNullArchivedRepository ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . IsNull { "is_archived" } ) . Count ( new ( Repository ) )
2020-06-11 22:18:11 +02:00
}
// FixNullArchivedRepository sets is_archived to false where it is null
func FixNullArchivedRepository ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . IsNull { "is_archived" } ) . Cols ( "is_archived" ) . NoAutoTime ( ) . Update ( & Repository {
2020-06-11 22:18:11 +02:00
IsArchived : false ,
} )
}
2021-01-29 19:30:43 +01:00
// CountWrongUserType count OrgUser who have wrong type
func CountWrongUserType ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : 0 } . And ( builder . Neq { "num_teams" : 0 } ) ) . Count ( new ( User ) )
2021-01-29 19:30:43 +01:00
}
// FixWrongUserType fix OrgUser who have wrong type
func FixWrongUserType ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : 0 } . And ( builder . Neq { "num_teams" : 0 } ) ) . Cols ( "type" ) . NoAutoTime ( ) . Update ( & User { Type : 1 } )
2021-01-29 19:30:43 +01:00
}
2021-02-10 03:50:44 +01:00
// CountCommentTypeLabelWithEmptyLabel count label comments with empty label
func CountCommentTypeLabelWithEmptyLabel ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : CommentTypeLabel , "label_id" : 0 } ) . Count ( new ( Comment ) )
2021-02-10 03:50:44 +01:00
}
// FixCommentTypeLabelWithEmptyLabel count label comments with empty label
func FixCommentTypeLabelWithEmptyLabel ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : CommentTypeLabel , "label_id" : 0 } ) . Delete ( new ( Comment ) )
2021-02-10 03:50:44 +01:00
}
2021-03-18 07:06:40 +01:00
2021-03-19 14:25:14 +01:00
// CountCommentTypeLabelWithOutsideLabels count label comments with outside label
func CountCommentTypeLabelWithOutsideLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( "comment.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))" , CommentTypeLabel ) .
2021-03-19 14:25:14 +01:00
Table ( "comment" ) .
Join ( "inner" , "label" , "label.id = comment.label_id" ) .
Join ( "inner" , "issue" , "issue.id = comment.issue_id " ) .
Join ( "inner" , "repository" , "issue.repo_id = repository.id" ) .
Count ( new ( Comment ) )
}
// FixCommentTypeLabelWithOutsideLabels count label comments with outside label
func FixCommentTypeLabelWithOutsideLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
res , err := db . GetEngine ( db . DefaultContext ) . Exec ( ` DELETE FROM comment WHERE comment . id IN (
2021-03-19 14:25:14 +01:00
SELECT il_too . id FROM (
SELECT com . id
FROM comment AS com
INNER JOIN label ON com . label_id = label . id
INNER JOIN issue on issue . id = com . issue_id
2021-03-24 00:10:19 +01:00
INNER JOIN repository ON issue . repo_id = repository . id
2021-03-19 14:25:14 +01:00
WHERE
2021-03-24 00:10:19 +01:00
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 ) )
2021-03-19 14:25:14 +01:00
) AS il_too ) ` , CommentTypeLabel )
if err != nil {
return 0 , err
}
return res . RowsAffected ( )
}
// CountIssueLabelWithOutsideLabels count label comments with outside label
func CountIssueLabelWithOutsideLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Expr ( "(label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id)" ) ) .
2021-03-19 14:25:14 +01:00
Table ( "issue_label" ) .
2021-03-23 11:23:33 +01:00
Join ( "inner" , "label" , "issue_label.label_id = label.id " ) .
2021-03-19 14:25:14 +01:00
Join ( "inner" , "issue" , "issue.id = issue_label.issue_id " ) .
Join ( "inner" , "repository" , "issue.repo_id = repository.id" ) .
Count ( new ( IssueLabel ) )
}
// FixIssueLabelWithOutsideLabels fix label comments with outside label
func FixIssueLabelWithOutsideLabels ( ) ( int64 , error ) {
2021-09-23 17:45:36 +02:00
res , err := db . GetEngine ( db . DefaultContext ) . Exec ( ` DELETE FROM issue_label WHERE issue_label . id IN (
2021-03-19 14:25:14 +01:00
SELECT il_too . id FROM (
SELECT il_too_too . id
FROM issue_label AS il_too_too
2021-03-22 19:26:38 +01:00
INNER JOIN label ON il_too_too . label_id = label . id
2021-03-19 14:25:14 +01:00
INNER JOIN issue on issue . id = il_too_too . issue_id
INNER JOIN repository on repository . id = issue . repo_id
WHERE
2021-03-24 00:10:19 +01:00
( label . org_id = 0 AND issue . repo_id != label . repo_id ) OR ( label . repo_id = 0 AND label . org_id != repository . owner_id )
2021-03-19 14:25:14 +01:00
) AS il_too ) ` )
if err != nil {
return 0 , err
}
return res . RowsAffected ( )
}