fix for issue #919

This commit is contained in:
Brad Rydzewski 2015-11-10 12:32:09 -08:00
parent a13ce5cb6f
commit 5e06cb093f

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 {