fathom/pkg/datastore/datastore.go

64 lines
1.4 KiB
Go
Raw Normal View History

package datastore
import (
2018-04-25 11:45:21 +00:00
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql" // mysql driver
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq" // postgresql driver
"github.com/rubenv/sql-migrate"
"log"
)
var dbx *sqlx.DB
2018-04-25 11:45:21 +00:00
// ErrNoResults is returned when a query yielded 0 results
var ErrNoResults = errors.New("datastore: query returned 0 results")
2018-04-25 11:45:21 +00:00
// Init creates a database connection pool (using sqlx)
func Init(driver string, host string, name string, user string, password string) *sqlx.DB {
dbx = New(driver, getDSN(driver, host, name, user, password))
// run migrations
runMigrations(driver)
return dbx
}
// New creates a new database pool
func New(driver string, dsn string) *sqlx.DB {
dbx := sqlx.MustConnect(driver, dsn)
return dbx
}
func getDSN(driver string, host string, name string, user string, password string) string {
var dsn = fmt.Sprintf("%s:%s@%s/%s", user, password, host, name)
switch driver {
case "postgres":
dsn = "postgres://" + dsn
case "mysql":
dsn = dsn + "?parseTime=true"
}
return dsn
}
func runMigrations(driver string) {
migrations := migrate.FileMigrationSource{
Dir: "pkg/datastore/migrations", // TODO: Move to bindata
}
migrate.SetTable("migrations")
n, err := migrate.Exec(dbx.DB, driver, migrations, migrate.Up)
if err != nil {
log.Fatal("Database migrations failed: ", err)
}
if n > 0 {
log.Printf("Applied %d database migrations!\n", n)
}
}