-
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.
Merge pull request #58 from scribd/laynax/SERF-498/upgrade-gorm
[SERF-498] Upgrade gorm to v2
- Loading branch information
Showing
16 changed files
with
414 additions
and
385 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
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
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
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 |
---|---|---|
@@ -1,31 +1,50 @@ | ||
package database | ||
|
||
import ( | ||
"github.com/DATA-DOG/go-txdb" | ||
"github.com/jinzhu/gorm" | ||
"strconv" | ||
"time" | ||
|
||
// Imports required gorm MySQL dialect. | ||
_ "github.com/jinzhu/gorm/dialects/mysql" | ||
"github.com/DATA-DOG/go-txdb" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
const testEnv = "test" | ||
|
||
// NewConnection returns a new Gorm database connection. | ||
func NewConnection(config *Config, environment string) (*gorm.DB, error) { | ||
var db *gorm.DB | ||
var err error | ||
|
||
connectionDetails := NewConnectionDetails(config) | ||
|
||
switch environment { | ||
case "test": | ||
txdb.Register("txdb", connectionDetails.Dialect, connectionDetails.String()) | ||
db, err = gorm.Open(connectionDetails.Dialect, "txdb", "tx_1") | ||
default: | ||
db, err = gorm.Open(connectionDetails.Dialect, connectionDetails.String()) | ||
connectionString := connectionDetails.String() | ||
driverName := connectionDetails.Dialect | ||
|
||
// Register the test driver and mock driver name and connection string in test environment. | ||
if environment == testEnv { | ||
// Using time.Now() as a unique identifier for the test database so that we can call NewConnection() | ||
// multiple times without getting an error. | ||
testDriverName := strconv.Itoa(int(time.Now().UnixNano())) | ||
|
||
txdb.Register(testDriverName, connectionDetails.Dialect, connectionString) | ||
driverName = testDriverName | ||
connectionString = testDriverName | ||
} | ||
|
||
if err == nil { | ||
db.DB().SetMaxIdleConns(connectionDetails.Pool) | ||
dialector := mysql.New(mysql.Config{ | ||
DSN: connectionString, | ||
DriverName: driverName, | ||
}) | ||
|
||
db, err := gorm.Open(dialector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, err | ||
sqlDB, err := db.DB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sqlDB.SetMaxIdleConns(config.Pool) | ||
|
||
return db, nil | ||
} |
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
Oops, something went wrong.