mirror of https://github.com/go-gorm/gorm.git
fix: avoid panic when open fails
This commit is contained in:
parent
740f2be453
commit
c1ea730367
2
gorm.go
2
gorm.go
|
@ -181,7 +181,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
|
||||||
err = config.Dialector.Initialize(db)
|
err = config.Dialector.Initialize(db)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db, err := db.DB(); err == nil {
|
if db, _ := db.DB(); db != nil {
|
||||||
_ = db.Close()
|
_ = db.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,19 @@ package tests_test
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"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) {
|
func TestReturningWithNullToZeroValues(t *testing.T) {
|
||||||
dialect := DB.Dialector.Name()
|
dialect := DB.Dialector.Name()
|
||||||
switch dialect {
|
switch dialect {
|
||||||
|
|
Loading…
Reference in New Issue