Skip to content

Commit 27e3071

Browse files
authored
Fix db.Find bug (#23115) (#23119)
Backport #23115 Caused by #20821 Fix #23110
1 parent e02e752 commit 27e3071

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

models/db/list_options.go renamed to models/db/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func Find[T any](ctx context.Context, opts FindOptions, objects *[]T) error {
134134
if !opts.IsListAll() {
135135
sess.Limit(opts.GetSkipTake())
136136
}
137-
return sess.Find(&objects)
137+
return sess.Find(objects)
138138
}
139139

140140
// Count represents a common count function which accept an options interface
@@ -148,5 +148,5 @@ func FindAndCount[T any](ctx context.Context, opts FindOptions, objects *[]T) (i
148148
if !opts.IsListAll() {
149149
sess.Limit(opts.GetSkipTake())
150150
}
151-
return sess.FindAndCount(&objects)
151+
return sess.FindAndCount(objects)
152152
}

models/db/list_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package db_test
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
repo_model "code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/models/unittest"
12+
13+
"github.com/stretchr/testify/assert"
14+
"xorm.io/builder"
15+
)
16+
17+
type mockListOptions struct {
18+
db.ListOptions
19+
}
20+
21+
func (opts *mockListOptions) IsListAll() bool {
22+
return true
23+
}
24+
25+
func (opts *mockListOptions) ToConds() builder.Cond {
26+
return builder.NewCond()
27+
}
28+
29+
func TestFind(t *testing.T) {
30+
assert.NoError(t, unittest.PrepareTestDatabase())
31+
xe := unittest.GetXORMEngine()
32+
assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
33+
34+
opts := mockListOptions{}
35+
var repoUnits []repo_model.RepoUnit
36+
err := db.Find(db.DefaultContext, &opts, &repoUnits)
37+
assert.NoError(t, err)
38+
assert.EqualValues(t, 83, len(repoUnits))
39+
40+
cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
41+
assert.NoError(t, err)
42+
assert.EqualValues(t, 83, cnt)
43+
44+
repoUnits = make([]repo_model.RepoUnit, 0, 10)
45+
newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)
46+
assert.NoError(t, err)
47+
assert.EqualValues(t, cnt, newCnt)
48+
}

0 commit comments

Comments
 (0)