-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Refactor DeleteByID #28450
Refactor DeleteByID #28450
Conversation
@@ -423,10 +422,10 @@ func (ar artifactRoutes) downloadArtifact(ctx *ArtifactContext) { | |||
} | |||
|
|||
artifactID := ctx.ParamsInt64("artifact_id") | |||
artifact, err := actions.GetArtifactByID(ctx, artifactID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xxx, exist, err := db.xxx(xxx)
I think the behavior should be unified. Here it returns 404 first and then returns 500.
if err != nil { | ||
ctx.Error(http.StatusNotFound, "GetPushMirrors", err) | ||
return | ||
} else if !exist { | ||
ctx.Error(http.StatusNotFound, "GetPushMirrors", nil) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But there, they both return 404. And the sequence of !exist
and err != nil
is different.
@@ -93,7 +94,7 @@ func SyncPushMirror(ctx context.Context, mirrorID int64) bool { | |||
log.Error("PANIC whilst syncPushMirror[%d] Panic: %v\nStacktrace: %s", mirrorID, err, log.Stack(2)) | |||
}() | |||
|
|||
m, err := repo_model.GetPushMirror(ctx, repo_model.PushMirrorOptions{ID: mirrorID}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And there, the exist
gets ignored.
|
||
func Delete[T any](ctx context.Context, opts FindOptions) (int64, error) { | ||
if opts == nil { | ||
return 0, ErrConditionRequired{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt this ErrConditionRequired
strange.
If I'm going to use the Delete
function and forget to add conditions, then it returns an error, I naturally think something is wrong with the database. Maybe the database is somehow disconnected or some other errors. So I probably will use if err != nil
and then return the error to the upper function or log the error to remind the user end.
It's unlikely for every developer to check the ErrConditionRequired
error. Because 1. it's cumbersome. 2. It's hard to notice because this error was only introduced last week and no one is familiar with it.
Then the error is shown on the user end. But actually, the programmer should be warned instead of the user end.
I prefer to delete this ErrConditionRequired
and let the test code find out the problem. But maybe there is a better solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
panic
.
@@ -154,7 +154,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) | |||
|
|||
// ***** START: PublicKey ***** | |||
if _, err = db.DeleteByBean(ctx, &asymkey_model.PublicKey{OwnerID: u.ID}); err != nil { | |||
return fmt.Errorf("deletePublicKeys: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? The code from line 167 to line 180 all uses DeleteByBean
and they are not changed.
@@ -73,7 +73,8 @@ func TestDeleteNotice(t *testing.T) { | |||
assert.NoError(t, unittest.PrepareTestDatabase()) | |||
|
|||
unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) | |||
assert.NoError(t, system.DeleteNotice(db.DefaultContext, 3)) | |||
_, err := db.DeleteByID[system.Notice](db.DefaultContext, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That test can now be deleted?
if err != nil { | ||
ctx.Error(http.StatusNotFound, "GetPushMirrors", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotFound?
Sounds wrong…
Is this PR still alive? |
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 <xiaolunwen@gmail.com>
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 go-gitea#28450 Closes go-gitea#28450 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
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 go-gitea#28450 Closes go-gitea#28450 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
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 go-gitea#28450 Closes go-gitea#28450 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This PR introduced a new generic method
DeleteByID[T any](ctx context.Context, id int64)
, so no more deleteByID methods need to be implemented for current models or future models.