@@ -12,9 +12,7 @@ import (
1212 "code.gitea.io/gitea/models/db"
1313 "code.gitea.io/gitea/models/organization"
1414 access_model "code.gitea.io/gitea/models/perm/access"
15- project_model "code.gitea.io/gitea/models/project"
1615 repo_model "code.gitea.io/gitea/models/repo"
17- system_model "code.gitea.io/gitea/models/system"
1816 "code.gitea.io/gitea/models/unit"
1917 user_model "code.gitea.io/gitea/models/user"
2018 "code.gitea.io/gitea/modules/git"
@@ -715,138 +713,13 @@ func UpdateReactionsMigrationsByType(ctx context.Context, gitServiceType api.Git
715713 return err
716714}
717715
718- // DeleteIssuesByRepoID deletes issues by repositories id
719- func DeleteIssuesByRepoID (ctx context.Context , repoID int64 ) (attachmentPaths []string , err error ) {
720- // MariaDB has a performance bug: https://jira.mariadb.org/browse/MDEV-16289
721- // so here it uses "DELETE ... WHERE IN" with pre-queried IDs.
722- sess := db .GetEngine (ctx )
723-
724- for {
725- issueIDs := make ([]int64 , 0 , db .DefaultMaxInSize )
726-
727- err := sess .Table (& Issue {}).Where ("repo_id = ?" , repoID ).OrderBy ("id" ).Limit (db .DefaultMaxInSize ).Cols ("id" ).Find (& issueIDs )
728- if err != nil {
729- return nil , err
730- }
731-
732- if len (issueIDs ) == 0 {
733- break
734- }
735-
736- // Delete content histories
737- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& ContentHistory {})
738- if err != nil {
739- return nil , err
740- }
741-
742- // Delete comments and attachments
743- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& Comment {})
744- if err != nil {
745- return nil , err
746- }
747-
748- // Dependencies for issues in this repository
749- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& IssueDependency {})
750- if err != nil {
751- return nil , err
752- }
753-
754- // Delete dependencies for issues in other repositories
755- _ , err = sess .In ("dependency_id" , issueIDs ).Delete (& IssueDependency {})
756- if err != nil {
757- return nil , err
758- }
759-
760- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& IssueUser {})
761- if err != nil {
762- return nil , err
763- }
764-
765- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& Reaction {})
766- if err != nil {
767- return nil , err
768- }
769-
770- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& IssueWatch {})
771- if err != nil {
772- return nil , err
773- }
774-
775- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& Stopwatch {})
776- if err != nil {
777- return nil , err
778- }
779-
780- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& TrackedTime {})
781- if err != nil {
782- return nil , err
783- }
784-
785- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& project_model.ProjectIssue {})
786- if err != nil {
787- return nil , err
788- }
789-
790- _ , err = sess .In ("dependent_issue_id" , issueIDs ).Delete (& Comment {})
791- if err != nil {
792- return nil , err
793- }
794-
795- var attachments []* repo_model.Attachment
796- err = sess .In ("issue_id" , issueIDs ).Find (& attachments )
797- if err != nil {
798- return nil , err
799- }
800-
801- for j := range attachments {
802- attachmentPaths = append (attachmentPaths , attachments [j ].RelativePath ())
803- }
804-
805- _ , err = sess .In ("issue_id" , issueIDs ).Delete (& repo_model.Attachment {})
806- if err != nil {
807- return nil , err
808- }
809-
810- _ , err = sess .In ("id" , issueIDs ).Delete (& Issue {})
811- if err != nil {
812- return nil , err
813- }
814- }
815-
816- return attachmentPaths , err
817- }
818-
819- // DeleteOrphanedIssues delete issues without a repo
820- func DeleteOrphanedIssues (ctx context.Context ) error {
821- var attachmentPaths []string
822- err := db .WithTx (ctx , func (ctx context.Context ) error {
823- var ids []int64
824-
825- if err := db .GetEngine (ctx ).Table ("issue" ).Distinct ("issue.repo_id" ).
826- Join ("LEFT" , "repository" , "issue.repo_id=repository.id" ).
827- Where (builder.IsNull {"repository.id" }).GroupBy ("issue.repo_id" ).
828- Find (& ids ); err != nil {
829- return err
830- }
831-
832- for i := range ids {
833- paths , err := DeleteIssuesByRepoID (ctx , ids [i ])
834- if err != nil {
835- return err
836- }
837- attachmentPaths = append (attachmentPaths , paths ... )
838- }
839-
840- return nil
841- })
842- if err != nil {
843- return err
844- }
845-
846- // Remove issue attachment files.
847- for i := range attachmentPaths {
848- // FIXME: it's not right, because the attachment might not be on local filesystem
849- system_model .RemoveAllWithNotice (ctx , "Delete issue attachment" , attachmentPaths [i ])
716+ func GetOrphanedIssueRepoIDs (ctx context.Context ) ([]int64 , error ) {
717+ var repoIDs []int64
718+ if err := db .GetEngine (ctx ).Table ("issue" ).Distinct ("issue.repo_id" ).
719+ Join ("LEFT" , "repository" , "issue.repo_id=repository.id" ).
720+ Where (builder.IsNull {"repository.id" }).
721+ Find (& repoIDs ); err != nil {
722+ return nil , err
850723 }
851- return nil
724+ return repoIDs , nil
852725}
0 commit comments