lib/app/mysql.go

34 lines
738 B
Go
Raw Normal View History

2021-01-10 19:50:48 +00:00
package app
import (
"database/sql"
2021-01-13 02:41:43 +00:00
// need this for SQL
_ "github.com/go-sql-driver/mysql"
2021-01-10 19:50:48 +00:00
)
// MyDB is the MySQL handler variable
var MyDB *sql.DB
// InitDB initializes the MySQL database using DSN information from the config file
2021-01-27 19:31:41 +00:00
//
2021-01-10 19:50:48 +00:00
func InitDB() {
var db *sql.DB
var err error
2021-01-27 19:31:41 +00:00
if GetDBMS() != "mysql" {
2021-01-10 19:50:48 +00:00
sugar.Fatalf("app.InitDB: 💣 ⛔ unknown DBMS: %s", GetDBMS())
return
}
2021-01-27 19:31:41 +00:00
db, err = sql.Open("mysql", GetDSN())
2021-01-10 19:50:48 +00:00
if err != nil {
sugar.Fatalf("app.InitDB: 💣 ⛔ Can't log on to DBMS host: %w", err)
return
}
2021-01-27 19:31:41 +00:00
_, err = db.Exec("set foreign_key_check=0")
if err != nil {
sugar.Warnf("app.InitDB: 💣⛔ can't disable foreign key check")
}
2021-01-10 19:50:48 +00:00
MyDB = db
sugar.Debugf("app.InitDB: 👍 📤 %s", GetDBMS())
}