Semaphore/db/mysql.go

89 lines
2.2 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package db
2016-01-05 00:32:53 +01:00
import (
"database/sql"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/util"
2016-01-05 00:32:53 +01:00
_ "github.com/go-sql-driver/mysql" // imports mysql driver
"gopkg.in/gorp.v1"
"time"
log "github.com/Sirupsen/logrus"
2016-01-05 00:32:53 +01:00
)
// Mysql is the gorp database map
// db.Connect must be called to set this up correctly
2016-01-05 00:32:53 +01:00
var Mysql *gorp.DbMap
// DatabaseTimeFormat represents the format that dredd uses to validate the datetime.
// This is not the same as the raw value we pass to a new object so
// we need to use this to coerce raw values to meet the API standard
// /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}T\d{2}:\d{2}:\d{2}Z$/
const DatabaseTimeFormat = "2006-01-02T15:04:05:99Z"
// GetParsedTime returns the timestamp as it will retrieved from the database
// This allows us to create timestamp consistency on return values from create requests
func GetParsedTime(t time.Time) time.Time {
parsedTime, err := time.Parse(DatabaseTimeFormat,t.Format(DatabaseTimeFormat))
if err != nil {
log.Error(err)
}
return parsedTime
}
// Connect ensures that the db is connected and mapped properly with gorp
2016-04-18 02:58:29 +02:00
func Connect() error {
db, err := connect()
2016-01-05 00:32:53 +01:00
if err != nil {
2016-04-18 02:58:29 +02:00
return err
2016-01-05 00:32:53 +01:00
}
2016-04-30 07:54:29 +02:00
if err := db.Ping(); err != nil {
if err = createDb(); err != nil {
return err
}
db, err = connect()
if err != nil {
return err
}
if err = db.Ping(); err != nil {
return err
}
2016-01-05 00:32:53 +01:00
}
Mysql = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"}}
return nil
}
// Close closes the mysql connection and reports any errors
// called from main with a defer
func Close() {
err := Mysql.Db.Close()
if err != nil {
log.Warn("Error closing database:" + err.Error())
}
}
func createDb() error {
cfg := util.Config.MySQL
url := cfg.Username + ":" + cfg.Password + "@tcp(" + cfg.Hostname + ")/?parseTime=true&interpolateParams=true"
db, err := sql.Open("mysql", url)
if err != nil {
2016-04-30 09:52:33 +02:00
return err
2016-04-30 07:54:29 +02:00
}
2016-04-18 02:58:29 +02:00
if _, err := db.Exec("create database if not exists " + cfg.DbName); err != nil {
2016-04-30 09:52:33 +02:00
return err
2016-04-30 07:54:29 +02:00
}
2016-04-18 02:58:29 +02:00
return nil
2016-01-05 00:32:53 +01:00
}
func connect() (*sql.DB, error) {
cfg := util.Config.MySQL
url := cfg.Username + ":" + cfg.Password + "@tcp(" + cfg.Hostname + ")/" + cfg.DbName + "?parseTime=true&interpolateParams=true"
return sql.Open("mysql", url)
}