From 778ad795fd4a19dc15723b59a846a250034c7c3a Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 25 Dec 2023 21:25:29 +0100 Subject: Refactor deletion (#28610) Introduce the new generic deletion methods - `func DeleteByID[T any](ctx context.Context, id int64) (int64, error)` - `func DeleteByIDs[T any](ctx context.Context, ids ...int64) error` - `func Delete[T any](ctx context.Context, opts FindOptions) (int64, error)` So, we no longer need any specific deletion method and can just use the generic ones instead. Replacement of #28450 Closes #28450 --------- Co-authored-by: Lunny Xiao --- models/issues/issue_test.go | 4 ++-- models/issues/label.go | 2 +- models/issues/milestone.go | 6 ++---- models/issues/review.go | 11 +++++------ 4 files changed, 10 insertions(+), 13 deletions(-) (limited to 'models/issues') diff --git a/models/issues/issue_test.go b/models/issues/issue_test.go index 723fa27b1b..cc363d2fae 100644 --- a/models/issues/issue_test.go +++ b/models/issues/issue_test.go @@ -249,11 +249,11 @@ func TestIssue_InsertIssue(t *testing.T) { // there are 5 issues and max index is 5 on repository 1, so this one should 6 issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6) - _, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue)) + _, err := db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID) assert.NoError(t, err) issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7) - _, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue)) + _, err = db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID) assert.NoError(t, err) } diff --git a/models/issues/label.go b/models/issues/label.go index 3b811c1529..527d8d7853 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -256,7 +256,7 @@ func DeleteLabel(ctx context.Context, id, labelID int64) error { return nil } - if _, err = sess.ID(labelID).Delete(new(Label)); err != nil { + if _, err = db.DeleteByID[Label](ctx, labelID); err != nil { return err } else if _, err = sess. Where("label_id = ?", labelID). diff --git a/models/issues/milestone.go b/models/issues/milestone.go index 9db7cb2b15..eb42df8263 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -289,9 +289,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error { } defer committer.Close() - sess := db.GetEngine(ctx) - - if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil { + if _, err = db.DeleteByID[Milestone](ctx, m.ID); err != nil { return err } @@ -311,7 +309,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error { repo.NumMilestones = int(numMilestones) repo.NumClosedMilestones = int(numClosedMilestones) - if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil { + if _, err = db.GetEngine(ctx).ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil { return err } diff --git a/models/issues/review.go b/models/issues/review.go index e2f65e369f..e06670c9a1 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -446,7 +446,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, issue *Issue, revi continue } - if _, err := sess.ID(teamReviewRequest.ID).NoAutoCondition().Delete(teamReviewRequest); err != nil { + if _, err := db.DeleteByID[Review](ctx, teamReviewRequest.ID); err != nil { return nil, nil, err } } @@ -869,7 +869,6 @@ func DeleteReview(ctx context.Context, r *Review) error { return err } defer committer.Close() - sess := db.GetEngine(ctx) if r.ID == 0 { return fmt.Errorf("review is not allowed to be 0") @@ -885,7 +884,7 @@ func DeleteReview(ctx context.Context, r *Review) error { ReviewID: r.ID, } - if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil { + if _, err := db.Delete[Comment](ctx, opts); err != nil { return err } @@ -895,7 +894,7 @@ func DeleteReview(ctx context.Context, r *Review) error { ReviewID: r.ID, } - if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil { + if _, err := db.Delete[Comment](ctx, opts); err != nil { return err } @@ -905,11 +904,11 @@ func DeleteReview(ctx context.Context, r *Review) error { ReviewID: r.ID, } - if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil { + if _, err := db.Delete[Comment](ctx, opts); err != nil { return err } - if _, err := sess.ID(r.ID).Delete(new(Review)); err != nil { + if _, err := db.DeleteByID[Review](ctx, r.ID); err != nil { return err } -- cgit v1.2.3