diff --git a/store/datastore/store.go b/store/datastore/store.go index 107b8092..bb279ca8 100644 --- a/store/datastore/store.go +++ b/store/datastore/store.go @@ -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 {