fix: avoid panic when open fails

This commit is contained in:
black 2023-06-01 10:54:36 +08:00 committed by Cr
parent 740f2be453
commit c1ea730367
2 changed files with 11 additions and 1 deletions

View File

@ -181,7 +181,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
err = config.Dialector.Initialize(db)
if err != nil {
if db, err := db.DB(); err == nil {
if db, _ := db.DB(); db != nil {
_ = db.Close()
}
}

View File

@ -3,9 +3,19 @@ package tests_test
import (
"testing"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func TestOpen(t *testing.T) {
dsn := "gorm:gorm@tcp(localhost:9910)/gorm?loc=Asia%2FHongKong" // invalid loc
_, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err == nil {
t.Fatalf("should returns error but got nil")
}
}
func TestReturningWithNullToZeroValues(t *testing.T) {
dialect := DB.Dialector.Name()
switch dialect {