gorm/callbacks/transaction.go
MOREL Matthieu 21423b914f require lint before tests
use golangci/golangci-lint-action instead of reviewdog/action-golangci-lint as the second was not reporting any failures even if there was some.

Report code coverage with codecov/codecov-action
I have set some flags per dialect and go version

Several linters has been fixed, some disabled so the build can pass
2021-12-27 09:33:36 +01:00

35 lines
695 B
Go

package callbacks
import (
"errors"
"gorm.io/gorm"
)
func BeginTransaction(db *gorm.DB) {
if !db.Config.SkipDefaultTransaction && db.Error == nil {
if tx := db.Begin(); tx.Error == nil {
db.Statement.ConnPool = tx.Statement.ConnPool
db.InstanceSet("gorm:started_transaction", true)
} else if errors.Is(tx.Error, gorm.ErrInvalidTransaction) {
tx.Error = nil
} else {
db.Error = tx.Error
}
}
}
func CommitOrRollbackTransaction(db *gorm.DB) {
if !db.Config.SkipDefaultTransaction {
if _, ok := db.InstanceGet("gorm:started_transaction"); ok {
if db.Error != nil {
db.Rollback()
} else {
db.Commit()
}
db.Statement.ConnPool = db.ConnPool
}
}
}