You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.4 KiB
87 lines
1.4 KiB
package mysqldriver
|
|
|
|
import (
|
|
"github.com/pressly/goose"
|
|
"log"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var testDB *DB
|
|
|
|
func TestMain(m *testing.M) {
|
|
testDbConnect := os.Getenv("DB_TEST_CONNECT")
|
|
if testDbConnect == "" {
|
|
testDbConnect = "stufflog_test:test1234@(localhost:3306)/stufflog_test"
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
db, err := Open(testDbConnect)
|
|
if err != nil {
|
|
log.Println("DB ERROR", i, err)
|
|
time.Sleep(time.Second)
|
|
continue
|
|
}
|
|
|
|
testDB = db
|
|
break
|
|
}
|
|
if testDB == nil {
|
|
log.Println("DB FAILED")
|
|
os.Exit(1)
|
|
return
|
|
}
|
|
|
|
err := goose.SetDialect("mysql")
|
|
if err != nil {
|
|
log.Println("GOOSE ERROR", err)
|
|
os.Exit(1)
|
|
return
|
|
}
|
|
|
|
for {
|
|
err = goose.Down(testDB.db.DB, "../../../migrations/mysql")
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
err = goose.Up(testDB.db.DB, "../../../migrations/mysql")
|
|
if err != nil && err != goose.ErrNoNextVersion && err != goose.ErrNoCurrentVersion {
|
|
log.Println("UP ERROR", err)
|
|
os.Exit(3)
|
|
return
|
|
}
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
}
|
|
|
|
func clearTable(tableName string) error {
|
|
// If you really want to SQL inject the test DB, go right ahead!
|
|
_, err := testDB.db.Exec("DELETE FROM " + tableName + " WHERE TRUE")
|
|
return err
|
|
}
|
|
|
|
func mustParseTime(str string) time.Time {
|
|
d, err := time.Parse(time.RFC3339Nano, str)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return d.UTC()
|
|
}
|
|
|
|
func ptrInt(v int) *int {
|
|
return &v
|
|
}
|
|
|
|
func ptrString(v string) *string {
|
|
return &v
|
|
}
|
|
|
|
func ptrBool(v bool) *bool {
|
|
return &v
|
|
}
|