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:
wxiaoguang
2026-05-08 08:49:40 +08:00
committed by GitHub
parent 6a509da96e
commit 2b93eaf55b
7 changed files with 136 additions and 19 deletions
+8 -5
View File
@@ -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
}