mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-26 21:39:42 +09:00
refactor: only reset a database table when the table's data was changed (#37573)
Reduce CI time Saves about 3 minutes for each test suit test-unit: 13min -> 10min (-race) test-pgsql: 24min -> 20min (-race) test-mysql: 15min -> 12min test-mssql: 16min -> 12min --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
This commit is contained in:
co-authored by
silverwind
Claude
parent
6a509da96e
commit
2b93eaf55b
@@ -17,12 +17,15 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type engineContextKeyType struct{}
|
||||
type contextKey struct{ key string }
|
||||
|
||||
var engineContextKey = engineContextKeyType{}
|
||||
var (
|
||||
contextKeyEngine = contextKey{"engine"}
|
||||
ContextKeyTestFixtures = contextKey{"test-fixtures"}
|
||||
)
|
||||
|
||||
func withContextEngine(ctx context.Context, e Engine) context.Context {
|
||||
return context.WithValue(ctx, engineContextKey, e)
|
||||
return context.WithValue(ctx, contextKeyEngine, e)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -68,7 +71,7 @@ func contextSafetyCheck(e Engine) {
|
||||
|
||||
// GetEngine gets an existing db Engine/Statement or creates a new Session
|
||||
func GetEngine(ctx context.Context) Engine {
|
||||
if engine, ok := ctx.Value(engineContextKey).(Engine); ok {
|
||||
if engine, ok := ctx.Value(contextKeyEngine).(Engine); ok {
|
||||
// if reusing the existing session, need to do "contextSafetyCheck" because the Iterate creates a "autoResetStatement=false" session
|
||||
contextSafetyCheck(engine)
|
||||
return engine
|
||||
@@ -309,7 +312,7 @@ func InTransaction(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func getTransactionSession(ctx context.Context) *xorm.Session {
|
||||
e, _ := ctx.Value(engineContextKey).(Engine)
|
||||
e, _ := ctx.Value(contextKeyEngine).(Engine)
|
||||
if sess, ok := e.(*xorm.Session); ok && sess.IsInTx() {
|
||||
return sess
|
||||
}
|
||||
|
||||
@@ -22,11 +22,17 @@ type EngineHook struct {
|
||||
var _ contexts.Hook = (*EngineHook)(nil)
|
||||
|
||||
func (*EngineHook) BeforeProcess(c *contexts.ContextHook) (context.Context, error) {
|
||||
if c.Ctx.Value(ContextKeyTestFixtures) != nil {
|
||||
return c.Ctx, nil
|
||||
}
|
||||
ctx, _ := gtprof.GetTracer().Start(c.Ctx, gtprof.TraceSpanDatabase)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (h *EngineHook) AfterProcess(c *contexts.ContextHook) error {
|
||||
if c.Ctx.Value(ContextKeyTestFixtures) != nil {
|
||||
return nil
|
||||
}
|
||||
span := gtprof.GetContextSpan(c.Ctx)
|
||||
if span != nil {
|
||||
// Do not record SQL parameters here:
|
||||
|
||||
Reference in New Issue
Block a user