database: Look for migrations relative to executable

This commit is contained in:
Andrew Lewis 2016-03-03 15:02:02 +02:00
parent 5a716f93ad
commit 35297b2461

View File

@ -21,7 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"runtime" "path/filepath"
"strings" "strings"
"time" "time"
@ -123,8 +123,11 @@ func Open(config *config.DatabaseConfig) (database.Datastore, error) {
func migrate(dataSource string) error { func migrate(dataSource string) error {
log.Info("running database migrations") log.Info("running database migrations")
_, filename, _, _ := runtime.Caller(1) exeDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
migrationDir := path.Join(path.Dir(filename), "/migrations/") if err != nil {
return err
}
migrationDir := path.Join(exeDir, "/migrations/")
conf := &goose.DBConf{ conf := &goose.DBConf{
MigrationsDir: migrationDir, MigrationsDir: migrationDir,
Driver: goose.DBDriver{ Driver: goose.DBDriver{
@ -234,8 +237,13 @@ func OpenForTest(name string, withTestData bool) (*pgSQLTest, error) {
// Load test data if specified. // Load test data if specified.
if withTestData { if withTestData {
_, filename, _, _ := runtime.Caller(0) exeDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
d, _ := ioutil.ReadFile(path.Join(path.Dir(filename)) + "/testdata/data.sql") if err != nil {
dropDatabase(dataSourceDefaultDatabase, dbName)
log.Error(err)
return nil, err
}
d, _ := ioutil.ReadFile(path.Join(exeDir, "testdata/data.sql"))
_, err = db.(*pgSQL).Exec(string(d)) _, err = db.(*pgSQL).Exec(string(d))
if err != nil { if err != nil {
dropDatabase(dataSourceDefaultDatabase, dbName) dropDatabase(dataSourceDefaultDatabase, dbName)