Add httpfs DB migrations and generate the input (#35)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-05-11 12:42:10 -04:00
1 parent bd5ddea6e4
commit 7f2aa5ca8f
21 files changed
+576 -139

No files matched your search

+3 -37
View File
@@ -2,11 +2,8 @@ package db
import (
"crypto/rand"
"fmt"
"os"
"testing"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
wrapping "github.com/hashicorp/go-kms-wrapping"
@@ -15,19 +12,15 @@ import (
)
// setup the tests (initialize the database one-time and intialized testDatabaseURL)
func TestSetup(t *testing.T, migrationsDirectory string) (func() error, *gorm.DB) {
if _, err := os.Stat(migrationsDirectory); os.IsNotExist(err) {
t.Fatal("error migrationsDirectory does not exist")
}
func TestSetup(t *testing.T, dialect string) (func() error, *gorm.DB) {
cleanup := func() error { return nil }
var url string
var err error
cleanup, url, err = initDbInDocker(t, migrationsDirectory)
cleanup, url, err = InitDbInDocker(dialect)
if err != nil {
t.Fatal(err)
}
db, err := gorm.Open("postgres", url)
db, err := gorm.Open(dialect, url)
if err != nil {
t.Fatal(err)
}
@@ -51,30 +44,3 @@ func TestWrapper(t *testing.T) wrapping.Wrapper {
return root
}
// initDbInDocker initializes postgres within dockertest for the unit tests
func initDbInDocker(t *testing.T, migrationsDirectory string) (cleanup func() error, retURL string, err error) {
if os.Getenv("PG_URL") != "" {
TestInitStore(t, func() error { return nil }, os.Getenv("PG_URL"), migrationsDirectory)
return func() error { return nil }, os.Getenv("PG_URL"), nil
}
c, url, err := StartDbInDocker()
if err != nil {
return func() error { return nil }, "", fmt.Errorf("could not start docker: %w", err)
}
TestInitStore(t, c, url, migrationsDirectory)
return c, url, nil
}
// TestInitStore will execute the migrations needed to initialize the store for tests
func TestInitStore(t *testing.T, cleanup func() error, url string, migrationsDirectory string) {
// run migrations
m, err := migrate.New(fmt.Sprintf("file://%s", migrationsDirectory), url)
if err != nil {
cleanup()
t.Fatalf("Error creating migrations: %s", err)
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
cleanup()
t.Fatalf("Error running migrations: %s", err)
}
}