-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gorm 2 support in gollygorm2 package
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package gollygorm2 | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
|
||
"github.com/craigmj/golly" | ||
) | ||
|
||
// DbOpen is identical to golly.DbOpen, but returns a gorm database connection instead. | ||
func DbOpen(driverName, datasourceName string) (*gorm.DB, error) { | ||
var db *gorm.DB | ||
var err error | ||
if err := golly.Run(func() error { | ||
db, err = gorm.Open(driverName, datasourceName) | ||
if nil != err { | ||
if nil != golly.ErrorLog { | ||
golly.ErrorLog(err) | ||
} | ||
return err | ||
} | ||
if err = db.DB().Ping(); nil != err { | ||
db.Close() | ||
if nil != err && nil != golly.ErrorLog { | ||
golly.ErrorLog(err) | ||
} | ||
return err | ||
} | ||
return nil | ||
}); nil != err { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |