Skip to content

Commit f91dbbb

Browse files
authored
Next round of db.DefaultContext refactor (#27089)
Part of #27065
1 parent a1b2a11 commit f91dbbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+434
-464
lines changed

models/actions/schedule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func init() {
4141
}
4242

4343
// GetSchedulesMapByIDs returns the schedules by given id slice.
44-
func GetSchedulesMapByIDs(ids []int64) (map[int64]*ActionSchedule, error) {
44+
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
4545
schedules := make(map[int64]*ActionSchedule, len(ids))
46-
return schedules, db.GetEngine(db.DefaultContext).In("id", ids).Find(&schedules)
46+
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
4747
}
4848

4949
// GetReposMapByIDs returns the repos by given id slice.
50-
func GetReposMapByIDs(ids []int64) (map[int64]*repo_model.Repository, error) {
50+
func GetReposMapByIDs(ctx context.Context, ids []int64) (map[int64]*repo_model.Repository, error) {
5151
repos := make(map[int64]*repo_model.Repository, len(ids))
52-
return repos, db.GetEngine(db.DefaultContext).In("id", ids).Find(&repos)
52+
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
5353
}
5454

5555
var cronParser = cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)

models/actions/schedule_spec_list.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func (specs SpecList) GetScheduleIDs() []int64 {
2323
return ids.Values()
2424
}
2525

26-
func (specs SpecList) LoadSchedules() error {
26+
func (specs SpecList) LoadSchedules(ctx context.Context) error {
2727
scheduleIDs := specs.GetScheduleIDs()
28-
schedules, err := GetSchedulesMapByIDs(scheduleIDs)
28+
schedules, err := GetSchedulesMapByIDs(ctx, scheduleIDs)
2929
if err != nil {
3030
return err
3131
}
@@ -34,7 +34,7 @@ func (specs SpecList) LoadSchedules() error {
3434
}
3535

3636
repoIDs := specs.GetRepoIDs()
37-
repos, err := GetReposMapByIDs(repoIDs)
37+
repos, err := GetReposMapByIDs(ctx, repoIDs)
3838
if err != nil {
3939
return err
4040
}
@@ -95,7 +95,7 @@ func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, erro
9595
return nil, 0, err
9696
}
9797

98-
if err := specs.LoadSchedules(); err != nil {
98+
if err := specs.LoadSchedules(ctx); err != nil {
9999
return nil, 0, err
100100
}
101101
return specs, total, nil

models/admin/task.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ type TranslatableMessage struct {
4848
}
4949

5050
// LoadRepo loads repository of the task
51-
func (task *Task) LoadRepo() error {
52-
return task.loadRepo(db.DefaultContext)
53-
}
54-
55-
func (task *Task) loadRepo(ctx context.Context) error {
51+
func (task *Task) LoadRepo(ctx context.Context) error {
5652
if task.Repo != nil {
5753
return nil
5854
}
@@ -70,13 +66,13 @@ func (task *Task) loadRepo(ctx context.Context) error {
7066
}
7167

7268
// LoadDoer loads do user
73-
func (task *Task) LoadDoer() error {
69+
func (task *Task) LoadDoer(ctx context.Context) error {
7470
if task.Doer != nil {
7571
return nil
7672
}
7773

7874
var doer user_model.User
79-
has, err := db.GetEngine(db.DefaultContext).ID(task.DoerID).Get(&doer)
75+
has, err := db.GetEngine(ctx).ID(task.DoerID).Get(&doer)
8076
if err != nil {
8177
return err
8278
} else if !has {
@@ -90,13 +86,13 @@ func (task *Task) LoadDoer() error {
9086
}
9187

9288
// LoadOwner loads owner user
93-
func (task *Task) LoadOwner() error {
89+
func (task *Task) LoadOwner(ctx context.Context) error {
9490
if task.Owner != nil {
9591
return nil
9692
}
9793

9894
var owner user_model.User
99-
has, err := db.GetEngine(db.DefaultContext).ID(task.OwnerID).Get(&owner)
95+
has, err := db.GetEngine(ctx).ID(task.OwnerID).Get(&owner)
10096
if err != nil {
10197
return err
10298
} else if !has {
@@ -110,8 +106,8 @@ func (task *Task) LoadOwner() error {
110106
}
111107

112108
// UpdateCols updates some columns
113-
func (task *Task) UpdateCols(cols ...string) error {
114-
_, err := db.GetEngine(db.DefaultContext).ID(task.ID).Cols(cols...).Update(task)
109+
func (task *Task) UpdateCols(ctx context.Context, cols ...string) error {
110+
_, err := db.GetEngine(ctx).ID(task.ID).Cols(cols...).Update(task)
115111
return err
116112
}
117113

@@ -169,12 +165,12 @@ func (err ErrTaskDoesNotExist) Unwrap() error {
169165
}
170166

171167
// GetMigratingTask returns the migrating task by repo's id
172-
func GetMigratingTask(repoID int64) (*Task, error) {
168+
func GetMigratingTask(ctx context.Context, repoID int64) (*Task, error) {
173169
task := Task{
174170
RepoID: repoID,
175171
Type: structs.TaskTypeMigrateRepo,
176172
}
177-
has, err := db.GetEngine(db.DefaultContext).Get(&task)
173+
has, err := db.GetEngine(ctx).Get(&task)
178174
if err != nil {
179175
return nil, err
180176
} else if !has {
@@ -184,13 +180,13 @@ func GetMigratingTask(repoID int64) (*Task, error) {
184180
}
185181

186182
// GetMigratingTaskByID returns the migrating task by repo's id
187-
func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, error) {
183+
func GetMigratingTaskByID(ctx context.Context, id, doerID int64) (*Task, *migration.MigrateOptions, error) {
188184
task := Task{
189185
ID: id,
190186
DoerID: doerID,
191187
Type: structs.TaskTypeMigrateRepo,
192188
}
193-
has, err := db.GetEngine(db.DefaultContext).Get(&task)
189+
has, err := db.GetEngine(ctx).Get(&task)
194190
if err != nil {
195191
return nil, nil, err
196192
} else if !has {
@@ -205,12 +201,12 @@ func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, e
205201
}
206202

207203
// CreateTask creates a task on database
208-
func CreateTask(task *Task) error {
209-
return db.Insert(db.DefaultContext, task)
204+
func CreateTask(ctx context.Context, task *Task) error {
205+
return db.Insert(ctx, task)
210206
}
211207

212208
// FinishMigrateTask updates database when migrate task finished
213-
func FinishMigrateTask(task *Task) error {
209+
func FinishMigrateTask(ctx context.Context, task *Task) error {
214210
task.Status = structs.TaskStatusFinished
215211
task.EndTime = timeutil.TimeStampNow()
216212

@@ -231,6 +227,6 @@ func FinishMigrateTask(task *Task) error {
231227
}
232228
task.PayloadContent = string(confBytes)
233229

234-
_, err = db.GetEngine(db.DefaultContext).ID(task.ID).Cols("status", "end_time", "payload_content").Update(task)
230+
_, err = db.GetEngine(ctx).ID(task.ID).Cols("status", "end_time", "payload_content").Update(task)
235231
return err
236232
}

models/auth/session.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package auth
55

66
import (
7+
"context"
78
"fmt"
89

910
"code.gitea.io/gitea/models/db"
@@ -22,21 +23,21 @@ func init() {
2223
}
2324

2425
// UpdateSession updates the session with provided id
25-
func UpdateSession(key string, data []byte) error {
26-
_, err := db.GetEngine(db.DefaultContext).ID(key).Update(&Session{
26+
func UpdateSession(ctx context.Context, key string, data []byte) error {
27+
_, err := db.GetEngine(ctx).ID(key).Update(&Session{
2728
Data: data,
2829
Expiry: timeutil.TimeStampNow(),
2930
})
3031
return err
3132
}
3233

3334
// ReadSession reads the data for the provided session
34-
func ReadSession(key string) (*Session, error) {
35+
func ReadSession(ctx context.Context, key string) (*Session, error) {
3536
session := Session{
3637
Key: key,
3738
}
3839

39-
ctx, committer, err := db.TxContext(db.DefaultContext)
40+
ctx, committer, err := db.TxContext(ctx)
4041
if err != nil {
4142
return nil, err
4243
}
@@ -55,24 +56,24 @@ func ReadSession(key string) (*Session, error) {
5556
}
5657

5758
// ExistSession checks if a session exists
58-
func ExistSession(key string) (bool, error) {
59+
func ExistSession(ctx context.Context, key string) (bool, error) {
5960
session := Session{
6061
Key: key,
6162
}
62-
return db.GetEngine(db.DefaultContext).Get(&session)
63+
return db.GetEngine(ctx).Get(&session)
6364
}
6465

6566
// DestroySession destroys a session
66-
func DestroySession(key string) error {
67-
_, err := db.GetEngine(db.DefaultContext).Delete(&Session{
67+
func DestroySession(ctx context.Context, key string) error {
68+
_, err := db.GetEngine(ctx).Delete(&Session{
6869
Key: key,
6970
})
7071
return err
7172
}
7273

7374
// RegenerateSession regenerates a session from the old id
74-
func RegenerateSession(oldKey, newKey string) (*Session, error) {
75-
ctx, committer, err := db.TxContext(db.DefaultContext)
75+
func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, error) {
76+
ctx, committer, err := db.TxContext(ctx)
7677
if err != nil {
7778
return nil, err
7879
}
@@ -114,12 +115,12 @@ func RegenerateSession(oldKey, newKey string) (*Session, error) {
114115
}
115116

116117
// CountSessions returns the number of sessions
117-
func CountSessions() (int64, error) {
118-
return db.GetEngine(db.DefaultContext).Count(&Session{})
118+
func CountSessions(ctx context.Context) (int64, error) {
119+
return db.GetEngine(ctx).Count(&Session{})
119120
}
120121

121122
// CleanupSessions cleans up expired sessions
122-
func CleanupSessions(maxLifetime int64) error {
123-
_, err := db.GetEngine(db.DefaultContext).Where("expiry <= ?", timeutil.TimeStampNow().Add(-maxLifetime)).Delete(&Session{})
123+
func CleanupSessions(ctx context.Context, maxLifetime int64) error {
124+
_, err := db.GetEngine(ctx).Where("expiry <= ?", timeutil.TimeStampNow().Add(-maxLifetime)).Delete(&Session{})
124125
return err
125126
}

models/auth/webauthn.go

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ func (cred WebAuthnCredential) TableName() string {
6767
}
6868

6969
// UpdateSignCount will update the database value of SignCount
70-
func (cred *WebAuthnCredential) UpdateSignCount() error {
71-
return cred.updateSignCount(db.DefaultContext)
72-
}
73-
74-
func (cred *WebAuthnCredential) updateSignCount(ctx context.Context) error {
70+
func (cred *WebAuthnCredential) UpdateSignCount(ctx context.Context) error {
7571
_, err := db.GetEngine(ctx).ID(cred.ID).Cols("sign_count").Update(cred)
7672
return err
7773
}
@@ -113,30 +109,18 @@ func (list WebAuthnCredentialList) ToCredentials() []webauthn.Credential {
113109
}
114110

115111
// GetWebAuthnCredentialsByUID returns all WebAuthn credentials of the given user
116-
func GetWebAuthnCredentialsByUID(uid int64) (WebAuthnCredentialList, error) {
117-
return getWebAuthnCredentialsByUID(db.DefaultContext, uid)
118-
}
119-
120-
func getWebAuthnCredentialsByUID(ctx context.Context, uid int64) (WebAuthnCredentialList, error) {
112+
func GetWebAuthnCredentialsByUID(ctx context.Context, uid int64) (WebAuthnCredentialList, error) {
121113
creds := make(WebAuthnCredentialList, 0)
122114
return creds, db.GetEngine(ctx).Where("user_id = ?", uid).Find(&creds)
123115
}
124116

125117
// ExistsWebAuthnCredentialsForUID returns if the given user has credentials
126-
func ExistsWebAuthnCredentialsForUID(uid int64) (bool, error) {
127-
return existsWebAuthnCredentialsByUID(db.DefaultContext, uid)
128-
}
129-
130-
func existsWebAuthnCredentialsByUID(ctx context.Context, uid int64) (bool, error) {
118+
func ExistsWebAuthnCredentialsForUID(ctx context.Context, uid int64) (bool, error) {
131119
return db.GetEngine(ctx).Where("user_id = ?", uid).Exist(&WebAuthnCredential{})
132120
}
133121

134122
// GetWebAuthnCredentialByName returns WebAuthn credential by id
135-
func GetWebAuthnCredentialByName(uid int64, name string) (*WebAuthnCredential, error) {
136-
return getWebAuthnCredentialByName(db.DefaultContext, uid, name)
137-
}
138-
139-
func getWebAuthnCredentialByName(ctx context.Context, uid int64, name string) (*WebAuthnCredential, error) {
123+
func GetWebAuthnCredentialByName(ctx context.Context, uid int64, name string) (*WebAuthnCredential, error) {
140124
cred := new(WebAuthnCredential)
141125
if found, err := db.GetEngine(ctx).Where("user_id = ? AND lower_name = ?", uid, strings.ToLower(name)).Get(cred); err != nil {
142126
return nil, err
@@ -147,11 +131,7 @@ func getWebAuthnCredentialByName(ctx context.Context, uid int64, name string) (*
147131
}
148132

149133
// GetWebAuthnCredentialByID returns WebAuthn credential by id
150-
func GetWebAuthnCredentialByID(id int64) (*WebAuthnCredential, error) {
151-
return getWebAuthnCredentialByID(db.DefaultContext, id)
152-
}
153-
154-
func getWebAuthnCredentialByID(ctx context.Context, id int64) (*WebAuthnCredential, error) {
134+
func GetWebAuthnCredentialByID(ctx context.Context, id int64) (*WebAuthnCredential, error) {
155135
cred := new(WebAuthnCredential)
156136
if found, err := db.GetEngine(ctx).ID(id).Get(cred); err != nil {
157137
return nil, err
@@ -162,16 +142,12 @@ func getWebAuthnCredentialByID(ctx context.Context, id int64) (*WebAuthnCredenti
162142
}
163143

164144
// HasWebAuthnRegistrationsByUID returns whether a given user has WebAuthn registrations
165-
func HasWebAuthnRegistrationsByUID(uid int64) (bool, error) {
166-
return db.GetEngine(db.DefaultContext).Where("user_id = ?", uid).Exist(&WebAuthnCredential{})
145+
func HasWebAuthnRegistrationsByUID(ctx context.Context, uid int64) (bool, error) {
146+
return db.GetEngine(ctx).Where("user_id = ?", uid).Exist(&WebAuthnCredential{})
167147
}
168148

169149
// GetWebAuthnCredentialByCredID returns WebAuthn credential by credential ID
170-
func GetWebAuthnCredentialByCredID(userID int64, credID []byte) (*WebAuthnCredential, error) {
171-
return getWebAuthnCredentialByCredID(db.DefaultContext, userID, credID)
172-
}
173-
174-
func getWebAuthnCredentialByCredID(ctx context.Context, userID int64, credID []byte) (*WebAuthnCredential, error) {
150+
func GetWebAuthnCredentialByCredID(ctx context.Context, userID int64, credID []byte) (*WebAuthnCredential, error) {
175151
cred := new(WebAuthnCredential)
176152
if found, err := db.GetEngine(ctx).Where("user_id = ? AND credential_id = ?", userID, credID).Get(cred); err != nil {
177153
return nil, err
@@ -182,11 +158,7 @@ func getWebAuthnCredentialByCredID(ctx context.Context, userID int64, credID []b
182158
}
183159

184160
// CreateCredential will create a new WebAuthnCredential from the given Credential
185-
func CreateCredential(userID int64, name string, cred *webauthn.Credential) (*WebAuthnCredential, error) {
186-
return createCredential(db.DefaultContext, userID, name, cred)
187-
}
188-
189-
func createCredential(ctx context.Context, userID int64, name string, cred *webauthn.Credential) (*WebAuthnCredential, error) {
161+
func CreateCredential(ctx context.Context, userID int64, name string, cred *webauthn.Credential) (*WebAuthnCredential, error) {
190162
c := &WebAuthnCredential{
191163
UserID: userID,
192164
Name: name,
@@ -205,18 +177,14 @@ func createCredential(ctx context.Context, userID int64, name string, cred *weba
205177
}
206178

207179
// DeleteCredential will delete WebAuthnCredential
208-
func DeleteCredential(id, userID int64) (bool, error) {
209-
return deleteCredential(db.DefaultContext, id, userID)
210-
}
211-
212-
func deleteCredential(ctx context.Context, id, userID int64) (bool, error) {
180+
func DeleteCredential(ctx context.Context, id, userID int64) (bool, error) {
213181
had, err := db.GetEngine(ctx).ID(id).Where("user_id = ?", userID).Delete(&WebAuthnCredential{})
214182
return had > 0, err
215183
}
216184

217185
// WebAuthnCredentials implementns the webauthn.User interface
218-
func WebAuthnCredentials(userID int64) ([]webauthn.Credential, error) {
219-
dbCreds, err := GetWebAuthnCredentialsByUID(userID)
186+
func WebAuthnCredentials(ctx context.Context, userID int64) ([]webauthn.Credential, error) {
187+
dbCreds, err := GetWebAuthnCredentialsByUID(ctx, userID)
220188
if err != nil {
221189
return nil, err
222190
}

0 commit comments

Comments
 (0)