forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbtest.go
47 lines (37 loc) · 1.19 KB
/
dbtest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dbtest
import (
"path"
"runtime"
"testing"
migrate "github.com/rubenv/sql-migrate"
"github.com/stellar/go/support/db/dbtest"
)
func OpenWithoutMigrations(t *testing.T) *dbtest.DB {
db := dbtest.Postgres(t)
// Recoverysigner requires at least Postgres v10 because it uses IDENTITYs
// instead of SERIAL/BIGSERIAL, which are recommended against.
dbVersion := db.Version()
if dbVersion < 10 {
t.Skipf("Skipping test becuase Postgres v%d found, and Postgres v10+ required for this test.", dbVersion)
}
return db
}
func Open(t *testing.T) *dbtest.DB {
db := OpenWithoutMigrations(t)
// Get the folder holding the migrations relative to this file. We cannot
// hardcode "../migrations" because Open is called from tests in multiple
// packages and tests are executed with the current working directory set
// to the package the test lives in.
_, filename, _, _ := runtime.Caller(0)
migrationsDir := path.Join(path.Dir(filename), "..", "dbmigrate", "migrations")
migrations := &migrate.FileMigrationSource{
Dir: migrationsDir,
}
conn := db.Open()
defer conn.Close()
_, err := migrate.Exec(conn.DB, "postgres", migrations, migrate.Up)
if err != nil {
t.Fatal(err)
}
return db
}