2022-10-12 18:57:22 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-02 15:19:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-12 18:57:22 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getTxAndContext() (tx *gorm.DB, ctx context.Context) {
|
|
|
|
tx = global.DB.Begin()
|
2022-11-02 15:19:14 +08:00
|
|
|
ctx = context.WithValue(context.Background(), constant.DB, tx)
|
2022-10-12 18:57:22 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-21 15:54:34 +08:00
|
|
|
|
|
|
|
func getTxByContext(ctx context.Context) (*gorm.DB, context.Context) {
|
|
|
|
tx, ok := ctx.Value(constant.DB).(*gorm.DB)
|
|
|
|
if ok {
|
|
|
|
return tx, ctx
|
|
|
|
}
|
|
|
|
tx = global.DB.Begin()
|
|
|
|
ctx = context.WithValue(context.Background(), constant.DB, tx)
|
|
|
|
return tx, ctx
|
|
|
|
}
|