Skip to content

Commit 765770d

Browse files
committed
feat: orm 支持 gorm 事务封装
1 parent 13b8fc4 commit 765770d

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

kratos/orm/gorm_logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (gl *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (str
9393
timeUsed := float64(elapsed.Nanoseconds()) / 1e6
9494

9595
fields := make([]interface{}, 0)
96-
fields = append(fields, "timeUsedMicroseconds", timeUsed)
96+
fields = append(fields, "timeUsed", timeUsed)
9797

9898
sql, rows := fc()
9999

@@ -108,7 +108,7 @@ func (gl *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (str
108108
case elapsed > gl.SlowThreshold && gl.SlowThreshold != 0:
109109
fields = append(fields, "sql", sql)
110110
fields = append(fields, "rows", rows)
111-
fields = append(fields, "slow-elapsed", gl.SlowThreshold)
111+
fields = append(fields, "slowElapsed", gl.SlowThreshold)
112112
gl.dbLog.Warnw(fields...)
113113
// normal
114114
default:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package orm_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/glebarez/sqlite"
9+
"github.com/go-kratos/kratos/v2/log"
10+
"gorm.io/gorm"
11+
12+
"github.com/omalloc/contrib/kratos/orm"
13+
)
14+
15+
var cfg = `
16+
server:
17+
http:
18+
addr: 0.0.0.0:8000
19+
timeout: 1s
20+
grpc:
21+
addr: 0.0.0.0:9000
22+
timeout: 1s
23+
data:
24+
database:
25+
driver: sqlite
26+
source: file:test.db?cache=shared&mode=memory
27+
redis:
28+
addr: 127.0.0.1:6379
29+
read_timeout: 0.2s
30+
write_timeout: 0.2s
31+
`
32+
33+
type Data struct {
34+
db *gorm.DB
35+
}
36+
37+
func newData() *Data {
38+
db, err := orm.New(
39+
orm.WithDriver(sqlite.Open("file:test.db?cache=shared&mode=memory&charset=utf8mb4&parseTime=true&loc=Local")),
40+
orm.WithTracingOpts(orm.WithDatabaseName("test")),
41+
orm.WithLogger(
42+
orm.WithDebug(),
43+
orm.WIthSlowThreshold(time.Second*2),
44+
orm.WithSkipCallerLookup(true),
45+
orm.WithSkipErrRecordNotFound(true),
46+
orm.WithLogHelper(log.NewFilter(log.GetLogger(), log.FilterLevel(log.LevelDebug))),
47+
),
48+
)
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
return &Data{
54+
db: db,
55+
}
56+
}
57+
58+
func TestTransaction(t *testing.T) {
59+
data := newData()
60+
data.db.AutoMigrate(&User{})
61+
62+
txm := orm.NewTransactionManager(data.db)
63+
64+
txm.WithContext(context.TODO(), func(ctx context.Context, tx *gorm.DB) error {
65+
return tx.Model(&User{}).Create(&User{Name: "test1"}).Error
66+
})
67+
}

kratos/orm/transaction.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package orm
2+
3+
import (
4+
"context"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
// txContextKey gorm database transaction context key
10+
type txContextKey struct{}
11+
12+
type Transaction interface {
13+
WithContext(context.Context, func(ctx context.Context, tx *gorm.DB) error) error
14+
}
15+
16+
type transactionManager struct {
17+
db *gorm.DB
18+
}
19+
20+
func NewTransactionManager(dataSource *gorm.DB) Transaction {
21+
return &transactionManager{
22+
db: dataSource,
23+
}
24+
}
25+
26+
func (tm *transactionManager) WithContext(ctx context.Context, fn func(ctx context.Context, tx *gorm.DB) error) error {
27+
if tx, ok := ctx.Value(txContextKey{}).(*gorm.DB); ok {
28+
return fn(ctx, tx)
29+
}
30+
31+
return tm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
32+
return fn(context.WithValue(ctx, txContextKey{}, tx), tx)
33+
})
34+
}

0 commit comments

Comments
 (0)