woodpecker/store/datastore/store.go

133 lines
3.1 KiB
Go
Raw Normal View History

package datastore
import (
"database/sql"
"os"
2015-11-10 20:32:09 +00:00
"time"
"github.com/drone/drone/store"
"github.com/drone/drone/store/datastore/ddl"
"github.com/russross/meddler"
"github.com/Sirupsen/logrus"
)
// datastore is an implementation of a model.Store built on top
// of the sql/database driver with a relational database backend.
type datastore struct {
*sql.DB
2017-03-28 09:13:13 +00:00
driver string
config string
}
// New creates a database connection for the given driver and datasource
// and returns a new Store.
func New(driver, config string) store.Store {
2017-03-28 09:13:13 +00:00
return &datastore{
DB: open(driver, config),
driver: driver,
config: config,
}
2015-10-28 01:48:04 +00:00
}
// From returns a Store using an existing database connection.
2015-10-28 01:48:04 +00:00
func From(db *sql.DB) store.Store {
2017-03-28 09:13:13 +00:00
return &datastore{DB: db}
}
// open opens a new database connection with the specified
// driver and connection string and returns a store.
func open(driver, config string) *sql.DB {
db, err := sql.Open(driver, config)
if err != nil {
logrus.Errorln(err)
logrus.Fatalln("database connection failed")
}
if driver == "mysql" {
// per issue https://github.com/go-sql-driver/mysql/issues/257
db.SetMaxIdleConns(0)
}
2015-11-10 20:32:09 +00:00
setupMeddler(driver)
2015-11-10 20:32:09 +00:00
if err := pingDatabase(db); err != nil {
logrus.Errorln(err)
logrus.Fatalln("database ping attempts failed")
2015-11-10 20:32:09 +00:00
}
if err := setupDatabase(driver, db); err != nil {
logrus.Errorln(err)
logrus.Fatalln("migration failed")
}
return db
}
2017-03-28 09:13:13 +00:00
// openTest opens a new database connection for testing purposes.
// The database driver and connection string are provided by
// environment variables, with fallback to in-memory sqlite.
func openTest() *sql.DB {
var (
driver = "sqlite3"
config = ":memory:"
)
if os.Getenv("DATABASE_DRIVER") != "" {
driver = os.Getenv("DATABASE_DRIVER")
2016-03-25 20:24:16 +00:00
config = os.Getenv("DATABASE_CONFIG")
}
return open(driver, config)
}
2017-03-28 09:13:13 +00:00
// newTest creates a new database connection for testing purposes.
// The database driver and connection string are provided by
// environment variables, with fallback to in-memory sqlite.
func newTest() *datastore {
var (
driver = "sqlite3"
config = ":memory:"
)
if os.Getenv("DATABASE_DRIVER") != "" {
driver = os.Getenv("DATABASE_DRIVER")
config = os.Getenv("DATABASE_CONFIG")
}
return &datastore{
DB: open(driver, config),
driver: driver,
config: config,
}
}
2015-11-10 20:32:09 +00:00
// 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
}
logrus.Infof("database ping failed. retry in 1s")
2015-11-10 20:32:09 +00:00
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 {
2017-05-13 07:56:23 +00:00
return ddl.Migrate(driver, db)
}
// helper function to setup the meddler default driver
// based on the selected driver name.
func setupMeddler(driver string) {
switch driver {
case "sqlite3":
meddler.Default = meddler.SQLite
case "mysql":
meddler.Default = meddler.MySQL
case "postgres":
meddler.Default = meddler.PostgreSQL
}
}