Skip to content

Commit

Permalink
fix: avoid panic when open fails
Browse files Browse the repository at this point in the history
  • Loading branch information
black-06 authored and a631807682 committed Jun 1, 2023
1 parent 740f2be commit c1ea730
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c1ea730

Please sign in to comment.