Merge pull request #1308 from bradrydzewski/master

fix for issue #919
This commit is contained in:
Brad Rydzewski 2015-11-10 12:37:04 -08:00
commit 64908ed241

View file

@ -3,6 +3,7 @@ package datastore
import (
"database/sql"
"os"
"time"
"github.com/drone/drone/shared/envconfig"
"github.com/drone/drone/store"
@ -66,8 +67,14 @@ func Open(driver, config string) *sql.DB {
log.Errorln(err)
log.Fatalln("database connection failed")
}
setupMeddler(driver)
if err := pingDatabase(db); err != nil {
log.Errorln(err)
log.Fatalln("database ping attempts failed")
}
if err := setupDatabase(driver, db); err != nil {
log.Errorln(err)
log.Fatalln("migration failed")
@ -90,6 +97,21 @@ func openTest() *sql.DB {
return Open(driver, config)
}
// helper function to ping the database with backoff to ensure
// a connection can be established before we proceed with the
// database setup and migration.
func pingDatabase(db *sql.DB) (err error) {
for i := 0; i < 30; i++ {
err = db.Ping()
if err == nil {
return
}
log.Infof("database ping failed. retry in 1s")
time.Sleep(time.Second)
}
return
}
// helper function to setup the databsae by performing
// automated database migration steps.
func setupDatabase(driver string, db *sql.DB) error {