Don't use subselect in DeleteIssuesByRepoID (#27332) (#27408)

Backport #27332 by @JakobDev

Part of https://codeberg.org/forgejo/discussions/issues/61

This is workaround for a bug in MariaDB

Co-authored-by: JakobDev <jakobdev@gmx.de>
This commit is contained in:
Giteabot 2023-10-03 17:59:45 +08:00 committed by GitHub
parent 8c6464e39b
commit d5da0e622c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -696,71 +696,84 @@ func UpdateReactionsMigrationsByType(ctx context.Context, gitServiceType api.Git
// DeleteIssuesByRepoID deletes issues by repositories id // DeleteIssuesByRepoID deletes issues by repositories id
func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths []string, err error) { func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths []string, err error) {
deleteCond := builder.Select("id").From("issue").Where(builder.Eq{"issue.repo_id": repoID}) // MariaDB has a performance bug: https://jira.mariadb.org/browse/MDEV-16289
// so here it uses "DELETE ... WHERE IN" with pre-queried IDs.
sess := db.GetEngine(ctx) sess := db.GetEngine(ctx)
for {
issueIDs := make([]int64, 0, db.DefaultMaxInSize)
err := sess.Table(&Issue{}).Where("repo_id = ?", repoID).OrderBy("id").Limit(db.DefaultMaxInSize).Cols("id").Find(&issueIDs)
if err != nil {
return nil, err
}
if len(issueIDs) == 0 {
break
}
// Delete content histories // Delete content histories
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&ContentHistory{})
Delete(&ContentHistory{}); err != nil { if err != nil {
return nil, err return nil, err
} }
// Delete comments and attachments // Delete comments and attachments
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&Comment{})
Delete(&Comment{}); err != nil { if err != nil {
return nil, err return nil, err
} }
// Dependencies for issues in this repository // Dependencies for issues in this repository
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&IssueDependency{})
Delete(&IssueDependency{}); err != nil { if err != nil {
return nil, err return nil, err
} }
// Delete dependencies for issues in other repositories // Delete dependencies for issues in other repositories
if _, err = sess.In("dependency_id", deleteCond). _, err = sess.In("dependency_id", issueIDs).Delete(&IssueDependency{})
Delete(&IssueDependency{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{})
Delete(&IssueUser{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&Reaction{})
Delete(&Reaction{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&IssueWatch{})
Delete(&IssueWatch{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&Stopwatch{})
Delete(&Stopwatch{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&TrackedTime{})
Delete(&TrackedTime{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&project_model.ProjectIssue{})
Delete(&project_model.ProjectIssue{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = sess.In("dependent_issue_id", deleteCond). _, err = sess.In("dependent_issue_id", issueIDs).Delete(&Comment{})
Delete(&Comment{}); err != nil { if err != nil {
return nil, err return nil, err
} }
var attachments []*repo_model.Attachment var attachments []*repo_model.Attachment
if err = sess.In("issue_id", deleteCond). err = sess.In("issue_id", issueIDs).Find(&attachments)
Find(&attachments); err != nil { if err != nil {
return nil, err return nil, err
} }
@ -768,14 +781,16 @@ func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths []
attachmentPaths = append(attachmentPaths, attachments[j].RelativePath()) attachmentPaths = append(attachmentPaths, attachments[j].RelativePath())
} }
if _, err = sess.In("issue_id", deleteCond). _, err = sess.In("issue_id", issueIDs).Delete(&repo_model.Attachment{})
Delete(&repo_model.Attachment{}); err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = db.DeleteByBean(ctx, &Issue{RepoID: repoID}); err != nil { _, err = sess.In("id", issueIDs).Delete(&Issue{})
if err != nil {
return nil, err return nil, err
} }
}
return attachmentPaths, err return attachmentPaths, err
} }