Skip to content

Commit 778ad79

Browse files
delvhlunny
andauthored
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 <[email protected]>
1 parent b41925c commit 778ad79

File tree

31 files changed

+89
-169
lines changed

31 files changed

+89
-169
lines changed

models/actions/artifact.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ func getArtifactByNameAndPath(ctx context.Context, runID int64, name, fpath stri
9090
return &art, nil
9191
}
9292

93-
// GetArtifactByID returns an artifact by id
94-
func GetArtifactByID(ctx context.Context, id int64) (*ActionArtifact, error) {
95-
var art ActionArtifact
96-
has, err := db.GetEngine(ctx).ID(id).Get(&art)
97-
if err != nil {
98-
return nil, err
99-
} else if !has {
100-
return nil, util.ErrNotExist
101-
}
102-
103-
return &art, nil
104-
}
105-
10693
// UpdateArtifactByID updates an artifact by id
10794
func UpdateArtifactByID(ctx context.Context, id int64, art *ActionArtifact) error {
10895
art.ID = id

models/actions/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func DeleteRunner(ctx context.Context, id int64) error {
254254
return err
255255
}
256256

257-
_, err := db.GetEngine(ctx).Delete(&ActionRunner{ID: id})
257+
_, err := db.DeleteByID[ActionRunner](ctx, id)
258258
return err
259259
}
260260

models/asymkey/ssh_key.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,6 @@ func UpdatePublicKeyUpdated(ctx context.Context, id int64) error {
227227
return nil
228228
}
229229

230-
// DeletePublicKeys does the actual key deletion but does not update authorized_keys file.
231-
func DeletePublicKeys(ctx context.Context, keyIDs ...int64) error {
232-
if len(keyIDs) == 0 {
233-
return nil
234-
}
235-
236-
_, err := db.GetEngine(ctx).In("id", keyIDs).Delete(new(PublicKey))
237-
return err
238-
}
239-
240230
// PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key
241231
func PublicKeysAreExternallyManaged(ctx context.Context, keys []*PublicKey) ([]bool, error) {
242232
sources := make([]*auth.Source, 0, 5)
@@ -322,8 +312,8 @@ func deleteKeysMarkedForDeletion(ctx context.Context, keys []string) (bool, erro
322312
log.Error("SearchPublicKeyByContent: %v", err)
323313
continue
324314
}
325-
if err = DeletePublicKeys(ctx, key.ID); err != nil {
326-
log.Error("deletePublicKeys: %v", err)
315+
if _, err = db.DeleteByID[PublicKey](ctx, key.ID); err != nil {
316+
log.Error("DeleteByID[PublicKey]: %v", err)
327317
continue
328318
}
329319
sshKeysNeedUpdate = true

models/db/context.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error) {
175175

176176
func Get[T any](ctx context.Context, cond builder.Cond) (object *T, exist bool, err error) {
177177
if !cond.IsValid() {
178-
return nil, false, ErrConditionRequired{}
178+
panic("cond is invalid in db.Get(ctx, cond). This should not be possible.")
179179
}
180180

181181
var bean T
@@ -201,7 +201,7 @@ func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err e
201201

202202
func Exist[T any](ctx context.Context, cond builder.Cond) (bool, error) {
203203
if !cond.IsValid() {
204-
return false, ErrConditionRequired{}
204+
panic("cond is invalid in db.Exist(ctx, cond). This should not be possible.")
205205
}
206206

207207
var bean T
@@ -213,16 +213,36 @@ func ExistByID[T any](ctx context.Context, id int64) (bool, error) {
213213
return GetEngine(ctx).ID(id).NoAutoCondition().Exist(&bean)
214214
}
215215

216+
// DeleteByID deletes the given bean with the given ID
217+
func DeleteByID[T any](ctx context.Context, id int64) (int64, error) {
218+
var bean T
219+
return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(&bean)
220+
}
221+
222+
func DeleteByIDs[T any](ctx context.Context, ids ...int64) error {
223+
if len(ids) == 0 {
224+
return nil
225+
}
226+
227+
var bean T
228+
_, err := GetEngine(ctx).In("id", ids).NoAutoCondition().NoAutoTime().Delete(&bean)
229+
return err
230+
}
231+
232+
func Delete[T any](ctx context.Context, opts FindOptions) (int64, error) {
233+
if opts == nil || !opts.ToConds().IsValid() {
234+
panic("opts are empty or invalid in db.Delete(ctx, opts). This should not be possible.")
235+
}
236+
237+
var bean T
238+
return GetEngine(ctx).Where(opts.ToConds()).NoAutoCondition().NoAutoTime().Delete(&bean)
239+
}
240+
216241
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
217242
func DeleteByBean(ctx context.Context, bean any) (int64, error) {
218243
return GetEngine(ctx).Delete(bean)
219244
}
220245

221-
// DeleteByID deletes the given bean with the given ID
222-
func DeleteByID(ctx context.Context, id int64, bean any) (int64, error) {
223-
return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(bean)
224-
}
225-
226246
// FindIDs finds the IDs for the given table name satisfying the given condition
227247
// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition
228248
func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error) {

models/db/error.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,3 @@ func (err ErrNotExist) Error() string {
7272
func (err ErrNotExist) Unwrap() error {
7373
return util.ErrNotExist
7474
}
75-
76-
// ErrConditionRequired represents an error which require condition.
77-
type ErrConditionRequired struct{}
78-
79-
// IsErrConditionRequired checks if an error is an ErrConditionRequired
80-
func IsErrConditionRequired(err error) bool {
81-
_, ok := err.(ErrConditionRequired)
82-
return ok
83-
}
84-
85-
func (err ErrConditionRequired) Error() string {
86-
return "condition is required"
87-
}
88-
89-
// Unwrap unwraps this as a ErrNotExist err
90-
func (err ErrConditionRequired) Unwrap() error {
91-
return util.ErrInvalidArgument
92-
}

models/issues/issue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ func TestIssue_InsertIssue(t *testing.T) {
249249

250250
// there are 5 issues and max index is 5 on repository 1, so this one should 6
251251
issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6)
252-
_, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
252+
_, err := db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID)
253253
assert.NoError(t, err)
254254

255255
issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7)
256-
_, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
256+
_, err = db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID)
257257
assert.NoError(t, err)
258258
}
259259

models/issues/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func DeleteLabel(ctx context.Context, id, labelID int64) error {
256256
return nil
257257
}
258258

259-
if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
259+
if _, err = db.DeleteByID[Label](ctx, labelID); err != nil {
260260
return err
261261
} else if _, err = sess.
262262
Where("label_id = ?", labelID).

models/issues/milestone.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error {
289289
}
290290
defer committer.Close()
291291

292-
sess := db.GetEngine(ctx)
293-
294-
if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
292+
if _, err = db.DeleteByID[Milestone](ctx, m.ID); err != nil {
295293
return err
296294
}
297295

@@ -311,7 +309,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error {
311309
repo.NumMilestones = int(numMilestones)
312310
repo.NumClosedMilestones = int(numClosedMilestones)
313311

314-
if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
312+
if _, err = db.GetEngine(ctx).ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
315313
return err
316314
}
317315

models/issues/review.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, issue *Issue, revi
446446
continue
447447
}
448448

449-
if _, err := sess.ID(teamReviewRequest.ID).NoAutoCondition().Delete(teamReviewRequest); err != nil {
449+
if _, err := db.DeleteByID[Review](ctx, teamReviewRequest.ID); err != nil {
450450
return nil, nil, err
451451
}
452452
}
@@ -869,7 +869,6 @@ func DeleteReview(ctx context.Context, r *Review) error {
869869
return err
870870
}
871871
defer committer.Close()
872-
sess := db.GetEngine(ctx)
873872

874873
if r.ID == 0 {
875874
return fmt.Errorf("review is not allowed to be 0")
@@ -885,7 +884,7 @@ func DeleteReview(ctx context.Context, r *Review) error {
885884
ReviewID: r.ID,
886885
}
887886

888-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
887+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
889888
return err
890889
}
891890

@@ -895,7 +894,7 @@ func DeleteReview(ctx context.Context, r *Review) error {
895894
ReviewID: r.ID,
896895
}
897896

898-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
897+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
899898
return err
900899
}
901900

@@ -905,11 +904,11 @@ func DeleteReview(ctx context.Context, r *Review) error {
905904
ReviewID: r.ID,
906905
}
907906

908-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
907+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
909908
return err
910909
}
911910

912-
if _, err := sess.ID(r.ID).Delete(new(Review)); err != nil {
911+
if _, err := db.DeleteByID[Review](ctx, r.ID); err != nil {
913912
return err
914913
}
915914

models/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func RemoveOrgUser(ctx context.Context, orgID, userID int64) error {
5757
}
5858
defer committer.Close()
5959

60-
if _, err := db.GetEngine(ctx).ID(ou.ID).Delete(ou); err != nil {
60+
if _, err := db.DeleteByID[organization.OrgUser](ctx, ou.ID); err != nil {
6161
return err
6262
} else if _, err = db.Exec(ctx, "UPDATE `user` SET num_members=num_members-1 WHERE id=?", orgID); err != nil {
6363
return err

0 commit comments

Comments
 (0)