Fix comment

This commit is contained in:
Krisdiano 2025-06-03 14:56:59 +08:00
parent fba5580bb9
commit 89f6bea94a
2 changed files with 20 additions and 39 deletions

View File

@ -2,9 +2,7 @@ package callbacks
import ( import (
"fmt" "fmt"
"os"
"reflect" "reflect"
"strconv"
"strings" "strings"
"gorm.io/gorm" "gorm.io/gorm"
@ -38,21 +36,12 @@ func BeforeCreate(db *gorm.DB) {
// Create create hook // Create create hook
func Create(config *Config) func(db *gorm.DB) { func Create(config *Config) func(db *gorm.DB) {
supportReturning := utils.Contains(config.CreateClauses, "RETURNING") supportReturning := utils.Contains(config.CreateClauses, "RETURNING")
rawSupportReturning := supportReturning
return func(db *gorm.DB) { return func(db *gorm.DB) {
if db.Error != nil { if db.Error != nil {
return return
} }
mock := os.Getenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING")
mockSupportReturning, err := strconv.ParseBool(mock)
if err == nil {
supportReturning = mockSupportReturning
} else {
supportReturning = rawSupportReturning
}
if db.Statement.Schema != nil { if db.Statement.Schema != nil {
if !db.Statement.Unscoped { if !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.CreateClauses { for _, c := range db.Statement.Schema.CreateClauses {

View File

@ -6,7 +6,6 @@ import (
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"fmt" "fmt"
"os"
"regexp" "regexp"
"testing" "testing"
"time" "time"
@ -557,28 +556,6 @@ func TestCreateWithDisableLastInsertID(t *testing.T) {
if isSQLServer() { if isSQLServer() {
t.Skip("SQLServer driver doesn't use default create hook in gorm") t.Skip("SQLServer driver doesn't use default create hook in gorm")
} }
mockCreateSupportReturning := func() func() {
revertCreateSupportReturning := func() {
os.Setenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING", "")
}
os.Setenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING", "false")
return revertCreateSupportReturning
}
mockConnPoolExec := func() func() {
rawPool := DB.ConnPool
DB.ConnPool = ConnPoolLastInsertIDMock{rawPool}
rawStatementPool := DB.Statement.ConnPool
DB.Statement.ConnPool = ConnPoolLastInsertIDMock{rawStatementPool}
return func() {
DB.ConnPool = rawPool
DB.Statement.ConnPool = rawStatementPool
}
}
defer mockCreateSupportReturning()()
defer mockConnPoolExec()()
user := GetUser("TestCreateWithDisableLastInsertID0", Config{}) user := GetUser("TestCreateWithDisableLastInsertID0", Config{})
err := DB.Create(user).Error err := DB.Create(user).Error
@ -586,12 +563,27 @@ func TestCreateWithDisableLastInsertID(t *testing.T) {
t.Fatalf("it should be error") t.Fatalf("it should be error")
} }
DB.DisableLastInsertID = true // create a new connection with new config
defer func() { db, err := OpenTestConnection(&gorm.Config{DisableLastInsertID: true})
DB.DisableLastInsertID = false if err != nil {
}() t.Fatal("failed to connect database")
}
err = DB.Create(user).Error // mock driver result
mockConnPoolExec := func() func() {
rawPool := db.ConnPool
db.ConnPool = ConnPoolLastInsertIDMock{rawPool}
rawStatementPool := db.Statement.ConnPool
db.Statement.ConnPool = ConnPoolLastInsertIDMock{rawStatementPool}
return func() {
db.ConnPool = rawPool
db.Statement.ConnPool = rawStatementPool
}
}
defer mockConnPoolExec()()
user = GetUser("TestCreateWithDisableLastInsertID1", Config{})
err = db.Create(user).Error
if err != nil { if err != nil {
t.Fatalf("it should be nil, got %v", err) t.Fatalf("it should be nil, got %v", err)
} }