Commit 598dd68b authored by Ajit Jagtap's avatar Ajit Jagtap
Browse files

Merge branch 'mayuriMysqlChanges' into 'devbranch'

Mysql changes  and test cases

See merge request !46
parents b385b8bf 3c29f9e8
Branches
Tags
2 merge requests!54Devbranch,!46Mysql changes and test cases
Showing with 200 additions and 25 deletions
......@@ -48,3 +48,13 @@ const (
// HTTP400ERROR is used to check 400 status
const HTTP400ERROR = 400
// MySQL Default Parameters
const (
// MAX_IDLE_CONNECTIONS - MaxIdleConns
MAX_IDLE_CONNECTIONS = 100
// MAX_OPEN_CONNECTIONS - MaxOpenConns
MAX_OPEN_CONNECTIONS = 5000
// CONNECTION_MAX_LIFETIME - ConnMaxLifetime
CONNECTION_MAX_LIFETIME = 3 * 24 * time.Hour
)
# configuration file for mysql
[MysqlHosts]
[MysqlHosts.localhost]
Server = "127.0.0.1:3306"
Username = "root"
Password = "root"
Protocol="tcp"
Database = "testdb"
MaxIdleConns = 200
[[MysqlHosts.localhost.Parameters]]
ParamKey = "parseTime"
ParamValue = "true"
[[MysqlHosts.localhost.Parameters]]
ParamKey = "multiStatements"
ParamValue = "true"
[MysqlHosts.dev]
Server = "10.2.10.15:3306"
Username = "dev"
Password = "dev#@!"
Protocol="tcp"
Database = "livedemy"
package mysql
import (
"fmt"
"sync"
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/configmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/constantmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
_ "github.com/go-sql-driver/mysql"
......@@ -14,32 +14,35 @@ import (
)
// Hold a single global connection (pooling provided by sql driver)
var sqlConnection *dbr.Connection
var sqlConnections map[string]*dbr.Connection
var connectionError error
var sqlOnce sync.Once
var config tomlConfig
var defaultHost string
// MYSQLConnection -
type mysqlConnection struct {
Host string `json:"host" bson:"host"`
Port string `json:"port" bson:"port"`
Dbname string `json:"dbname" bson:"dbname"`
Server string `json:"server" bson:"server"`
Username string `json:"username" bson:"username"`
Password string `json:"password " bson:"password "`
Protocol string `json:"protocol" bson:"protocol"`
Parameters []string `json:"params" bson:"params"`
Database string `json:"database" bson:"database"`
Parameters []param `json:"params" bson:"params"`
MaxIdleConns int `json:"maxIdleConns" bson:"maxIdleConns"`
MaxOpenConns int `json:"maxOpenConns" bson:"maxOpenConns"`
ConnMaxLifetime time.Duration `json:"connMaxLifetime" bson:"connMaxLifetime"`
}
type param struct {
ParamKey string `json:"paramkey" bson:"paramkey"`
ParamValue string `json:"paramvalue" bson:"paramvalue"`
}
type tomlConfig struct {
MYSQLConnections map[string]mysqlConnection
MysqlHosts map[string]mysqlConnection
}
// Init initializes MYSQL Connections for given toml file
func Init(tomlFilepath string) error {
func Init(tomlFilepath string, defaultHostName string) (map[string]*dbr.Connection, error) {
sqlOnce.Do(func() {
sqlConnections = make(map[string]*dbr.Connection)
_, err := configmdl.InitConfig(tomlFilepath, &config)
......@@ -48,32 +51,64 @@ func Init(tomlFilepath string) error {
connectionError = err
return
}
for connectionName, connectionDetails := range config.MYSQLConnections {
connection, err := dbr.Open("mysql", connectionDetails.Username+":"+connectionDetails.Password+"@"+connectionDetails.Protocol+"("+connectionDetails.Host+":"+connectionDetails.Port+")/"+connectionDetails.Dbname)
if err != nil {
sessionError = err
for connectionName, connectionDetails := range config.MysqlHosts {
paramsString := ""
if len(connectionDetails.Parameters) > 0 {
for paramIndex, param := range connectionDetails.Parameters {
if paramsString == "" {
paramsString = "?"
}
paramsString = paramsString + param.ParamKey + "=" + param.ParamValue
hasNextParam := paramIndex+1 < len(connectionDetails.Parameters)
if hasNextParam {
paramsString = paramsString + "&"
}
}
}
//connection, err := dbr.Open("mysql", "root:root@tcp(127.0.0.1:3306)/testdb?parseTime=true", nil)
connection, err := dbr.Open("mysql", connectionDetails.Username+":"+connectionDetails.Password+"@"+connectionDetails.Protocol+"("+connectionDetails.Server+")/"+connectionDetails.Database+paramsString, nil)
if errormdl.CheckErr1(err) != nil {
loggermdl.LogError(err)
connectionError = err
return
}
fmt.Println(mysqlDSN.String())
if connectionDetails.MaxIdleConns == 0 {
connectionDetails.MaxIdleConns = constantmdl.MAX_IDLE_CONNECTIONS // default is 2
}
if connectionDetails.MaxOpenConns == 0 {
connectionDetails.MaxOpenConns = constantmdl.MAX_OPEN_CONNECTIONS // default there's no limit
}
if connectionDetails.ConnMaxLifetime == 0 {
connectionDetails.ConnMaxLifetime = constantmdl.CONNECTION_MAX_LIFETIME
}
connection.SetMaxIdleConns(connectionDetails.MaxIdleConns)
connection.SetMaxOpenConns(connectionDetails.MaxOpenConns)
connection.SetConnMaxLifetime(connectionDetails.ConnMaxLifetime)
sqlConnections[connectionName] = connection
}
defaultHost = defaultHostName
})
return errormdl.CheckErr(sessionError)
return sqlConnections, errormdl.CheckErr2(connectionError)
}
//GetMYSQLConnection -
func GetMYSQLConnection(connectionName string) (*dbr.Connection, error) {
if sqlConnections == nil {
if errormdl.CheckBool(sqlConnections == nil) {
return nil, errormdl.Wrap("MYSQL_INIT_NOT_DONE")
}
if session, ok := sqlConnections[connectionName]; ok {
if connectionName == "" {
if instance, keyExist := sqlConnections[defaultHost]; keyExist {
return instance, nil
}
}
if session, keyExist := sqlConnections[connectionName]; keyExist {
return session, nil
}
return nil, errormdl.Wrap("Connection not found for instance: " + connectionName)
return nil, errormdl.Wrap("Connection not found for host: " + connectionName)
}
// GetMultipleSQLConnection -
func GetMultipleSQLConnection(tomlFilepath string) (map[string]*dbr.Connection, error) {
/* func GetMultipleSQLConnection(tomlFilepath string) (map[string]*dbr.Connection, error) {
sqlOnce.Do(func() {
_, err := configmdl.InitConfig(tomlFilepath, &config)
if errormdl.CheckErr(err) != nil {
......@@ -81,9 +116,9 @@ func GetMultipleSQLConnection(tomlFilepath string) (map[string]*dbr.Connection,
connectionError = err
return
}
connectionsToReturn = make(map[string]*dbr.Connection)
for connectionName, mysqlConnectionObj := range config.MYSQLConnections {
connection, err := dbr.Open("mysql", mysqlConnectionObj.Username+":"+mysqlConnectionObj.Password+"@"+mysqlConnectionObj.Protocol+"("+mysqlConnectionObj.Host+":"+mysqlConnectionObj.Port+")/"+mysqlConnectionObj.Dbname, nil)
sqlConnections = make(map[string]*dbr.Connection)
for connectionName, mysqlConnectionObj := range config.MysqlHosts {
connection, err := dbr.Open("mysql", mysqlConnectionObj.Username+":"+mysqlConnectionObj.Password+"@"+mysqlConnectionObj.Protocol+"("+mysqlConnectionObj.Server+")/"+mysqlConnectionObj.Database, nil)
if err != nil {
connectionError = err
}
......@@ -97,9 +132,8 @@ func GetMultipleSQLConnection(tomlFilepath string) (map[string]*dbr.Connection,
duration := 3 * 24 * time.Hour
connection.SetConnMaxLifetime(duration)
}
sqlConnection = connection
connectionsToReturn[connectionName] = sqlConnection
sqlConnections[connectionName] = connection
}
})
return connectionsToReturn, errormdl.CheckErr(connectionError)
}
return sqlConnections, errormdl.CheckErr(connectionError)
} */
package mysql
import (
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/stretchr/testify/assert"
)
func TestInit(t *testing.T) {
connmap, err := Init("mysql-config.toml", "localhost")
if err != nil {
loggermdl.LogError("err: ", err)
}
db := connmap["localhost"]
stmt, _ := db.Prepare("INSERT person SET Name=?")
stmt.Exec("astaxie1")
assert.NoError(t, err, "This should not return error")
}
func TestInitByPing(t *testing.T) {
connmap, err := Init("mysql-config.toml", "localhost")
if err != nil {
loggermdl.LogError("err: ", err)
}
db := connmap["localhost"]
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
assert.NoError(t, err, "This should not return error")
}
func TestInitWrongDefaultHostname(t *testing.T) {
connmap, err := Init("mysql-config.toml", "localhostWrong")
if err != nil {
loggermdl.LogError("err: ", err)
}
db := connmap["localhost"]
stmt, _ := db.Prepare("INSERT person SET Name=?")
stmt.Exec("astaxie1")
assert.NoError(t, err, "This should not return error")
}
func TestInitMultipleConnections(t *testing.T) {
connmap, err := Init("mysql-config.toml", "localhost")
if err != nil {
loggermdl.LogError("err: ", err)
}
db := connmap["dev"]
err2 := db.Ping()
if err2 != nil {
panic(err2.Error()) // proper error handling instead of panic in your app
}
stmt, _ := db.Prepare("INSERT person SET Name=?")
stmt.Exec("astaxie111")
assert.NoError(t, err, "This should not return error")
}
// func TestInitErrorWrongTomlFilePath(t *testing.T) {
// _, err := Init("mysql-config1.toml", "")
// assert.Error(t, err, "This should return error")
// }
// func TestInitError1(t *testing.T) {
// errormdl.IsTestingNegetiveCaseOn1 = true
// _, err := Init("mysql-config.toml", "localhost")
// errormdl.IsTestingNegetiveCaseOn1 = false
// assert.Error(t, err, "This should return error")
// }
func TestGetMYSQLConnection(t *testing.T) {
Init("mysql-config.toml", "localhost")
db, err := GetMYSQLConnection("localhost")
stmt, _ := db.Prepare("INSERT person SET Name=?")
stmt.Exec("rr1")
assert.NoError(t, err, "This should not return error")
}
func TestGetMYSQLConnectionEmptyHost(t *testing.T) {
Init("mysql-config.toml", "localhost")
_, err := GetMYSQLConnection("")
assert.NoError(t, err, "This should not return error")
}
func TestGetMYSQLConnectionWrongHost(t *testing.T) {
Init("mysql-config.toml", "localhost")
_, err := GetMYSQLConnection("localhost2")
assert.Error(t, err, "This should return error")
}
func TestGetMYSQLConnectionWrongDefaultHost(t *testing.T) {
Init("mysql-config.toml", "localhost2")
_, err := GetMYSQLConnection("localhost")
assert.NoError(t, err, "This should not return error")
}
// func TestGetMYSQLConnectionWrongDefaultHostAndEmptyHost(t *testing.T) {
// Init("mysql-config.toml", "localhost2")
// _, err := GetMYSQLConnection("")
// assert.Error(t, err, "This should return error")
// }
func TestGetMYSQLConnectionError(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckBool = true
_, err := GetMYSQLConnection("localhost123")
errormdl.IsTestingNegetiveCaseOnCheckBool = false
assert.Error(t, err, "This should return error")
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment