Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DeleteByID #28450

Closed
wants to merge 4 commits into from
Closed

Conversation

lunny
Copy link
Member

@lunny lunny commented Dec 13, 2023

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.

@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Dec 13, 2023
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 13, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 13, 2023
@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Dec 13, 2023
@lunny lunny changed the title WIP: Refactor DeleteByID Refactor DeleteByID Dec 14, 2023
@@ -423,10 +422,10 @@ func (ar artifactRoutes) downloadArtifact(ctx *ArtifactContext) {
}

artifactID := ctx.ParamsInt64("artifact_id")
artifact, err := actions.GetArtifactByID(ctx, artifactID)
Copy link
Member

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
}
Copy link
Member

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})
Copy link
Member

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{}
Copy link
Member

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.

Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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?

Comment on lines 234 to 235
if err != nil {
ctx.Error(http.StatusNotFound, "GetPushMirrors", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotFound?
Sounds wrong…

@delvh
Copy link
Member

delvh commented Dec 22, 2023

Is this PR still alive?

@delvh delvh mentioned this pull request Dec 25, 2023
@lunny lunny closed this Dec 25, 2023
@lunny lunny deleted the lunny/refactor_deletebyid branch December 25, 2023 12:09
delvh added a commit that referenced this pull request Dec 25, 2023
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>
katsusan pushed a commit to katsusan/gitea that referenced this pull request Dec 26, 2023
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>
fuxiaohei pushed a commit to fuxiaohei/gitea that referenced this pull request Jan 17, 2024
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>
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
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>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants