src.dualinventive.com/go/lib/database/mysql.go

27 lines
776 B
Go

package database
import (
"fmt"
"github.com/jinzhu/gorm"
// Import only the mysql driver for the connection. Functions are not needed.
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// NewMySQLDatabase creates a new MySQL connection to a MySQL database using the provided credentials
func NewMySQLDatabase(host, user, password, name string) (*gorm.DB, error) {
constring := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
user,
password,
host,
name)
dbHandle, err := gorm.Open("mysql", constring)
if err != nil {
return nil, fmt.Errorf("failed to connect database: %v", err)
}
// See upstream issue: https://github.com/go-sql-driver/mysql/issues/657
// See our issue BACKEND-222
dbHandle.DB().SetMaxIdleConns(0)
return dbHandle, err
}