2015-08-18 05:57:27 +05:30
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2016-02-09 05:31:44 +05:30
|
|
|
"os"
|
|
|
|
"testing"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2015-08-25 04:05:44 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2016-02-09 05:31:44 +05:30
|
|
|
|
|
|
|
"github.com/coreos/dex/db"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
func connect(t *testing.T) *gorp.DbMap {
|
|
|
|
dsn := os.Getenv("DEX_TEST_DSN")
|
|
|
|
if dsn == "" {
|
2016-06-10 04:26:37 +05:30
|
|
|
return db.NewMemDB()
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
c, err := db.NewConnection(db.Config{DSN: dsn})
|
|
|
|
if err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Fatalf("Unable to connect to database: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
if err = c.DropTablesIfExists(); err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Fatalf("Unable to drop database tables: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-08-20 04:10:59 +05:30
|
|
|
if err = db.DropMigrationsTable(c); err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Fatalf("Unable to drop migration table: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-08-20 04:10:59 +05:30
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
n, err := db.MigrateToLatest(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to migrate: %v", err)
|
2015-08-21 01:08:15 +05:30
|
|
|
}
|
2016-02-09 05:31:44 +05:30
|
|
|
if n == 0 {
|
|
|
|
t.Fatalf("No migrations performed")
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
return c
|
|
|
|
}
|