Skip to content

Commit f9c0f34

Browse files
lunnySysoev, Vladimir
authored and
Sysoev, Vladimir
committed
Adjust transaction handling via db.Context (go-gitea#20031)
1 parent b5ecae5 commit f9c0f34

File tree

4 files changed

+29
-42
lines changed

4 files changed

+29
-42
lines changed

models/db/context.go

+19-26
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,46 @@ type contextKey struct {
2323
name string
2424
}
2525

26-
// EnginedContextKey is a context key. It is used with context.Value() to get the current Engined for the context
27-
var EnginedContextKey = &contextKey{"engined"}
26+
// enginedContextKey is a context key. It is used with context.Value() to get the current Engined for the context
27+
var enginedContextKey = &contextKey{"engined"}
28+
var _ Engined = &Context{}
2829

2930
// Context represents a db context
3031
type Context struct {
3132
context.Context
32-
e Engine
33+
e Engine
34+
transaction bool
3335
}
3436

35-
// WithEngine returns a Context from a context.Context and Engine
36-
func WithEngine(ctx context.Context, e Engine) *Context {
37+
func newContext(ctx context.Context, e Engine, transaction bool) *Context {
3738
return &Context{
38-
Context: ctx,
39-
e: e.Context(ctx),
39+
Context: ctx,
40+
e: e,
41+
transaction: transaction,
4042
}
4143
}
4244

45+
// InTransaction if context is in a transaction
46+
func (ctx *Context) InTransaction() bool {
47+
return ctx.transaction
48+
}
49+
4350
// Engine returns db engine
4451
func (ctx *Context) Engine() Engine {
4552
return ctx.e
4653
}
4754

4855
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object
4956
func (ctx *Context) Value(key interface{}) interface{} {
50-
if key == EnginedContextKey {
57+
if key == enginedContextKey {
5158
return ctx
5259
}
5360
return ctx.Context.Value(key)
5461
}
5562

5663
// WithContext returns this engine tied to this context
5764
func (ctx *Context) WithContext(other context.Context) *Context {
58-
return WithEngine(other, ctx.e)
65+
return newContext(ctx, ctx.e.Context(other), ctx.transaction)
5966
}
6067

6168
// Engined structs provide an Engine
@@ -68,7 +75,7 @@ func GetEngine(ctx context.Context) Engine {
6875
if engined, ok := ctx.(Engined); ok {
6976
return engined.Engine()
7077
}
71-
enginedInterface := ctx.Value(EnginedContextKey)
78+
enginedInterface := ctx.Value(enginedContextKey)
7279
if enginedInterface != nil {
7380
return enginedInterface.(Engined).Engine()
7481
}
@@ -89,18 +96,7 @@ func TxContext() (*Context, Committer, error) {
8996
return nil, nil, err
9097
}
9198

92-
return &Context{
93-
Context: DefaultContext,
94-
e: sess,
95-
}, sess, nil
96-
}
97-
98-
// WithContext represents executing database operations
99-
func WithContext(f func(ctx *Context) error) error {
100-
return f(&Context{
101-
Context: DefaultContext,
102-
e: x,
103-
})
99+
return newContext(DefaultContext, sess, true), sess, nil
104100
}
105101

106102
// WithTx represents executing database operations on a transaction
@@ -118,10 +114,7 @@ func WithTx(f func(ctx context.Context) error, stdCtx ...context.Context) error
118114
return err
119115
}
120116

121-
if err := f(&Context{
122-
Context: parentCtx,
123-
e: sess,
124-
}); err != nil {
117+
if err := f(newContext(parentCtx, sess, true)); err != nil {
125118
return err
126119
}
127120

models/organization/org.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ type accessibleReposEnv struct {
680680
user *user_model.User
681681
team *Team
682682
teamIDs []int64
683-
e db.Engine
683+
ctx context.Context
684684
keyword string
685685
orderBy db.SearchOrderBy
686686
}
@@ -706,7 +706,7 @@ func AccessibleReposEnv(ctx context.Context, org *Organization, userID int64) (A
706706
org: org,
707707
user: user,
708708
teamIDs: teamIDs,
709-
e: db.GetEngine(ctx),
709+
ctx: ctx,
710710
orderBy: db.SearchOrderByRecentUpdated,
711711
}, nil
712712
}
@@ -717,7 +717,7 @@ func (org *Organization) AccessibleTeamReposEnv(team *Team) AccessibleReposEnvir
717717
return &accessibleReposEnv{
718718
org: org,
719719
team: team,
720-
e: db.GetEngine(db.DefaultContext),
720+
ctx: db.DefaultContext,
721721
orderBy: db.SearchOrderByRecentUpdated,
722722
}
723723
}
@@ -744,7 +744,7 @@ func (env *accessibleReposEnv) cond() builder.Cond {
744744
}
745745

746746
func (env *accessibleReposEnv) CountRepos() (int64, error) {
747-
repoCount, err := env.e.
747+
repoCount, err := db.GetEngine(env.ctx).
748748
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
749749
Where(env.cond()).
750750
Distinct("`repository`.id").
@@ -761,7 +761,7 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
761761
}
762762

763763
repoIDs := make([]int64, 0, pageSize)
764-
return repoIDs, env.e.
764+
return repoIDs, db.GetEngine(env.ctx).
765765
Table("repository").
766766
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
767767
Where(env.cond()).
@@ -783,15 +783,15 @@ func (env *accessibleReposEnv) Repos(page, pageSize int) ([]*repo_model.Reposito
783783
return repos, nil
784784
}
785785

786-
return repos, env.e.
786+
return repos, db.GetEngine(env.ctx).
787787
In("`repository`.id", repoIDs).
788788
OrderBy(string(env.orderBy)).
789789
Find(&repos)
790790
}
791791

792792
func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
793793
repoIDs := make([]int64, 0, 10)
794-
return repoIDs, env.e.
794+
return repoIDs, db.GetEngine(env.ctx).
795795
Table("repository").
796796
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
797797
Where(env.cond()).
@@ -812,7 +812,7 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*repo_model.Repository, error) {
812812
return repos, nil
813813
}
814814

815-
return repos, env.e.
815+
return repos, db.GetEngine(env.ctx).
816816
In("`repository`.id", repoIDs).
817817
Find(&repos)
818818
}

models/repo/repo.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
319319

320320
// UnitEnabled if this repository has the given unit enabled
321321
func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) {
322-
if err := db.WithContext(func(ctx *db.Context) error {
323-
result = repo.UnitEnabledCtx(ctx, tp)
324-
return nil
325-
}); err != nil {
326-
log.Error("repo.UnitEnabled: %v", err)
327-
}
328-
return result
322+
return repo.UnitEnabledCtx(db.DefaultContext, tp)
329323
}
330324

331325
// UnitEnabled if this repository has the given unit enabled

modules/repository/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili
229229
}
230230

231231
// Create/Remove git-daemon-export-ok for git-daemon...
232-
if err := CheckDaemonExportOK(db.WithEngine(ctx, e), repo); err != nil {
232+
if err := CheckDaemonExportOK(ctx, repo); err != nil {
233233
return err
234234
}
235235

0 commit comments

Comments
 (0)