dex/functional/repo/testutil.go

39 lines
745 B
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package repo
import (
"os"
"testing"
2015-08-18 05:57:27 +05:30
"github.com/go-gorp/gorp"
"github.com/coreos/dex/db"
2015-08-18 05:57:27 +05:30
)
func connect(t *testing.T) *gorp.DbMap {
dsn := os.Getenv("DEX_TEST_DSN")
if dsn == "" {
t.Fatal("DEX_TEST_DSN environment variable not set")
}
2015-08-18 05:57:27 +05:30
c, err := db.NewConnection(db.Config{DSN: dsn})
if err != nil {
t.Fatalf("Unable to connect to database: %v", err)
2015-08-18 05:57:27 +05:30
}
if err = c.DropTablesIfExists(); err != nil {
t.Fatalf("Unable to drop database tables: %v", err)
2015-08-18 05:57:27 +05:30
}
if err = db.DropMigrationsTable(c); err != nil {
t.Fatalf("Unable to drop migration table: %v", err)
2015-08-18 05:57:27 +05:30
}
n, err := db.MigrateToLatest(c)
if err != nil {
t.Fatalf("Unable to migrate: %v", err)
}
if n == 0 {
t.Fatalf("No migrations performed")
}
2015-08-18 05:57:27 +05:30
return c
}